Advanced e-mail

Visual Basic Code | ASP Code

The sample sends HTML e-mail with alternative plain-text version (for HTML-incapable readers), attachments and custom headers to several recipients.

HTML part of the e-mail is constructed from 3 parts. The header and the footer are exported from HTML files (with their embedded objects, if any). Main HTML content is manually generated.

Alternative plain-text version of the HTML content is generated by SMTP.Message.MakeAltBody method.

Additionally, regular (not inline) attachment is added to the e-mail.

The e-mail has several recipients of various types (To, CC, BCC), different Reply-To and From headers, friendly names and comments in some of e-mail addresses.

Full syntax (e.g. SMTP.Message.ToAddr instead of SMTP.ToAddr shortcut) is used to set properties of the message to be sent by SMTP object. Using full syntax allows accessing all methods and properties of the Message object that SMTP component sends.

If required, SMTP authentication can be enabled by uncommenting corresponding lines. Logging is enabled by default, error-checking is performed.

[Visual Basic]:

Dim objSMTP

' Create mailer component
Set objSMTP = CreateObject("MailBee.SMTP")

' Enable logging SMTP session into a file
objSMTP.EnableLogging = True
objSMTP.LogFilePath = "C:\Temp\smtp_log.txt"
objSMTP.ClearLog

' Unlock SMTP component
objSMTP.LicenseKey = "put your license key here"

' Set SMTP server name
objSMTP.ServerName = "mail.server.com"

' Uncomment the following 3 lines to enable SMTP authentication
' objSMTP.AuthMethod = 2
' objSMTP.UserName = "jdoe"
' objSMTP.Password = "secret"

' Set "From:", "Reply-To:", "To:", "CC:", "BCC:", "Subject:" fields
objSMTP.Message.FromAddr = "John Doe <jdoe@some.com> (Some Company)"
objSMTP.Message.ReplyToAddr = "Foo Company Support Team <support@some.com>"
objSMTP.Message.ToAddr = "Bill Johnes <bill.johnes@domain1.com>, someone@domain2.com (Another company)"
objSMTP.Message.CCAddr = "Management Department <man@some.com> (Some Company), stat@statserver.some.com"
objSMTP.Message.BCCAddr = "Big.Brother@secretservice.com, Mr. X <collect_secrets@competitorcompany.com>"
objSMTP.Message.Subject = "Complex message"

' Set HTML format of the body
objSMTP.Message.BodyFormat = 1

' Construct HTML body: header from file + main part + footer from file

' Export the header into HTML body
objSMTP.Message.ImportBodyText "C:\docs\letter.htm", True

' Append main part to HTML body
objSMTP.Message.BodyText = objSMTP.BodyText & "<p>This is main part</p>"

' Export the footer into HTML body. AppendMode is True,
' so exported content will not overwrite existing body
objSMTP.Message.ImportBodyText "C:\docs\letter.htm", True, True

' HTML body ready, generate plain-text version
objSMTP.Message.MakeAltBody

' Add attachment from the file
objSMTP.Message.AddAttachment "C:\My documents\report.xls"

' Add custom header
objSMTP.Message.AddHeader "X-Special", "Something"

' Try to send message
If objSMTP.Send Then
  MsgBox "Sent successfully"
Else
  MsgBox "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc
End If

[ASP]:

<%
Dim objSMTP

' Create mailer component
Set objSMTP = Server.CreateObject("MailBee.SMTP")

' Enable logging SMTP session into a file
objSMTP.EnableLogging = True
objSMTP.LogFilePath = "C:\Temp\smtp_log.txt"
objSMTP.ClearLog

' Unlock SMTP component
objSMTP.LicenseKey = "put your license key here"

' Set SMTP server name
objSMTP.ServerName = "mail.server.com"

' Uncomment the following 3 lines to enable SMTP authentication
' objSMTP.AuthMethod = 2
' objSMTP.UserName = "jdoe"
' objSMTP.Password = "secret"

' Set "From:", "Reply-To:", "To:", "CC:", "BCC:", "Subject:" fields
objSMTP.Message.FromAddr = "John Doe <jdoe@some.com> (Some Company)"
objSMTP.Message.ReplyToAddr = "Foo Company Support Team <support@some.com>"
objSMTP.Message.ToAddr = "Bill Johnes <bill.johnes@domain1.com>, someone@domain2.com (Another company)"
objSMTP.Message.CCAddr = "Management Department <man@some.com> (Some Company), stat@statserver.some.com"
objSMTP.Message.BCCAddr = "Big.Brother@secretservice.com, Mr. X <collect_secrets@competitorcompany.com>"
objSMTP.Message.Subject = "Complex message"

' Set HTML format of the body
objSMTP.Message.BodyFormat = 1

' Construct HTML body: header from file + main part + footer from file

' Export the header into HTML body
objSMTP.Message.ImportBodyText "C:\docs\letter.htm", True

' Append main part to HTML body
objSMTP.Message.BodyText = objSMTP.BodyText & "<p>This is main part</p>"

' Export the footer into HTML body. AppendMode is True,
' so exported content will not overwrite existing body
objSMTP.Message.ImportBodyText "C:\docs\letter.htm", True, True

' HTML body ready, generate plain-text version
objSMTP.Message.MakeAltBody

' Add attachment from the file
objSMTP.Message.AddAttachment "C:\My documents\report.xls"

' Add custom header
objSMTP.Message.AddHeader "X-Special", "Something"

' Try to send message
If objSMTP.Send Then
  Response.Write "Sent successfully"
Else
  Response.Write "Error #" & objSMTP.ErrCode & ", " & objSMTP.ErrDesc
End If
%>
See Also:
MakeAltBody Method

 


Send feedback to AfterLogic
Copyright © 2002-2022, AfterLogic Corporation. All rights reserved.