Microsoft KB Archive/314201

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

Article ID: 314201

Article Last Modified on 7/19/2005

-

APPLIES TO


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

-



This article was previously published under Q314201



SUMMARY
This article demonstrates how to use System.Web.Mail to send an e-mail message in Visual Basic .NET.



MORE INFORMATION
 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. Add a reference to System.Web.dll. To do this, follow these steps:  On the Project menu, click Add Reference. On the .NET tab, locate System.Web.dll, and then click Select. 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.   In the code window, replace the whole code with: Imports System.Web.Mail

Module Module1

Sub Main

Dim oMsg As MailMessage = New MailMessage

' TODO: Replace with sender e-mail address. oMsg.From = &quot;sender@somewhere.com&quot; ' TODO: Replace with recipient e-mail address. oMsg.To = &quot;recipient@somewhere.com&quot; oMsg.Subject = &quot;Send using Web Mail&quot;

' 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 = &quot;<HTML><BODY><B>Hello World!</B></BODY></HTML>&quot;

' ADD AN ATTACHMENT. ' TODO: Replace with path to attachment. Dim sFile As String = &quot;C:\temp\Hello.txt&quot; 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 = &quot;MySMTPServer&quot; SmtpMail.Send(oMsg)

oMsg = Nothing oAttch = Nothing End Sub

End Module </li> Modify code where you see &quot;TODO&quot;.</li> Press F5 to build and run the program.</li> Verify that the e-mail message has been sent and received.</li></ol>

Keywords: kbhowto KB314201

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.