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::GetAddrInfo::Socket6api(3pm)			User Contributed Perl Documentation		      Socket::GetAddrInfo::Socket6api(3pm)

NAME
"Socket::GetAddrInfo::Socket6api" - Provide Socket::GetAddrInfo functions using Socket6 API SYNOPSIS
use Socket qw( AF_UNSPEC SOCK_STREAM ); use Socket::GetAddrInfo::Socket6api qw( getaddrinfo getnameinfo ); my $sock; my @res = getaddrinfo( "www.google.com", "www", AF_UNSPEC, SOCK_STREAM ); die "Cannot resolve name - $res[0]" if @res == 1; while( @res >= 5 ) { my ( $family, $socktype, $protocol, $addr, undef ) = splice @res, 0, 5, (); $sock = IO::Socket->new(); $sock->socket( $family, $socktype, $protocol ) or undef $sock, next; $sock->connect( $addr ) or undef $sock, next; last; } if( $sock ) { my ( $host, $service ) = getnameinfo( $sock->peername ); print "Connected to $host:$service " if defined $host; } DESCRIPTION
Socket::GetAddrInfo provides the functions of "getaddrinfo" and "getnameinfo" using a convenient interface where hints and address structures are represented as hashes. Socket6 also provides these functions, in a form taking and returning flat lists of values. This module wraps the functions provided by "Socket::GetAddrInfo" to provide them in an identical API to "Socket6". It is intended to stand as a utility for existing code written for the "Socket6" API to use these functions instead. FUNCTIONS
@res = getaddrinfo( $host, $service, $family, $socktype, $protocol, $flags ) This version of the API takes the hints values as separate ordered parameters. Unspecified parameters should be passed as 0. If successful, this function returns a flat list of values, five for each returned address structure. Each group of five elements will contain, in order, the "family", "socktype", "protocol", "addr" and "canonname" values of the address structure. If unsuccessful, it will return a single value, containing the string error message. To remain compatible with the "Socket6" interface, this value does not have the error integer part. ( $host, $service ) = getnameinfo( $addr, $flags ) This version of the API returns only the host name and service name, if successfully resolved. On error, it will return an empty list. To remain compatible with the "Socket6" interface, no error information will be supplied. AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-06-08 Socket::GetAddrInfo::Socket6api(3pm)
All times are GMT -4. The time now is 09:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy