Sending an email using System.Net.Mail
Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
Download C# System.Net.Mail Helper
Download VB.NET System.Net.Mail Helper
Calling the function from code
MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
MailHelper.cs
using System.Net.Mail;
public class MailHelper
{
/// <summary>
/// Sends an mail message
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="to">Recepient address</param>
/// <param name="bcc">Bcc recepient</param>
/// <param name="cc">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mailMessage = new MailMessage();
// Set the sender address of the mail message
mailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if (!string.IsNullOrEmpty(bcc))
{
// Set the Bcc address of the mail message
mailMessage.Bcc.Add(new MailAddress(bcc));
}
// Check if the cc value is null or an empty value
if (!string.IsNullOrEmpty(cc))
{
// Set the CC address of the mail message
mailMessage.CC.Add(new MailAddress(cc));
}
// Set the subject of the mail message
mailMessage.Subject = subject;
// Set the body of the mail message
mailMessage.Body = body;
// Set the format of the mail message body as HTML
mailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient smtpClient = new SmtpClient();
// Send the mail message
smtpClient.Send(mailMessage);
}
}
MailHelper.vb
Imports System.Net.Mail
Public Class MailHelper
''' <summary>
''' Sends an mail message
''' </summary>
''' <param name="from">Sender address</param>
''' <param name="recepient">Recepient address</param>
''' <param name="bcc">Bcc recepient</param>
''' <param name="cc">Cc recepient</param>
''' <param name="subject">Subject of mail message</param>
''' <param name="body">Body of mail message</param>
Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
' Instantiate a new instance of MailMessage
Dim mailMessage As New MailMessage()
' Set the sender address of the mail message
mailMessage.From = New MailAddress(from)
' Set the recepient address of the mail message
mailMessage.To.Add(New MailAddress(recepient))
' Check if the bcc value is nothing or an empty string
If Not String.IsNullOrEmpty(bcc) Then
' Set the Bcc address of the mail message
mailMessage.Bcc.Add(New MailAddress(bcc))
End If
' Check if the cc value is nothing or an empty value
If Not String.IsNullOrEmpty(cc) Then
' Set the CC address of the mail message
mailMessage.CC.Add(New MailAddress(cc))
End If
' Set the subject of the mail message
mailMessage.Subject = subject
' Set the body of the mail message
mailMessage.Body = body
' Set the format of the mail message body as HTML
mailMessage.IsBodyHtml = True
' Set the priority of the mail message to normal
mailMessage.Priority = MailPriority.Normal
' Instantiate a new instance of SmtpClient
Dim smtpClient As New SmtpClient()
' Send the mail message
smtpClient.Send(mailMessage)
End Sub
End Class
Web.config
<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="defaultEmail@yourdomain.com">
<network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Similar Posts
- VB.NET GridView Sorting/Paging w/o a DataSourceControl DataSource
- Accessing Config File Mail Settings Programmatically
- C# GridView Sorting/Paging w/o a DataSourceControl DataSource





Comments
Dom on on 3.19.2006 at 5:54 AM
I just wish MS would support VB fans as much as they do c#. On the MailMessage class, there is no code sample in VB. I am pleased you provided one!