Send an email from a webpage with asp.net

July 15, 2010 - 15:06

Emailing with .net is fairly simple. You will also need to know an SMTP mailserver which you have permission to relay from, otherwise you will recieve an error 550.

<% @Import Namespace="System.Web.Mail" %>

SmtpMail.SmtpServer = "smtp.myisp.com"
Dim OrderMail as new MailMessage
OrderMail.To = "test@test.com"
OrderMail.From = "noreply@simplevb.net"
OrderMail.Subject = "Subject here"
OrderMail.BodyFormat = MailFormat.Html
OrderMail.Body = "message here"
SmtpMail.Send(OrderMail)


Firstly the smtp server is specified the rest is self explanatory, however you will need to have valid email addresses for this to run without any errors. What i usually do is put a try/catch clause around the SmtpMail.Send as usually if the from address is taken from a webform, most users won't enter a valid email address.



© 2011 simplevb.net