• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Sending mail using VC++ 5.0

I'm building an application using Visual C++ 5.0
I need my application to send an email using the computer
default email client. How can i do that?
Thanks in advanced.
0
Nuno Alves
Asked:
Nuno Alves
  • 7
  • 4
  • 2
  • +3
1 Solution
 
jkrCommented:
the easient way to do this is issueing a
'ShellExecute ( NULL, NULL, "mailto:<email address>", NULL, NULL, SW_SHOWNORMAL);'
0
 
Nuno AlvesAuthor Commented:
Thank you for your answer jkr's.
But how do i build the mail?
What i want besides open the mail client is to pass the mail text.
The user just have to check the mail text and send it.
0
 
WxWCommented:
With the mailto: internet shortcut , you can only pass the receipient and the subject of the mail . I don't know if there is a way to pass the text using OLE , but that would be hard . The best is to try to send the mail from your own app .
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hazmanCommented:
You could also do this the 'manual' way.

To do this, firstly you'll have to understand the SMTP protocol (for sending mail) and POP3 protocol (if you'd like to receive mail). These protocols define how your SMTP server and your mail client program will 'converse' throughout the e-mail sending session.

When you connect to the SMTP server, the following dialog takes place between your client and the server. The lines beginning with C, you have to send (we'll discuss this later):

S: 220 MAILSERVER.COM Simple Mail Transfer Service Ready
C: HELO MAILSERVER.COM
S: 220 MAILSERVER.COM
C: MAIL FROM:<sender@hotmail.com>
S: 250 OK
C: RCPT TO:<receiver@rocketmail.com>
S: 250 OK
C: DATA
S: 354 Start mail input; end with <CRLF>.<CRLF>
C: Hello, this is my mail's first line
C: Hello, this is my mail's second line
C: .
S: 250 OK
C: QUIT
S: 250 MAILSERVER.COM closing transmission channel

In the example above, we assume that the smtp server has the IP Address MAILSERVER.COM.

Upon connecting to the server, your client has to greet it with HELO. Then, using the MAIL FROM command, you identify the sender. Using RCPT TO, you tell the server the intended recipient.

Now, your client sends DATA to notify the server that we're ready to send the actual message. Continue by sending each message line. When you're done, send a <CRLF>.<CRLF> (in C++, just send "\r\n.\r\n".

Finally, your client closes the session by sending QUIT.

Now, as for sending and receiving the data to/from the server, you can use the Windows socket classes offered by MFC. I'm sorry but this is as far as I can help you because it would be too detailed.

Hazman
www.hazman.com


0
 
Nuno AlvesAuthor Commented:
Thank you for your answer hazman.
But the problem is that the user must see the mail before it will be sent.
In that way, it will be automatic.
And this application is to be used for several users, so it must used their own mail account.
0
 
hazmanCommented:
Hi nmrca, if the user has to see the mail before pressing 'send', it's not a problem. Your client simply displays the mail text and send the same text to the SMTP server. As for configuring for multiple users, well, somehow the client must be able keep the multiple configurations, perhaps using the registry. Of course, the drawback with this method, compared to using MAPI, is you have to deal with the grease and dirt yourself. :)

Cheer, Hazman.
0
 
WxWCommented:
I agree , but you need also to know MIME heards in order for your mail to have subject, priority , etc . Otherwise your mail will be plain text => Only the message text , no subject or other MIME tricks
0
 
Nuno AlvesAuthor Commented:
All the solutions that you all sugest, I think that the most easy is the shellexecute.
I've tried to run it but when I include the shellapi.h, on compilation, several errors appeard
from the shellapi.h, anyone know whats happened?
Thank all for the help.
0
 
MaDdUCKCommented:
How would you open a connection to an SMTP server to allow communication with it.

I would like to be able to issue a command such as 'HELO me' and then to receive the response from the server. How?
0
 
Nuno AlvesAuthor Commented:
Adjusted points to 150
0
 
The_BrainCommented:
Here is a potential solution.  it should send mail as (console app)

#include <windows.h>
#include <iostream.h>
#include <mapi.h>

LPMAPILOGON lpfnMAPILOGON;
LPMAPISENDMAIL lpfnMAPISendMail;
LPMAPILOGOFF lpfnMAPILogoff;  /*you can add MAPIReadMail but this is a send program. */

MapiRecipDesc recipient =
{
   0, MAPI_TO,
   "Geoffrey du Toit", "SMTP:g_dutoit@hotmail.com",
   0, NULL
}; /*You could use variables, But that is for you to do, I am at work at the moment.*/

MapiMessage message =
{
   0, "This Works!",
   "Hello, Geoffrey, if this got to you thanks :) {PLEASE TYPE YOUR E-MAIL ADDRESS HERE}\n",
   NULL, NULL, NULL, 0, NULL, 1, &Recipient, 0, NULL
};

void main() //you can use main(void)
{
  LHANDLE lhSession;
  HANDLE hMAPILib;

  hMAPILib = LoadLibrary("MAPI32.DLL");
  lpfnMAPILogon = (LPMAPILOGON)GetProcAddress(hMAPILib, "MAPILogon); //Logs on the user
  lpfnMAPISendMail = (LPMAPISENDMAIL)GetProcAddress(hMAPILib, "MAPISendMail"); //delivers the mail, to recipient
  lpfnMAPILogoff = (LPMAPILOGOFF)GetProcAddress(hMAPILib, "MAPILogoff");
 
  (*lpfnMAPILogon)(0, NULL, NULL, 0, 0, &lhSession);
  (*lpfnMAPISendMail)(lhSession, 0, &Message, 0, 0);
  (*lpfnMAPILogoff)(lhSession, 0, 0, 0);
  cout<<"Message Send to g_dutoit@hotmail.com";
 
  FreeLibrary(hMAPILib);
}
 


This should work, (the string might be too long, don't know, not checked.  

Thanks goes to Viktor Toth, for his book that gave this code, which made me understand MAPI in the first place.
0
 
The_BrainCommented:
#include <windows.h>
#include <iostream.h>
#include <mapi.h>

LPMAPILOGON lpfnMAPILogon;
LPMAPISENDMAIL lpfnMAPISendMail;
LPMAPILOGOFF lpfnMAPILogoff;  /*you can add MAPIReadMail but this is a send program. */

MapiRecipDesc Recipient =
{
   0, MAPI_TO,
   "Geoffrey du Toit", "SMTP:xy16538@exchange.oldmutual.com",
   0, NULL
}; /*You could use variables, But that is for you to do, I am at work at the moment.*/

MapiMessage Message =
{
   0, "This Works!",
   "Hello, Geoffrey, if this got to you thanks :) {PLEASE TYPE YOUR E-MAIL ADDRESS HERE}\n",
   NULL, NULL, NULL, 0, NULL, 1, &Recipient, 0, NULL
};

void main() //you can use main(void)
{
  LHANDLE lhSession;
  HANDLE hMAPILib;

  hMAPILib = LoadLibrary("MAPI32.DLL");
  lpfnMAPILogon =
        (LPMAPILOGON)GetProcAddress(hMAPILib, "MAPILogon"); //Logs on the user
  lpfnMAPISendMail =
  (LPMAPISENDMAIL)GetProcAddress(hMAPILib, "MAPISendMail"); //delivers the mail, to recipient
  lpfnMAPILogoff =
        (LPMAPILOGOFF)GetProcAddress(hMAPILib, "MAPILogoff");
   
  (*lpfnMAPILogon)(0, NULL, NULL, 0, 0, &lhSession);
  (*lpfnMAPISendMail)(lhSession, 0, &Message, 0, 0);
  (*lpfnMAPILogoff)(lhSession, 0, 0, 0);
  cout<<"Message Send to g_dutoit@hotmail.com";
   
  FreeLibrary(hMAPILib);
}
 

Sorry there were some typos in my previous one.  sorry.

HERE is the fixed version.  I have tested it, but I have not recieved it yet.
0
 
The_BrainCommented:
You can let the user see the mail before it is send.  
You will just have to have a pointer/

CString or
char *TheMessage;

the user inputs to this, his message and you post it back to the screen and then you can send it.  (But I am not going to do it for you, it is easy enough to implement it.

BTW, test it using zd13@pipex.co.za instead, I can't use hotmail for a while. :)

If you wan't me to write a better program then you will have to wait till end Jan I am very busy.  (but I could give you a quick MFC app which would do the trick.. (much like you own little E-mail sender. you can do recieve just read the help on that.  Well good luck, hope this was what you were looking for.

0
 
The_BrainCommented:
don't you ever check the answers?
0
 
Nuno AlvesAuthor Commented:
Thank you very much for your help, The_Brain.
I did not answer before because I did not worked on the 23.
I think that solve my problem.

0
 
Nuno AlvesAuthor Commented:
The Brain, I need your help.
I got an error when I run the code you gave me.
When I do the LoadLibrary("MAPI32.dll") I got the following error
"The procedure entry point WNetEnumCachedPasswords could not be located in the dynamic link library MPR.dll".
What is happened? I have an old version of this dll?
If so, please send me yours, my email nma@uninova.pt
0
 
Nuno AlvesAuthor Commented:
The Brain, I need your help.
I got an error when I run the code you gave me.
When I do the LoadLibrary("MAPI32.dll") I got the following error
"The procedure entry point WNetEnumCachedPasswords could not be located in the dynamic link library MPR.dll".
What is happened? I have an old version of this dll?
If so, please send me yours, my email nma@uninova.pt
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now