Solved

Sending mail using VC++ 5.0

Posted on 1998-12-15
17
313 Views
Last Modified: 2010-05-18
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
Comment
Question by:Nuno Alves
  • 7
  • 4
  • 2
  • +3
17 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1180221
the easient way to do this is issueing a
'ShellExecute ( NULL, NULL, "mailto:<email address>", NULL, NULL, SW_SHOWNORMAL);'
0
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180222
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
 
LVL 6

Expert Comment

by:WxW
ID: 1180223
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
 

Expert Comment

by:hazman
ID: 1180224
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
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180225
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
 

Expert Comment

by:hazman
ID: 1180226
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
 
LVL 6

Expert Comment

by:WxW
ID: 1180227
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
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180228
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1180229
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
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180230
Adjusted points to 150
0
 
LVL 1

Accepted Solution

by:
The_Brain earned 150 total points
ID: 1180231
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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180232
#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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180233
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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180234
don't you ever check the answers?
0
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180235
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
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180236
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
 
LVL 1

Author Comment

by:Nuno Alves
ID: 1180237
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now