Link to home
Start Free TrialLog in
Avatar of LovinSpoonful
LovinSpoonful

asked on

VB6: Send emails programmatically

Hello everyone.  I need a short, simple way to send an email inside my VB program.  

I have the component Microsoft MAPI Controls 6.0 checked.  Also, I am referencing Microsoft CDO for Windows 2000 Library.

I've spent an hour reading all the emails about how to send email from VB, using MAPI, CDO, and the free project: http://www.freevbcode.com/ShowCode.Asp?ID=109.
I've tried several examples and none work, and i do not want to use the freevbcode project because it is too big.  I do not have access to MSDN.

Does anyone have a short project (less than 100 lines) that will send an email from VB?  It can use outlook, but not some other email program.
please, do not reply if you cannot provide a complete example that can be put into a blank project (with a form), that will work.  thank you very much!

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  

I would really like to use this one because it's so compact, but am open to other solutions.  

'Reference CDO 1.21 Library in your project(if u do not have
'CDO dll, download from msft)
'use CDO to send email
Public Sub SendEmail()

   Dim objSession As MAPI.Session            <-------------- I GET ERROR: 'USER DEFINED TYPE NOT DEFINED'   WHAT DO I NEED TO DO?
   Dim objMessage As Message
   Dim objRecipient As Recipient
   Dim objAttachment As Attachment
   
   If objSession Is Nothing Then
       Set objSession = CreateObject("MAPI.SESSION")
   End If
   
   objSession.Logon profilename:="MS Exchange Settings"
   
   Set objMessage = objSession.Outbox.Messages.Add
   objMessage.Subject = "subject"
   objMessage.Text = "message body"
   
   Set objRecipient = objMessage.Recipients.Add
   objRecipient.Name = "e-mail address"
   objRecipient.Type = mapiTo
   objRecipient.Resolve
   
   Set objAttachment = objMessage.Attachments.Add
   With objAttachment
        .Type = CdoFileData
        .Position = 0
        .Name = "display name"
        .Source = "C:\file"
   End With
   objMessage.Update
   
   objMessage.Send showdialog:=False
   objSession.Logoff
   Set objSession = Nothing

End Sub

ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LovinSpoonful
LovinSpoonful

ASKER

I guess I don't have CDO 1.21.  (only have  Microsoft CDO for Windows 2000 Library)   Where can I find it?  
I had a lot of problems getting that on my machine at home as well, when I was interfacing with emails last year.  I ended up putting in my Office install CD, and installing it from there (it is an optional installation file).
If you can't do that, let me know and I'll see what I can find.

After searching quickly at google, it looks like you can get a copy from a link at
http://www.advansyscorp.com/faq/20000823t171038.htm
They link to an internal file of theirs that has it, as well as a MS link a little bit down the page.  You might also be able to find something at www.cdolive.com
If all else fails, you could just search google for "CDO 1.21".  Note that the links above are just from a simple search, I haven't tested the download or anything.
u can downlaod dlls from here

http://www.good.com/faq/14065.html

and u can get lots of information regarding CDO from here

http://support.microsoft.com/ph/6384/en-us/?aid=1
LovinSpoonful,

According to EE guidelines we are not really suppose to provide full code rather help you understand how to proceed and to learn. In the past I had to develop something similar and had problems with the MAPI controls.

In the end I wrote my own wrapper class to handle the SMTP connections myself, for basic email its relatively straightforward. You will need the winsock control, open a connection to email server on the standard port and then just maintain state between client and server. The advantage is that you are not relying upon external factors like mapi, mail exchange, office, outlook rather talking with the mail server directly which means you have more control and detailed logging. You will need to be well versed in the RFC for SMTP (simple mail transfer protocol).

regards.
thanks for the more compact code!
Glad to help! I take it you got CDO installed on your computer :)
Matt