We help IT Professionals succeed at work.

E-mail program

cjava
cjava asked
on
Medium Priority
350 Views
Last Modified: 2012-05-04
Hello,
A while back I created a Java based e-mail program. I now wish to convert it to C++, but I have no clue where to start. The general flow of the Java program was:

to:
(and then once the address was entered and return was pressed)
from:
(and then once the address was entered and return was pressed)
subject:
(and then once the subject was entered and return was pressed)
message body:
(and then once the message was entered and return was pressed)
sends message via the mail server of the sender.

I have some C++ experiance, and it seems similar to Java, but when it comes to networking and electronic mail, I'm pretty lost. Links or sample source-code would be highly appreciated. Thank you!

Note: I want to stick as close to standard C++ as possible (no MFC or anything like that)
Comment
Watch Question

There are many ways to make this in Windows:
1. Using Mapi: Simple MAPI Console Application(send + reseive)
http://support.microsoft.com/support/kb/articles/q171/0/96.asp
2. Using sockets directly:
 Send Mail (socket 2) "Davide Libenzi"
http://www.xmailserver.org/davide.html
3. Using some ActiveX component:
  JMail component to send and receive mails.
  you can download it free from
http:\\dimac.net
 

Author

Commented:
how portable would these solutions be?

Author

Commented:
Is there any solution that isn't so dependent on Win32 API? How much mail or networking functionality is implemented in C++?

Commented:
>>how portable would these solutions be?
usually not much; depends on the approach you choose to take

