How to send e-mail programmatically with System.Web.Mail and Visual Basic .NET

This article was previously published under Q314201
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
This article demonstrates how to use System.Web.Mail to send an e-mail message in Visual Basic .NET.
MORE INFORMATION
  1. Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Click Visual Basic Projects, click the Console Application template, and then click OK. By default, Module1.vb is created.
  2. Add a reference to System.Web.dll. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the .NET tab, locate System.Web.dll, and then click Select.
    3. Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries you selected, click Yes.
  3. In the code window, replace the whole code with:
    Imports System.Web.MailModule Module1    Sub Main()        Dim oMsg As MailMessage = New MailMessage()        ' TODO: Replace with sender e-mail address.        oMsg.From = "sender@somewhere.com"        ' TODO: Replace with recipient e-mail address.        oMsg.To = "recipient@somewhere.com"        oMsg.Subject = "Send using Web Mail"        ' SEND IN HTML FORMAT (comment this line to send plain text).        oMsg.BodyFormat = MailFormat.Html        'HTML Body (remove HTML tags for plain text).        oMsg.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>"        ' ADD AN ATTACHMENT.        ' TODO: Replace with path to attachment.        Dim sFile As String = "C:\temp\Hello.txt"        Dim oAttch As MailAttachment = New MailAttachment(sFile, MailEncoding.Base64)        oMsg.Attachments.Add(oAttch)        ' TODO: Replace with the name of your remote SMTP server.        SmtpMail.SmtpServer = "MySMTPServer"        SmtpMail.Send(oMsg)        oMsg = Nothing        oAttch = Nothing    End SubEnd Module					
  4. Modify code where you see "TODO".
  5. Press F5 to build and run the program.
  6. Verify that the e-mail message has been sent and received.
Properties

Article ID: 314201 - Last Review: 12/07/2015 08:20:21 - Revision: 2.2

Microsoft Visual Basic .NET 2003 Standard Edition, Microsoft Visual Basic .NET 2002 Standard Edition

  • kbnosurvey kbarchive kbhowto KB314201
Feedback