Creating and sending HTML-formatted messages
(Part 1)
Sending simple HTML e-mail
Summary: Demonstrates sending
simple HTML e-mail. Focuses on connecting to the SMTP server and sending e-mail
rather than message composing.
Tutorial map:
Part 1 - Sending simple HTML e-mail
Part 2 - Importing HTML file into message body
Part 3 - Assembling body text from different
sources
Part 4 - Building alternative message body
Part 5 - Advanced topics
To send an e-mail, we need to create MailBee.SMTP
object first.
Once SMTP object is created and valid LicenseKey
is assigned, next step is to set the properties of the e-mail itself, and specify
SMTP server which will actually deliver our e-mail. We'll set the following properties
of the e-mail itself:
Code example:
Dim Mailer ' Using Visual Basic to create object Set Mailer = CreateObject("MailBee.SMTP") ' Using ASP to create object ' Set Mailer = Server.CreateObject("MailBee.SMTP") ' Unlock MailBee.SMTP object Mailer.LicenseKey = "put your license key here" ' SMTP server name Mailer.ServerName = "mail.server.com" ' Recipients Mailer.Message.ToAddr = "you@yourdomain.com" ' Sender Mailer.Message.FromAddr = "me@mydomain.com" ' Subject Mailer.Message.Subject = "Hello" ' HTML body text Mailer.Message.BodyText = "<html>Hello</html>" ' Mark that body has HTML format Mailer.Message.BodyFormat = 1 ' Send it! Mailer.Send ' Close the SMTP session Mailer.Disconnect
E-mails are not sent: SMTP authentication
required
Sometimes e-mails are not sent if only the SMTP server name is specified. This
occurs if the SMTP server's administrator restricted access to the server by
any means. For example, e-mails may be delivered to local addresses only or
may not be delivered at all.
As a rule, SMTP servers give unrestricted access to authenticated users only.
To authenticate on SMTP server, just set AuthMethod,
UserName and Password
properties of SMTP object prior to calling Send method.
Values of UserName and Password properties are specific to each
user, usually they are the same as the user account name and the password of
the user's POP3 account on the same mail server.
AuthMethod property denotes type of the procedure to be used to authenticate
user on the SMTP server. Value of 2 (LOGIN authentication) is
supported by most SMTP servers. MSN servers require to use value of 5
(MSN authentication).
E-mails are still not sent. Any ideas?
In rare cases, e-mails are not being sent even if SMTP authentication is used.
Typical reasons: the SMTP server is down, it rejects your IP-address, size of
the e-mail you are trying to send is out of quota, some of the recipients are
invalid or not allowed, etc.
To find-out what is the problem's source, you can analyze MailBee log file or
the values of ErrCode and ServerResponse
properties.
To force MailBee to create log file, set EnableLogging
and LogFilePath properties.
Also, you can add ClearLog method
call to clear log file on each run.
Sample code description
The sample below sends the same e-mail as in the previous sample, but connects
to the SMTP server using SMTP authentication, logs SMTP session into a file,
and notifies user about errors.
Code example:
Dim Mailer 'Using Visual Basic to create object Set Mailer = CreateObject("MailBee.SMTP") ' Using ASP to create object ' Set Mailer = Server.CreateObject("MailBee.SMTP") ' ' Also, in ASP use Response.Write instead of MsgBox ' Enable logging and clear log file Mailer.LogFilePath = "C:\Temp\smtp_log.txt" Mailer.EnableLogging = True Mailer.ClearLog ' Unlock MailBee.SMTP object Mailer.LicenseKey = "put your license key here" ' SMTP server name Mailer.ServerName = "mail.server.com" ' Set SMTP Authentication properties Mailer.AuthMethod = 2 Mailer.Username = "username" Mailer.Password = "password" ' Set message properties Mailer.Message.ToAddr = "you@yourdomain.com" Mailer.Message.FromAddr = "me@mydomain.com" Mailer.Message.Subject = "Hello" Mailer.Message.BodyText = "<html>Hello</html>" Mailer.Message.BodyFormat = 1 ' Try to send the e-mail If Mailer.Send Then ' Succeeded ;-) MsgBox "Sent successfully" Else ' Error :-( MsgBox "Error #" & Mailer.ErrCode & _ ", Server responded:" & Mailer.ServerResponse End If ' Close the SMTP session Mailer.Disconnect
See Also:
Part 2 (Importing
HTML file into message body)
Send Method
Copyright © 2002-2024, AfterLogic Corporation. All rights reserved.