Our Sites:  Tutorial Buzz  |  How To Tree  |  Recipe Voice  |  Golf Twist  |  DIY Click  |  Movie Lizard  |  Halloween Twist  
Search:
Submit Link
Mail to a Friend
RSS FeedReceive updates via our RSS feed

Creating and sending HTML email in Visual Basic - Part 1

by: AfterLogic

Sending Simple HTML E-mail
This tutorial demonstrates sending simple HTML e-mail using AfterLogic's MailBee SMTP component in Visual Basic. This 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:

  • SMTP.Message.FromAddr - "From:" field of the message
  • SMTP.Message.ToAddr - "To:" field of the message
  • SMTP.Message.Subject - "Subject:" field of the message
  • SMTP.Message.BodyText - the message's body text
  • SMTP.Message.BodyFormat - the message's body format (i.e. plain-text or HTML)

    and the following property of the SMTP server:
     
  • SMTP.ServerName - SMTP server name

Once all properties are set, call Send method to perform actual sending. Finally, call Disconnect method to close the SMTP session.

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:\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

About AfterLogic
AfterLogic specializes in email and messaging components. You can visit AfterLogic at http://www.afterlogic.com/. Here you can download evaluations for all their email and messaging components and get support.

© Copyright 2002-2005 Afterlogic, an iForum LLC division

Home  |  News  |  Source Code  |  Tutorials  |  Components  |  Tools  |  Books  |  Free Magazines  |  Jobs  |  Gear  |  Hosting  |  Links
 
Copyright © 2000 - 2006 Code Beach  |    |  Privacy Policy
 
Free thumbnail preview by Thumbshots.org