troubleshooting Question

VB6: Send emails programmatically

Avatar of LovinSpoonful
LovinSpoonful asked on
Visual Basic Classic
8 Comments1 Solution931 ViewsLast Modified:
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

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros