Sending email using ASP.NET

The following code samples show the code you need to use in order to send email through ASP.NET using the System.Net.Mail namespace:

Using C#

Note that for the code below to work, you need to add using System.Net.Mail; to the list of ‘using’ directives at the top of your code file.

MailMessage mail = MailMessage();

mail.From = new MailAddress("username@greenwich.ac.uk");

mail.To.Add("username@greenwich.ac.uk");

mail.Body = "Your message here";

mail.Subject = "Your subject here";

SmtpClient client = new SmtpClient();

client.Host = "smtp.gre.ac.uk";

client.Send(mail);

Using VB

Note that for the code below to work, you need to add Imports System.Net.Mail to the list of ‘Imports’ directives at the top of your code file.

Dim mail As MailMessage

mail = New MailMessage

mail.From = New MailAddress("username@greenwich.ac.uk")

mail.To.Add("username@greenwich.ac.uk")

mail.Body = "Your message here"

mail.Subject = "Your subject here"

Dim client As New Mail.SmtpClient

client.Host = "smtp.gre.ac.uk"

client.Send(mail)

Comments are closed.