>>Is there any solution that isn't so dependent on Win32 API?
 Of course. The most generic one would be to learn some socket programming (only the basic stuff which is quite platform independent (POSIX) even on Windows 9x) and then read about the SMTP protocol (that's the protocol used to send mails). It's not hard to use and you'll end up with an almost 100% portable source code.

>>How much mail or networking functionality
is implemented in C++?
NONE. And in fact, there's none in java too, but you're used with the whole set of classes which come with java. But the language, c++ or java, has no mail specifications defined (and it's a wise thing).

Commented:
cjava, a word of caution: don't end up being dissapointed by C++; You'll write maybe 20 times (how about 100) more code than in java to get the thing done. If sending a mail it's all you care about maybe you'd better stick to java. C++ has it's advantages but it tends to ruin your keyboard. Thinks will be even worse since you want something portable where fancy stuff like ActiveX have no place.

Oh, i remembered, there's a company "/n software" (if I remember well) which sells some libraries which deal with network stuff and which supports multiple platforms. But I don't think it's cheap.

Author

Commented:
I've been using C++ for a while, just not on the networking side. I realize that it takes alot more code to do things in C++. However using C++ is almost nessecary in this case. Basically, if it should be cross-platform and implemented in a most portable way, I should use socket programming? If so, could you give me some resources? (links, books)

Thank You!

Commented:
books...I don't know.
About links, that's another story; there are tons of good resources on this topic:
 www.informit.com has some articles on this topic
 any linux guide on networking is pretty useful
 www.codeguru.com
 
 www.experts-exchange.com - yep, there were some very good problems on this subject

most interesting: www.google.com :)
>>could you give me some resources? (links, books)

Sockets Tutorial
http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html
Windows Sockets: A Quick And Dirty Primer
http://world.std.com/~jimf/papers/sockets/winsock.html
Winsock Programming Tutorial
http://users.neca.com/vmis/wsock.htm
And I recommend the book "WinSock 2.0" published by IDG Books.
http://www.sockaddr.com/WinSockBook.html
more sockets tutorials:
http://www.ibr.cs.tu-bs.de/~harbaum/docs/bsd-tut/sockets.html
some files of a socket tutorial:
http://www.cs.pdx.edu/~jrb/tcpip/sockets/
sockets links:
http://www.calweb.com/~chriss/tech/socklink.html

Author

Commented:
thanks, one wuick question: Is there a difference between Winsockets and Sockets?

Author

Commented:
and one last thing, if I'm using VC++6 on Windows and KDevelop on Linux, do I need any extra packages? (I noticed something about the GNU C Library, do I need this?, is it included?) Thank you very much!
>>Is there a difference between Winsockets and Sockets
Yes,there is, winsock includes many API, that makes programming more easy. In pure socket you must use only  a small(~30) number standart functions, but it is sufficent for make apps.
>>do I need any extra packages?
There are some sockets libraries for some OS:
EasySocket
http://roncarolo.eu.org/ESlib/
GNOME socket
http://gnome-socket.sourceforge.net/
Socket Library SDK
http://www.asyncsystems.com/tcpip.htm
....
But in my apps, for example, I use Winsock and don't need
something else. Alex

Author

Commented:
sorry, I haven't been able to get on EE for the past couple of days (was the server down?), thank you Alex, you have provided me with lots of information and I will give you the answer if you can clarify one thing for me, if I want to code using pure sockets, do I need aditional packages? My IDEs are VC++6 and Kdevelop (IDE front end to GCC or GPP). Thank you very much!
>>do I need aditional packages?
No.

Commented:
hi there,

  just use the following function function ...

BOOL SendMailThroughSMTP(LPCTSTR szServer,LPCTSTR szFrom,LPCTSTR szTo,LPCTSTR szSubject,LPCTSTR szMessage)
{
     SOCKET                    sock;
     struct in_addr          ptr;
     struct sockaddr_in     serv_addr;
    CString                    strCommand;
    CString                    strResponse;
     char                    buffer[500];
     
     sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

     if(sock==INVALID_SOCKET)
          return FALSE;

     ptr.S_un.S_addr= inet_addr(szServer);
     memset((void *)&serv_addr, 0, sizeof(serv_addr));
     serv_addr.sin_addr =ptr ;
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_port = htons(25);

     if( connect(sock, (sockaddr*)&serv_addr, sizeof(serv_addr)) < 0 )
          return FALSE;

    // Read the "HELO" response from the server
     memset((void *)buffer,0,500);
     if(recv(sock,(char *)buffer,500,0)<0)
          return FALSE;
    if(strstr(buffer,"220")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);
          return FALSE;
     }

     // use the following lines if ur smtp requires it
/*
    if(send(sock,"HELO which smtp ur using\r\n",18,0)<0)
          return FALSE;
     memset((void *)buffer,0,500);
     if(recv(sock,(char *)buffer,500,0)<0)
          return FALSE;
    if(strstr(buffer,"250")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);
          return FALSE;
     }
*/
     // Send the "FROM" line and check the response
    strCommand = "MAIL FROM:<";
    strCommand += szFrom;
    strCommand += ">\r\n";
   
     if(send(sock,(char *)((LPCSTR)strCommand),strCommand.GetLength(),0)<0)
          return FALSE;
     memset((void *)buffer,0,500);
     if(recv(sock,(char *)buffer,500,0)<0)
          return FALSE;
    if(strstr(buffer,"250")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);
          return FALSE;
     }

    if(strstr(buffer,"250")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);
          return FALSE;
     }

    // Send the "RCPT" line and check the response
    strCommand = "RCPT TO:<";
    strCommand += szTo;
    strCommand += ">\r\n";
   
     if(send(sock,(char *)((LPCSTR)strCommand),strCommand.GetLength(),0)<0)
          return FALSE;
     memset((void *)buffer,0,500);
     if(recv(sock,(char *)buffer,500,0)<0)
          return FALSE;
    if(strstr(buffer,"250")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);
          return FALSE;
     }

    // Send the "DATA" line and check the response
    if(send(sock,"DATA\r\n",6,0)<0)
          return FALSE;
     memset((void *)buffer,0,500);
     if(recv(sock,(char *)buffer,500,0)<0)
          return FALSE;
    if(strstr(buffer,"354")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);  
          return FALSE;
     }

    // Send the "Subject" line
    strCommand = "Subject: ";
    strCommand += szSubject;
    strCommand += "\r\n";

    if(send(sock,strCommand,strCommand.GetLength(),0)<0)
          return FALSE;
    // Send actual Message
     if(send(sock,szMessage,strlen(szMessage),0)<0)
         return FALSE;

    // Send the termination line and check the response
     if(send(sock,"\r\n.\r\n",5,0)<0)
         return FALSE;
     memset((void *)buffer,0,500);
     if(recv(sock,(char *)buffer,500,0)<0)
          return FALSE;
    if(strstr(buffer,"250")==NULL)
     {
        send(sock,"QUIT\r\n",6,0);  
          return FALSE;
     }

    // Send the "QUIT" line
     if(send(sock,"QUIT\r\n",6,0)<0)
         return FALSE;
   
    return TRUE;
}

but u sholud know the rfc standards of smtp and the basics of socket...

regards,
himmya.

Author

Commented:
sorry for the delay, note to the last poster, your code doesn't really work, but thanks for trying, alex, you answered all my questions, thank you very much.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.