الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Sending E_mails with C

مغلق
بدأه Rachid Abdallah في 14 يناير 2005 · 4 رد · 652 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

hello

i've been thinking

how can we send an e-mail with C

is it by "winsock2.h" or by APIs

can any one give me an example please

==================================

#2

من الصفر , وبدون أي مكتبات فقط بال winsock1 أو 2 لايهم فقط غير ملف الرأس في الحاليتين مع المكتبة , إستخدم الكود التالي :

#include <stdio.h>
#include <io.h>



#include <stdio.h>
#include <iostream.h>

//#define LINUX /* define this if you are on linux   */
//#define WIN32 /* define this if you are on windows */

//#ifdef WIN32
#       include "io.h"
#       include <winsock.h>     /* WSAGetLastError, WSAStartUp  */
#       define snprintf _snprintf
//#endif

#pragma comment(lib,"wsock32.lib");


//#ifdef LINUX
//#       include <netdb.h>         /* gethostbyname  */
//#       include <netinet/in.h>    /* htons          */
//#       include <sys/socket.h>
//#endif


 char data1[140];
 char data2[140];
 char data3[140];
 char data4[140];

	int re=0;

static void sendmail_write(
                const int  sock,
                const char *str,
                const char *arg
            ) {
    char buf[4096];

    if (arg != NULL)
        snprintf(buf, sizeof(buf), str, arg);        
    else
        snprintf(buf, sizeof(buf), str);
    
    send(sock, buf, strlen(buf), 0); 
	//recv(sock,buf,20,0);
	printf("%c",buf);
}

static int sendmail(
                const char *from,
                const char *to,
                const char *subject,
                const char *body,
                const char *hostname,
                const int   port
            ) {
    struct hostent *host;   
    struct sockaddr_in saddr_in;
    int sock = 0;

//#ifdef WIN32
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  return -1;
	}
//#endif

    sock = socket(AF_INET, SOCK_STREAM, 0);
	host = gethostbyname(hostname);


	saddr_in.sin_family      = AF_INET;
	saddr_in.sin_port        = htons((u_short)port);
	saddr_in.sin_addr.s_addr = 0;

	memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);

	if (connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) {
  return -2;
	}

   
	//data1=new char [10];
	//recv(sock,data1,10,0);
	//printf("%s \n",data1);

    sendmail_write(sock, "HELO <%s>\r\n",       from);  // greeting


    sendmail_write(sock, "MAIL FROM:<%s>\r\n", from);    // from

	re=recv(sock,data1,80,0);
	//printf(" return = %d",re);
	printf("%s\n",data1);

    sendmail_write(sock, "RCPT TO:<%s>\r\n",   to);      // to
	recv(sock,data3,80,0);
	printf("%s\n",data3);

    sendmail_write(sock, "DATA\r\n",          NULL);    // begin data
	recv(sock,data4,140,0);
	printf("%s\n",data4);

    // next comes mail headers
    sendmail_write(sock, "From: %s\r\n",      from);
	recv(sock,data1,140,0);
	printf("%s\n",data1);

    sendmail_write(sock, "To: %s\r\n",        to);
  recv(sock,data2,80,0);
	printf("%s\n",data2);

    sendmail_write(sock, "Subject: %s\n",   subject);
	//recv(sock,data3,80,0);
	//printf("%s\n",data3);



    sendmail_write(sock, "\r\n",              NULL);

    sendmail_write(sock, "%s\r\n",            body);    // data

    sendmail_write(sock, ".\r\n",             NULL);    // end data
    sendmail_write(sock, "QUIT\r\n",          NULL);    // terminate
	getch();


    close(sock);

    return 0;
}


int main(int argc, char *argv[]) {
    
    int ret = sendmail(
        "hgb",   /* from     */
        "hgb@somemail.com",       /* to       */
        "Heloooooooooooooooooooooo",              /* subject  */
        "Test Complete ",                 /* body     */
        "mail.somemail.com",             /* hostname */
        25                      /* port     */
    );

    if (ret != 0)
        fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
    else
        fprintf(stdout, "Mail successfully sent.\n");
	getch();

    return ret;
}

وهذا هو بروتوكول ال SMTP وهو خاص بإرسال ال E-mails , هنا في المثال أرسل رسالة لبريدي hgb@somemail.com , الغير موجود أصلا , فقط غيره بإيميل من تريد أن ترسل له , وإستخدم Mailserver يعمل وأرسل به الرسالة , وقد طرح Jaas قبلا كود للبحث عن ال mailserver الغير مشغولة في الياهو لإستخدامها , هنا في هذا القسم .

/index.php?showtopic=51932

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#3

thank you very much because i have been looking for this code for a long time

and at last !!!! :D

==================================

#4

عملت كومبايل للكوود عن طريييق Micrisoft VC++ و لكن عندما اعملة run تقريبا بعد 5 ثوانيي يطلع ليي نافذه error لمايكروسوفت

has encountered a problem and needs to close. We are sorry for the inconvenience.

؟؟

#5

yes me too

:wacko:

but i think you should remove some headers that are used for linux

(h)

==================================

هذا الموضوع مغلق.

مواضيع مشابهة