Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Internet Web Mail Stuff Code Examples

Send an email using Outlook

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
Send an email using Outlook The following code demonstrates how to send an email using Outlook: Option Explicit 'Purpose : Sends a text email using Outlook 'Inputs : sTo A semi collan seperate list of email addresses to send the mail to. ' sSubject The subject of the email. ' sBody The body of the email. ' [sAttachments] If specified is a semi collan deliminated list of the files to attach. ' eg. "C:\autoexec.bat;C:\config.sys" ' [sAttachmentNames] If specified is a semi collan deliminated list of the display names for ' the files in the "sAttachments" variable. eg "Auto Exec File;Config Sys File" ' [bPreviewMail] If True the mail is shown to the user before they choose whether to send it or not. 'Outputs : Returns True on success Function OutlookSendMail(sTo As String, sSubject As String, sBody As String, Optional sAttachments As String = "", Optional sAttachmentNames As String = "", Optional bPreviewMail As Boolean = False) As Boolean Dim oOutlookApp As Object 'Outlook.Application Dim olMail As Object 'Outlook.MailItem Dim oInbox As Object Dim asAttachments() As String, asAttachmentNames() As String Dim lThisAttachment As Long, lLastAttachName As Long Dim asRecipients() As String, lThisRecip As Long On Error Resume Next 'Get a reference to an open copy of outlook Set oOutlookApp = GetObject(, "Outlook.Application") If oOutlookApp Is Nothing = True Then 'Outlook is not open, create a new outlook Set oOutlookApp = CreateObject("Outlook.Application") End If On Error GoTo ErrFailed If (oOutlookApp Is Nothing) = False Then 'Create a new mail item Set oInbox = oOutlookApp.Session.GetDefaultFolder(6) 'olFolderInbox Set olMail = oOutlookApp.CreateItem(0) 'olMailItem 'Set the mail fields olMail.Subject = sSubject 'Add the list of recipients asRecipients = Split(sTo, ";") For lThisRecip = 0 To UBound(asRecipients) olMail.Recipients.Add Trim$(asRecipients(lThisRecip)) Next olMail.Body = sBody If Len(sAttachments) > 0 Then 'Add attachments On Error Resume Next asAttachments = Split(sAttachments, ";") If Len(sAttachmentNames) Then asAttachmentNames = Split(sAttachmentNames, ";") lLastAttachName = UBound(asAttachmentNames) Else lLastAttachName = -1 End If For lThisAttachment = 0 To UBound(asAttachments) 'Check the attachment exists asAttachments(lThisAttachment) = Trim$(asAttachments(lThisAttachment)) If Len(Dir$(asAttachments(lThisAttachment))) > 0 Then 'Attachment exists, add it With olMail.Attachments.Add(asAttachments(lThisAttachment), 1) 'Where 1 = olByValue (Embed attachment in the item) If lThisAttachment <= lLastAttachName Then .DisplayName = asAttachmentNames(lThisAttachment) End If End With End If Next On Error GoTo ErrFailed End If If bPreviewMail Then 'Show the mail to the user and let them choose to send it olMail.Display vbModal Else 'Send Mail olMail.Send End If 'Clear object pointers Set olMail = Nothing Set oInbox = Nothing Set oOutlookApp = Nothing OutlookSendMail = True Else 'Failed to create an outlook OutlookSendMail = False End If Exit Function ErrFailed: 'Failed to send mail Debug.Print "Error in OutlookSendMail: " & Err.Description If (olMail Is Nothing) = False Then olMail.Delete End If Set olMail = Nothing Set oOutlookApp = Nothing OutlookSendMail = False End Function 'Demonstration routine Sub Test() If OutlookSendMail("[email protected];[email protected]", "Test Subject", "Test Body", "C:\clsReportEngine.cls", "Report Class", True) = True Then MsgBox "Sent email!", vbInformation Else MsgBox "Failed to send email!", vbInformation End If End Sub