473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to send email in program?

175 100+
I've got an application that produces some numbers that I would like to have emailed to myself. I have no idea how to do this though.

I've messed with sockets before, but I didn't get so in depth that I know how to do this. I basically just used it to grab html. I'm mentioning this because it's the only possible approach I know of.

I've searched the internet quite a bit for some help on emailing using c++ and found nothing. If anyone can help me out, or give me some tips I would appreciate it.
Jul 5 '07 #1
4 10395
gpraghuram
1,275 Expert 1GB
I've got an application that produces some numbers that I would like to have emailed to myself. I have no idea how to do this though.

I've messed with sockets before, but I didn't get so in depth that I know how to do this. I basically just used it to grab html. I'm mentioning this because it's the only possible approach I know of.

I've searched the internet quite a bit for some help on emailing using c++ and found nothing. If anyone can help me out, or give me some tips I would appreciate it.
do a search in this forum and i rememeber some threads already discussed regarding this.
If u are using unix then try using mail or mailx command

Raghuram
Jul 5 '07 #2
vermarajeev
180 100+
Check the below code. Works fine for me on windows

Expand|Select|Wrap|Line Numbers
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fstream.h>
  6. #include <iostream.h>
  7. #include <windows.h>
  8. #include <winsock2.h>
  9.  
  10. #pragma comment(lib, "ws2_32.lib")
  11.  
  12. // Insist on at least Winsock v1.1
  13. const VERSION_MAJOR = 1;
  14. const VERSION_MINOR = 1;
  15.  
  16. #define CRLF "\r\n"                 // carriage-return/line feed pair
  17.  
  18. void ShowUsage(void)
  19. {
  20.   cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
  21.        << "Example: SENDMAIL smtp.myisp.com rcvr@elsewhere.com my_id@mydomain.com message.txt" << endl;
  22.  
  23.   exit(1);
  24. }
  25.  
  26. // Basic error checking for send() and recv() functions
  27. void Check(int iStatus, char *szFunction)
  28. {
  29.   if((iStatus != SOCKET_ERROR) && (iStatus))
  30.     return;
  31.  
  32.   cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
  33. }
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.   int         iProtocolPort        = 0;
  38.   char        szSmtpServerName[64] = "";
  39.   char        szToAddr[64]         = "";
  40.   char        szFromAddr[64]       = "";
  41.   char        szBuffer[4096]       = "";
  42.   char        szLine[255]          = "";
  43.   char        szMsgLine[255]       = "";
  44.   SOCKET      hServer;
  45.   WSADATA     WSData;
  46.   LPHOSTENT   lpHostEntry;
  47.   LPSERVENT   lpServEntry;
  48.   SOCKADDR_IN SockAddr;
  49.  
  50.   // Check for four command-line args
  51.   if(argc != 5)
  52.     ShowUsage();
  53.  
  54.   // Load command-line args
  55.   lstrcpy(szSmtpServerName, argv[1]);
  56.   lstrcpy(szToAddr, argv[2]);
  57.   lstrcpy(szFromAddr, argv[3]);
  58.  
  59.   // Create input stream for reading email message file
  60.   ifstream MsgFile(argv[4]);
  61.  
  62.   // Attempt to intialize WinSock (1.1 or later)
  63.   if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
  64.   {
  65.     cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
  66.  
  67.     return 1;
  68.   }
  69.  
  70.   // Lookup email server's IP address.
  71.   lpHostEntry = gethostbyname(szSmtpServerName);
  72.   if(!lpHostEntry)
  73.   {
  74.     cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
  75.  
  76.     return 1;
  77.   }
  78.  
  79.   // Create a TCP/IP socket, no specific protocol
  80.   hServer = socket(PF_INET, SOCK_STREAM, 0);
  81.   if(hServer == INVALID_SOCKET)
  82.   {
  83.     cout << "Cannot open mail server socket" << endl;
  84.  
  85.     return 1;
  86.   }
  87.  
  88.   // Get the mail service port
  89.   lpServEntry = getservbyname("mail", 0);
  90.  
  91.   // Use the SMTP default port if no other port is specified
  92.   if(!lpServEntry)
  93.     iProtocolPort = htons(IPPORT_SMTP);
  94.   else
  95.     iProtocolPort = lpServEntry->s_port;
  96.  
  97.   // Setup a Socket Address structure
  98.   SockAddr.sin_family = AF_INET;
  99.   SockAddr.sin_port   = iProtocolPort;
  100.   SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  101.  
  102.   // Connect the Socket
  103.   if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
  104.   {
  105.     cout << "Error connecting to Server socket" << endl;
  106.  
  107.     return 1;
  108.   }
  109.  
  110.   // Receive initial response from SMTP server
  111.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
  112.  
  113.   // Send HELO server.com
  114.   sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF);
  115.   Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
  116.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
  117.  
  118.   // Send MAIL FROM: <sender@mydomain.com>
  119.   sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
  120.   Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
  121.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
  122.  
  123.   // Send RCPT TO: <receiver@domain.com>
  124.   sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);
  125.   Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
  126.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
  127.  
  128.   // Send DATA
  129.   sprintf(szMsgLine, "DATA%s", CRLF);
  130.   Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
  131.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
  132.  
  133.   // Send all lines of message body (using supplied text file)
  134.   MsgFile.getline(szLine, sizeof(szLine));             // Get first line
  135.  
  136.   do         // for each line of message text...
  137.   {
  138.     sprintf(szMsgLine, "%s%s", szLine, CRLF);
  139.     Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
  140.     MsgFile.getline(szLine, sizeof(szLine)); // get next line.
  141.   } while(MsgFile.good());
  142.  
  143.   // Send blank line and a period
  144.   sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
  145.   Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message");
  146.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message");
  147.  
  148.   // Send QUIT
  149.   sprintf(szMsgLine, "QUIT%s", CRLF);
  150.   Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
  151.   Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
  152.  
  153.   // Report message has been sent
  154.   cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
  155.  
  156.   // Close server socket and prepare to exit.
  157.   closesocket(hServer);
  158.  
  159.   WSACleanup();
  160.  
  161.   return 0;
  162. }
