Send an email with C/C++


 
Thread Tools Search this Thread
Top Forums Programming Send an email with C/C++
# 1  
Old 05-21-2009
Send an email with C/C++

HI,
I have a requirement to send an email through C++ code. We are already using sockets. I like a reference point to how we proceed on it. Like in starting we like to send on gamil or yahoo account. Is it possible with C code?

Thanks in advance.
# 2  
Old 05-21-2009
Yes it is possible.
Have a look on SMTP protocol.
# 3  
Old 05-21-2009
Any reference or sample ?
# 4  
Old 05-21-2009
Might this be helpful to you...

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

#include <stdio.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 "winsock2.h"      /* WSAGetLastError, WSAStartUp  */
#       define snprintf _snprintf
#endif

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

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);
}

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;
   }
    
    sendmail_write(sock, "HELO %s\n",       from);    // greeting
    sendmail_write(sock, "MAIL FROM: %s\n", from);    // from
    sendmail_write(sock, "RCPT TO: %s\n",   to);      // to
    sendmail_write(sock, "DATA\n",          NULL);    // begin data

    // next comes mail headers
    sendmail_write(sock, "From: %s\n",      from);
    sendmail_write(sock, "To: %s\n",        to);
    sendmail_write(sock, "Subject: %s\n",   subject);

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

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

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

    close(sock);

    return 0;
}


int main(int argc, char *argv[]) {
    
    int ret = sendmail(
        "from@from-host.org",   /* from     */
        "to@to-host.org",       /* to       */
        "Subject",              /* subject  */
        "body",                 /* body     */
        "hostname",             /* hostname */
        25                      /* port     */
    );

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

    return ret;
}

# 5  
Old 05-21-2009
General References: RFC 821, superseded by RFC 2821, and Wikipedia on SMTP
# 6  
Old 05-21-2009
Thanks arunchaudhary19 and puldi.
I will try and let you know.
# 7  
Old 10-28-2009
I have used above code

I have used above code but its fail.
when I try to send mail to my gmail account as follows

[root@stripe ~]# ./a.out 209.85.135.109 25
Please enter the message: 220 mx.google.com ESMTP 23sm3964177mun.33

HELO
250 mx.google.com at your service

MAIL FROM:<darshan.ghumare@gmail.com>
530 5.7.0 Must issue a STARTTLS command first. 23sm3964177mun.33


What should I do now?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. SuSE

Send outgoing email to my GroupWise email

Dear users, I have Linux server whose versions are Suse 10 SP 3 and Suse 11. I am trying to send email from these servers to my GroupWise email account. In /etc/postfix/main.cf file, The current value of MYHOSTNAME is LINUX.LOCAL. What should be the right value of MYHOSTNAME? Is... (0 Replies)
Discussion started by: JDBA
0 Replies

2. Shell Programming and Scripting

send email to email id which is having # symbol

Hi, I have one requirement to send email to email id which is having # ( in the begining of the email id). I'm using mailx command to send an email. But not receiving those emails, but email status is showing email sent successful. Code which i'm using is : cat file1.txt | mailx -s... (3 Replies)
Discussion started by: latika
3 Replies

3. UNIX for Dummies Questions & Answers

new to ldap, send email to a ou or group, and see a list from email client

hi, i'm running openldap on ubuntu 10.04, creating new items with apache directory studio (windows version). i use the ldap just as an address book to our small office (email clients are windows live mail 2009, 2011, microsoft outlook 2007 and 2010). a. i cant see a list of the contacts,... (0 Replies)
Discussion started by: V4705
0 Replies

4. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

5. Solaris

Send an email from Solaris using Linux email server

Hello everyone I have a problem and I need your help: I have a Solaris 10 and Solaris 8 UNIX Servers, and Linux Centos4 as email server. I need send an email from Solaris servers preferably using Centos4 email server. I have no mail service configured in my Solaris computers (1 Reply)
Discussion started by: aflores
1 Replies

6. UNIX for Dummies Questions & Answers

Send email where # is in the email address - Using Unix

Hi All, How do I send an email using malix where email address contains a #. I have a email address like this : #test@test.com I want to send email like malix -s "TEST" #test@test.com < SOMEFILE I tried \# but doesn't work. Please let me know how we can achieve this? I am in... (1 Reply)
Discussion started by: jingi1234
1 Replies

7. UNIX for Advanced & Expert Users

Unable to send eMail from a UNIX-Host ( using mailx ) to a Outlook-email-addres(Win)

Hi A) I am able to send eMail using mailx from a UNIX ( solaris 8 ) host to my Outlook-email-ID : FName.Surname@Citigroup.com ( This is NOT my actual -eMail-ID). But in Outlook the "From :" eMail address is displayed as " usr1@unix-host1.unregistered.email.citicorp.com " .i.e the words... (2 Replies)
Discussion started by: Vetrivela
2 Replies
Login or Register to Ask a Question