I have seen an example of sending an email using CDO, MAPI and Outlook in the March number of "Visual Basic - Programmers Journal". Does any on have this source code, or an other example of sending emails using these options?
All of these example require outlook. Better to send using SMTP and winsock.
MAPI, by Arron Young
Code:
Private Sub cmdSendMail_Click()
' Sign On using the Default MAPI Account
MAPISession1.SignOn
With MAPIMessages1
' Use the session we just started
.SessionID = MAPISession1.SessionID
' Start Composing a New Email
.Compose
' Who are we sending the email to?
.RecipAddress = "[email protected]"
' What's the Subject Line of the Email?
.MsgSubject = "MAPI SendMail Example"
' What's the content of the Message body?
.MsgNoteText = _
"This is an example of how To send emails with VB via the " & vbCrLf & _
"MAPI Mail Controls." & vbCrLf & vbCrLf & _
"This example also includes Multiple Attachments..." & vbCrLf & vbCrLf & _
"- Aaron Young."
' Add the First attachment:
' First, Set the Attachment Index To the Next available Index Number
.AttachmentIndex = .AttachmentCount
' Set the Name To display with the Attachment (usually the Filename)
.AttachmentName = "SomeFile1.txt"
' Set the Path (Locally) To the File To attach
.AttachmentPathName = "C:\SomeFile1.txt"
' Set the Type of Attachment (Data In most cases)
.AttachmentType = mapData
' Repeat the process To add Multiple attachments:
.AttachmentIndex = .AttachmentCount
.AttachmentName = "SomeFile2.txt"
.AttachmentPathName = "C:\SomeFile2.txt"
.AttachmentType = mapData
.AttachmentIndex = .AttachmentCount
.AttachmentName = "SomeFile3.txt"
.AttachmentPathName = "C:\SomeFile3.txt"
.AttachmentType = mapData
' Finally, Send the Email:
.Send
End With
' Close the MAPI Session.
MAPISession1.SignOff
End Sub
Outlook:
Code:
Private Sub Command1_Click()
Dim objOutlook As New outlook.Application
Dim objOutlookMsg As outlook.MailItem
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = "[email protected]"
.Subject = "Testing..."
.Body = "Testing email example"
.Attachments.Add "C:\Winzip.txt", olByValue, 1, "Hello"
.Send
End With
Set objOutlookMsg = Nothing
End Sub
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1
Const email = "[email protected]"
Private Sub Form_Load()
ShellExecute 0&, vbNullString, "mailto:" & email, vbNullString, "C:\", SW_SHOWNORMAL
Unload Me
End Sub
hi i have used exactly that code for outlook and went into reference and could only see microsoft outlook 10.0 object libary so i ticked this and when it trys and sends it says something is being sent do you want to allow this and you press yes and then you check in outlook outbbox and its there but hasnt been sent. why is this?
What if you don't have Outlook. I use Outlook Express which I can't see any reference to.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
This little program does just as it says... it sends emails without the need for Outlook and any extra components...
All that is required is winsock...
If my post has been helpful, then please rate it accordingly...
If it has solved your question(s), then don't forget to mark the thread as "[Resolved]"... thank you.