Q

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

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close