how to send mails from VC++ program without using outlook
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 47

Thread: how to send mails from VC++ program without using outlook

  1. #1
    Join Date
    Jul 2004
    Posts
    9

    how to send mails from VC++ program without using outlook

    Hi everybody,

    I m writting an application which calulates some results and send them thr e-mail.I want my VC++ program to send this mail to specified address without using Outlook Express or Microsoft Outlook...or without opening any browser window...in fact i want to send them undetected..that means user shud not know abt sending mails....plzzzzz anybody tell me how to do this..its urgent...do u think socket programming will do this?

    thanx in advance

  2. #2
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538
    sushb4u,

    Try looking into the tools by MarshallSoft, I have used their lib/dll e-mail tools for some time now with very very very good luck. They are very easy to code into your app and invisible to the user as they use their own functions and do not use Outlook or Outlook Express. They can be found at. www.marshallsoft.com


    HTH Good luck

    Jim

  3. #3
    Join Date
    Jul 2004
    Posts
    9
    hi jim..thanks a lot for ur quick reply..but will u plz tell me in detail??i mean what shud i look for on this given url??and how can i use that tools in my code??is it necessary to load those tools on user's computer??plzzzzz reply back...its a bit urgent.

    again thanks in advance.

  4. #4
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538
    You'll want to look at the "SEE4C" Email engine for c/c++ under the "Products" listing, it does require a dll to be on the users computer to send email in the background. There is full documentation online for reference/programmer and can be viewed. I looked and tested many email tools over a 4 month trial for one of my apps and found this one to be the best and easiest to understand and get running quickly. They do offer eval versions also.


    Jim

  5. #5
    Join Date
    May 2003
    Location
    Jodhpur -> Rajasthan -> INDIA
    Posts
    453
    Hi Vanaj

    Well if this library be downloaded, can it be used for longer time? As it is evaluation version, i think there might be some what resisting the use for longer time.

    Any comments
    Leave Your Mark Wherever You Go
    http://www.danasoft.com/sig/d0153030Sig.jpg

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    The following is a simple console application which sends an email...
    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <iostream.h>
    #include <windows.h>
    #include <winsock2.h>
    
    #pragma comment(lib, "ws2_32.lib")
    
    // Insist on at least Winsock v1.1
    const VERSION_MAJOR = 1;
    const VERSION_MINOR = 1;
    
    #define CRLF "\r\n"                 // carriage-return/line feed pair
    
    void ShowUsage(void)
    {
      cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
           << "Example: SENDMAIL smtp.myisp.com rcvr@elsewhere.com my_id@mydomain.com message.txt" << endl;
     
      exit(1);
    }
    
    // Basic error checking for send() and recv() functions
    void Check(int iStatus, char *szFunction)
    {
      if((iStatus != SOCKET_ERROR) && (iStatus))
        return;
      
      cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
    }
    
    int main(int argc, char *argv&#091;&#093;)
    {
      int         iProtocolPort        = 0;
      char        szSmtpServerName&#091;64&#093; = "";
      char        szToAddr&#091;64&#093;         = "";
      char        szFromAddr&#091;64&#093;       = "";
      char        szBuffer&#091;4096&#093;       = "";
      char        szLine&#091;255&#093;          = "";
      char        szMsgLine&#091;255&#093;       = "";
      SOCKET      hServer;
      WSADATA     WSData;
      LPHOSTENT   lpHostEntry;
      LPSERVENT   lpServEntry;
      SOCKADDR_IN SockAddr;
    
      // Check for four command-line args
      if(argc != 5)
        ShowUsage();
    
      // Load command-line args
      lstrcpy(szSmtpServerName, argv&#091;1&#093;);
      lstrcpy(szToAddr, argv&#091;2&#093;);
      lstrcpy(szFromAddr, argv&#091;3&#093;);
    
      // Create input stream for reading email message file
      ifstream MsgFile(argv&#091;4&#093;);
    
      // Attempt to intialize WinSock (1.1 or later)
      if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
      {
        cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
    
        return 1;
      }
    
      // Lookup email server's IP address.
      lpHostEntry = gethostbyname(szSmtpServerName);
      if(!lpHostEntry)
      {
        cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
    
        return 1;
      }
    
      // Create a TCP/IP socket, no specific protocol
      hServer = socket(PF_INET, SOCK_STREAM, 0);
      if(hServer == INVALID_SOCKET)
      {
        cout << "Cannot open mail server socket" << endl;
       
        return 1;
      }
    
      // Get the mail service port
      lpServEntry = getservbyname("mail", 0);
    
      // Use the SMTP default port if no other port is specified
      if(!lpServEntry)
        iProtocolPort = htons(IPPORT_SMTP);
      else
        iProtocolPort = lpServEntry->s_port;
    
      // Setup a Socket Address structure
      SockAddr.sin_family = AF_INET;
      SockAddr.sin_port   = iProtocolPort;
      SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
    
      // Connect the Socket
      if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
      {
        cout << "Error connecting to Server socket" << endl;
    
        return 1;
      }
    
      // Receive initial response from SMTP server
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
    
      // Send HELO server.com
      sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
    
      // Send MAIL FROM: <sender@mydomain.com>
      sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
    
      // Send RCPT TO: <receiver@domain.com>
      sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
    
      // Send DATA
      sprintf(szMsgLine, "DATA%s", CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
    
      // Send all lines of message body (using supplied text file)
      MsgFile.getline(szLine, sizeof(szLine));             // Get first line
    
      do         // for each line of message text...
      {
        sprintf(szMsgLine, "%s%s", szLine, CRLF);
        Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
        MsgFile.getline(szLine, sizeof(szLine)); // get next line.
      } while(MsgFile.good());
    
      // Send blank line and a period
      sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message");
    
      // Send QUIT
      sprintf(szMsgLine, "QUIT%s", CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
    
      // Report message has been sent
      cout << "Sent " << argv&#091;4&#093; << " as email message to " << szToAddr << endl;
    
      // Close server socket and prepare to exit.
      closesocket(hServer);
    
      WSACleanup();
    
      return 0;
    }
    Attachments need to be sent by encoding the message in MIME format. You can find the RFC at http://www.nacs.uci.edu/indiv/ehood...45/rfc2045.html for example.

    Additionally you might take a look at http://codeproject.com/internet/csmtpconn.asp. It wraps the SMTP protocol including the mentioned MIME attachments...
    Last edited by Andreas Masur; July 1st, 2004 at 09:53 AM.
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  7. #7
    Join Date
    Jun 2004
    Location
    India
    Posts
    432
    Investigate MAPI, look at CDONTS.DLL
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  8. #8
    Join Date
    Jul 2004
    Posts
    9
    Hi Andreas,

    how to execute this code??if i create new console application where to add this code??

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...the code I posted can basically copied directly into a console application. So simply create an empty console project, add an empty *.cpp file and copy/paste the whole code into the file...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  10. #10
    Join Date
    Jul 2004
    Posts
    9
    it gives this error

    d:\vcprojects\m\m.cpp(165) : fatal error C1010: unexpected end of file while looking for precompiled header directive

  11. #11
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457
    You can also specify the Subject, an alias for the sender and receiver (besides just their e-mail addresses), and the type of message to compose (HTML, plain text).

    Here's a partial example of how to send an HTML e-mail from CodeGuru:
    Code:
    static const TCHAR szMailFrom[] = TEXT("CodeGuru");
    static const TCHAR szMime[]     = TEXT("content-type: text/html; charset=us-ascii");
    
    ...
    
    wsprintf(szSend, TEXT("DATA\r\n"));
    send(hSocket, (PSTR)szSend, lstrlen(szSend), 0);
    recv(hSocket, (PSTR)szRecv, sizeof(szRecv) / sizeof(TCHAR), 0);
    lstrcpy(szRecv, "");
    
    // Send message header...
    wsprintf(szSend, TEXT("From: %s\r\nTo: %s\r\nSubject: %s\r\n%s\r\n\r\n"), 
                     szMailFrom, szMailTo, szMailSubject, szMime);
    send(hSocket, (PSTR)szSend, lstrlen(szSend), 0);
    
    // Send message body.  Do not use wsprintf here.  It only allows for 1024 bytes.
    lstrcpyn(szSend, szMailBody, ARRAYSIZE(szSend));
    send(hSocket, (PSTR)szSend, lstrlen(szSend), 0);
    
    // Send message end...
    lstrcpy(szSend, TEXT("\r\n.\r\n"));
    send(hSocket, (PSTR)szSend, lstrlen(szSend), 0);
    recv(hSocket, (PSTR)szRecv, sizeof(szRecv) / sizeof(TCHAR), 0);
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  12. #12
    Join Date
    Jun 2004
    Location
    England
    Posts
    90

    here it is

    Andreas' code can be used, you will need to modify it for you purpose though.

    Here is a COM object that can be used to populate and send email from the VC++ application. Its very easy to use.
    Just make sure you initialise the COM libraries

    http://www.programmersheaven.com/zon...1168/25879.htm

    Good luck.

    If this is not what you are after check out the Message Application Programming Interface as suggested earlier.
    The most knowledgeable people are those who know that they know nothing.

  13. #13
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by sushb4u
    d:\vcprojects\m\m.cpp(165) : fatal error C1010: unexpected end of file while looking for precompiled header directive
    Well...there was indeed missing a " in the posted code...anyway...attached is a compiled console application including the source...
    Attached Files Attached Files
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  14. #14
    Join Date
    Jun 2004
    Location
    England
    Posts
    90
    Andreas,
    in the code you attached why do you send line by line as opposed to the entire data buffer at once?
    I am sure there is a good reason behind it, but I can't figure it out?

    Thanks
    The most knowledgeable people are those who know that they know nothing.

  15. #15
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457
    Originally posted by Tamerocyte
    Andreas,
    in the code you attached why do you send line by line as opposed to the entire data buffer at once?
    I am sure there is a good reason behind it, but I can't figure it out?

    Thanks
    Maybe because sprintf, like wsprintf (as I found out), has a limit of 1024 characters. Not always enough for the entire message body.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center