Ask the Expert

The easiest way to send e-mails from VS.NET

I'm trying to use a Visual Studio .NET 2003 Web application to send e-mails to addresses I get out of my database. What is the simplest way send e-mails like these in Visual Studio? If possible, I would prefer not installing any additional software.
Sending Email from any .NET application is fairly straight-forward and uses behavior built into the System.Web Assembly.

You need to create a new instance of the SystemWeb.Mail.MailMessage object and set the From, To, Subject and Body properties. This message can then be sent through an SMTP server by setting the SmtpServer property of the System.Web.Mail.SmtpMail object and invoking the Send method.

The following code shows you how to set the properties and invoke the methods. It even shows you how to add an attachment:

''' delcare a new mail message
Dim aMailMessage As New System.Web.Mail.MailMessage

''' set the from address
aMailMessage.From = "me@somewhere.com"

''' set the to address and subject
aMailMessage.To = "smatzen@slm.com"
aMailMessage.Subject = "This is a test email message."

''' send in Text format
aMailMessage.BodyFormat = System.Web.Mail.MailFormat.Text

''' set the body text (Remember you need to keep your paragraphs to under 1000 characters)
aMailMessage.Body = "This is the text of the email message."

''' add an attachment if you want to
aMailMessage.Attachments.Add(New System.Web.Mail.MailAttachment("C:Test.txt", System.Web.Mail.MailEncoding.Base64))

''' send the email
System.Web.Mail.SmtpMail.SmtpServer = "smtp.sbcglobal.net"
System.Web.Mail.SmtpMail.Send(aMailMessage)

This was first published in July 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: