views:

383

answers:

3

I need to send e-mails from my application. But, instead of sending the e-mails directly from through SMTP, I'd like to send it through Microsoft Outlook. So...

How to Generate an E-mail Message in Microsoft Outlook Outbox in VB6?

+2  A: 

This support page from Microsft has examples.

Send a message to your new contact.
   Dim olMail As Outlook.MailItem
   Set olMail = olApp.CreateItem(olMailItem)
 ' Fill out & send message...
   olMail.To = olItem.Email1Address
   olMail.Subject = "About our meeting..."
   olMail.Body = _
        "Dear " & olItem.FirstName & ", " & vbCr & vbCr & vbTab & _
        "I'll see you in 2 minutes for our meeting!" & vbCr & vbCr & _
        "Btw: I've added you to my contact list."
   olMail.Send
Shoban
+2  A: 

Use COM automation. This KnowledgeBase article explains how to do it from VBA, it's exactly the same in VB6. The References command is on the Project menu in VB6 not the Tools menu, I think that's the only difference. EDIT: This KnowledgeBase article explains how to do it in VB6. Kudos to Shoban for this, upvote his answer not mine!

I think the MSDN topic Automating Outlook from a Visual Basic applications is also worth mentioning just for Microsoft's typo in the title!

MarkJ
A: 

Add a project reference to the Microsoft Outlook X Object Library

Then, in a form, module, class, or whatever ... I chose a button_click event.

Private Sub Command1_Click()
  Dim objOutlook As Outlook.Application
  Dim objMail As MailItem

  Dim strToAddress As String
  Dim strSubject As String
  Dim strBody As String

  Set objOutlook = New Outlook.Application
  Set objMail = Outlook.CreateItem(olMailItem)

  strToAddress = "[email protected]"
  strSubject = "VB6 test email"
  strBody = "This is a test email sent from VB6"

  With objMail
    .To = strToAddress
    .Subject = strSubject
    .BodyFormat = olFormatPlain
    .Body = strBody
  End With

  MsgBox "outlook security will now complain as I try to resolve your email addresses against your address book"
  objMail.Recipients.ResolveAll

  objMail.Save
  Set objMail = Nothing
  Set objOutlook = Nothing

  MsgBox "look in your drafts folder for the new email"

  'Thank you, I'm here all week, try the Veal. Good night.
End Sub
cometbill