Hey got one question to ask...
when I type
Expand|Select|Wrap|Line Numbers
  1. > telnet alt1.gmail-smtp-in.l.google.com 25 
on terminal I get this error
Expand|Select|Wrap|Line Numbers
  1. Trying 64.233.185.27...
  2. telnet: connect to address 64.233.185.27: No route to host
  3. Trying 64.233.185.114...
  4. telnet: connect to address 64.233.185.114: No route to host
  5.  
I got to know my network admin has blocked the routes. how can I suggest him to correct this problem or is there anything I can do myself, . Any idea????

Thanks.
Jul 5 '07 #3
manontheedge
175 100+
sorry, I forgot to mention I'm working with Windows XP and Visual Studio 6.

vermarajeev, thanks for posting that code, I'll mess around with it and see if I can get it to work.
Jul 5 '07 #4
How would you modify the code to send an attachment
Jul 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andrew Chan | last post by:
Dear All, I'm new in VS C++.I would like to write a c++ program that can be send email with attachment,and all the input will be call from a file ( such as To Email address, attachment path). ...
1
by: Jay McGrath | last post by:
Help - trying to send a simple text email with with as little user intervention. I am trying to create a button in my Access application that will automatically send a simple text email. It...
5
by: Andreas | last post by:
I am working with three computers, my developing computer, a Web Server and a Mail Server (Exchange). I am trying to send a email from the Web Server via the Mail Server to a valid email address...
1
by: John | last post by:
Hi, I have a question to ask in how to send e-mail through a program written by VB.Net with the help of MS Word and Outlook? The platform will be like this: OS - Win 2K Pro Office 2000 with...
1
by: Jozef | last post by:
Hello, I have a client that just switched to Windows 2003 server. Their website has always run fine under Windows 2000, using an email program queue_manager.exe to send order and support emails...
2
by: Sivaswami Jeganathan | last post by:
how to send email from an asp page ? -- Sivaswami Jeganathan
3
by: trynittee | last post by:
Hello, Thank you so much in advance to those who will take the time to read and answer. This group is a wonderful site. I have inherited a contacts database that sits on a server. Users...
2
by: Andy | last post by:
Hi, I have a C# application. In part of it, users can define distribution lists, which is basically an email group. To actually send the email, I've been using Process.Start with a mailto:...
7
Atran
by: Atran | last post by:
Hello, I use Html 4.0 in Dreamweaver program, to send an email, I know that I must put a Form, and I make the Form Action: mailto:MyEmail@hotmail.com Then I put to the Form three textboxes:...
7
by: TBoon | last post by:
how do u send email in vb.net? any samples?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.