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(
""
,
""
,
""
,
""
,
"Sample Subject"
,
"Sample body of text for mail message"
)
MailHelper.cs
using
System.Net.Mail;
public
class
MailHelper
{
///
///
Sends an mail message
///
///
Sender address
///
Recepient address
///
Bcc recepient
///
Cc recepient
///
Subject of mail message
///
Body of mail message
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
'''
''' Sends an mail message
'''
''' Sender address
''' Recepient address
''' Bcc recepient
''' Cc recepient
''' Subject of mail message
''' Body of mail message
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
=
"
"
>
<
network
host
=
"
smtp.yourdomain.com
"
port
=
"
25
"
userName
=
"
yourUserName
"
password
=
"
yourPassword
"
/>
smtp
>
mailSettings
>
system.net
>
configuration
>
Share this post:
|
|
|
|
|