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 = "email@example.com" ''' set the to address and subject aMailMessage.To = "firstname.lastname@example.org" 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