Sponsored Content
Full Discussion: Send an email with C/C++
Top Forums Programming Send an email with C/C++ Post 302318231 by arunchaudhary19 on Thursday 21st of May 2009 06:03:20 AM
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;
}

 

7 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

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. 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

6. 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

7. 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
SOCKET_BIND(3)								 1							    SOCKET_BIND(3)

socket_bind - Binds a name to a socket

SYNOPSIS
bool socket_bind (resource $socket, string $address, [int $port]) DESCRIPTION
Binds the name given in $address to the socket described by $socket. This has to be done before a connection is be established using socket_connect(3) or socket_listen(3). PARAMETERS
o $socket - A valid socket resource created with socket_create(3). o $address - If the socket is of the AF_INET family, the $address is an IP in dotted-quad notation (e.g. 127.0.0.1). If the socket is of the AF_UNIX family, the $address is the path of a Unix-domain socket (e.g. /tmp/my.sock). o $port (Optional) - The $port parameter is only used when binding an AF_INET socket, and designates the port on which to listen for connections. RETURN VALUES
Returns TRUE on success or FALSE on failure. The error code can be retrieved with socket_last_error(3). This code may be passed to socket_strerror(3) to get a textual explanation of the error. EXAMPLES
Example #1 Using socket_bind(3) to set the source address <?php // Create a new socket $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // An example list of IP addresses owned by the computer $sourceips['kevin'] = '127.0.0.1'; $sourceips['madcoder'] = '127.0.0.2'; // Bind the source address socket_bind($sock, $sourceips['madcoder']); // Connect to destination address socket_connect($sock, '127.0.0.1', 80); // Write $request = 'GET / HTTP/1.1' . " " . 'Host: example.com' . " "; socket_write($sock, $request); // Close socket_close($sock); ?> NOTES
Note This function must be used on the socket before socket_connect(3). Note Windows 9x/ME compatibility note: socket_last_error(3) may return an invalid error code if trying to bind the socket to a wrong address that does not belong to your machine. SEE ALSO
socket_connect(3), socket_listen(3), socket_create(3), socket_last_error(3), socket_strerror(3). PHP Documentation Group SOCKET_BIND(3)
All times are GMT -4. The time now is 03:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy