Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
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;
publicclassMailHelper { ///<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 staticvoidSendMailMessage(stringfrom,stringto,stringbcc,stringcc,stringsubject,stringbody) { // Instantiate a new instance of MailMessage MailMessagemMailMessage =newMailMessage();
// Set the sender address of the mail message mMailMessage.From =newMailAddress(from); // Set the recepient address of the mail message mMailMessage.To.Add(newMailAddress(to));
// Check if the bcc value is null
or an empty string if((bcc !=null) && (bcc !=string.Empty)) { // Set the Bcc address of the mail message mMailMessage.Bcc.Add(newMailAddress(bcc)); } // Check if the cc value is null or an empty value if((cc !=null) && (cc !=string.Empty)) { // Set the CC address of the mail message mMailMessage.CC.Add(newMailAddress(cc)); } // Set the subject of the mail message mMailMessage.Subject = subject; // Set the body of the mail message mMailMessage.Body = body;
// Set the format of the mail message body as HTML mMailMessage.IsBodyHtml =true; // Set the priority of the mail message to normal mMailMessage.Priority =MailPriority.Normal;
// Instantiate a new instance of SmtpClient SmtpClientmSmtpClient =newSmtpClient(); // Send the mail message mSmtpClient.Send(mMailMessage); } }
MailHelper.vb
Imports
System.Net.Mail
Public
ClassMailHelper '''
<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 SharedSubSendMailMessage(ByValfrom
AsString,ByVal
recepientAs
String,
ByValbcc
AsString,ByVal
ccAs
String,
ByValsubject
AsString,ByVal
bodyAs
String) ' Instantiate a new instance of MailMessage DimmMailMessage
AsNewMailMessage()
' Set the sender address of the mail message mMailMessage.From =NewMailAddress(from) ' Set the recepient address of the mail message mMailMessage.To.Add(NewMailAddress(recepient))
' Check if the bcc value is nothing or an empty string IfNot
bccIs
NothingAnd
bcc <>String.EmptyThen ' Set the Bcc address of the mail message mMailMessage.Bcc.Add(NewMailAddress(bcc)) EndIf
' Check if the cc value is nothing or an empty value IfNot
ccIs
NothingAnd
cc <>String.EmptyThen ' Set the CC address of the mail message mMailMessage.CC.Add(NewMailAddress(cc)) EndIf
' Set the subject of the mail message mMailMessage.Subject = subject ' Set the body of the mail message mMailMessage.Body = body
' Set the format of the mail message body as HTML mMailMessage.IsBodyHtml =True ' Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal
' Instantiate a new instance of SmtpClient DimmSmtpClient
AsNewSmtpClient() ' Send the mail message mSmtpClient.Send(mMailMessage) EndSub EndClass
Nice looking code. If you didn't already know, the String class has a new method in 2.0 called IsNullOrEmpty that is pretty handy, and would save a little code for your CC/BCC conditions.
Nice function to send email.
I've a one question related to it:
I want to send thousands emails. Shall I use this function in loop or any optimization is required to given code?
Thanking You.
Regards
Kuldeep Deokule
MCSD.NET
Blog: http://dkuldeep.blogspot.com
This posting is provided "AS IS" with no warranties, and confers no rights.
I want to send thousands emails. Shall I use this function in loop or any optimization is required to given code?
That depends on what thousands is equal to. You might be better off purchasing a commercial componenet to handle that and/or creating a multi-threading Windows service.
Strongtypes, Please I used the mail Helper.Vb that you put out and to be quite honest, I dont know how it works. I copied the code in to a new webpage and I cant see anything, how do I know its working. Please am sorry that I might be sounding a little
dumb but hey!!! just lost. I will appreciate all the help. Most importantly I want the mail to respond onclick a submit button, how do I achieve that. PLEASE
you put the class MailHelper in the App_Code subfolder, create a new file or download the codefile, and in the button's click eventhandler you can call it like StrongTypes explained in his original post.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
I did create a new file where i copied the code and how do i put the class MailHelper in the App_Code Subfolder. Am sorry am just confused. You did mention that I should call the code in the submit button eventhandler, how do I do that when I have the code
in the page. Please help. Thanks
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
HOW TO: Send email using System.Net.Mail
Mar 14, 2006 06:16 AM|LINK
Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
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 mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
} // Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
}
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 mMailMessage As New MailMessage()
' Set the sender address of the mail message
mMailMessage.From = New MailAddress(from)
' Set the recepient address of the mail message
mMailMessage.To.Add(New MailAddress(recepient))
' Check if the bcc value is nothing or an empty string
If Not bcc Is Nothing And bcc <> String.Empty Then
' Set the Bcc address of the mail message
mMailMessage.Bcc.Add(New MailAddress(bcc))
End If
' Check if the cc value is nothing or an empty value
If Not cc Is Nothing And cc <> String.Empty Then
' Set the CC address of the mail message
mMailMessage.CC.Add(New MailAddress(cc))
End If
' Set the subject of the mail message
mMailMessage.Subject = subject
' Set the body of the mail message
mMailMessage.Body = body
' Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = True
' Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal
' Instantiate a new instance of SmtpClient
Dim mSmtpClient As New SmtpClient()
' Send the mail message
mSmtpClient.Send(mMailMessage)
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>
IdiosMachi
Member
575 Points
115 Posts
Re: HOW TO: Send email using System.Net.Mail
Mar 18, 2006 03:30 AM|LINK
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Send email using System.Net.Mail
Mar 18, 2006 03:37 AM|LINK
Nice. Thanks for pointing that out. That's one of those little tidbits I missed in .NET 2.0.
lchrennew
Member
361 Points
79 Posts
Re: HOW TO: Send email using System.Net.Mail
Mar 18, 2006 09:08 AM|LINK
deokule2003
Participant
1786 Points
356 Posts
Re: HOW TO: Send email using System.Net.Mail
Mar 18, 2006 05:21 PM|LINK
I've a one question related to it:
I want to send thousands emails. Shall I use this function in loop or any optimization is required to given code?
Thanking You.
Regards
Kuldeep Deokule
Blog: http://dkuldeep.blogspot.com
This posting is provided "AS IS" with no warranties, and confers no rights.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Send email using System.Net.Mail
Mar 19, 2006 09:30 PM|LINK
That depends on what thousands is equal to. You might be better off purchasing a commercial componenet to handle that and/or creating a multi-threading Windows service.
HTH,
Ryan
deokule2003
Participant
1786 Points
356 Posts
Re: HOW TO: Send email using System.Net.Mail
Mar 20, 2006 05:56 AM|LINK
Regards
Kuldeep Deokule
Blog: http://dkuldeep.blogspot.com
This posting is provided "AS IS" with no warranties, and confers no rights.
linkyossy
Member
485 Points
98 Posts
Re: HOW TO: Send email using System.Net.Mail
Mar 20, 2006 06:08 AM|LINK
Strongtypes, Please I used the mail Helper.Vb that you put out and to be quite honest, I dont know how it works. I copied the code in to a new webpage and I cant see anything, how do I know its working. Please am sorry that I might be sounding a little dumb but hey!!! just lost. I will appreciate all the help. Most importantly I want the mail to respond onclick a submit button, how do I achieve that. PLEASE
Thanks
XIII
All-Star
182707 Points
23464 Posts
ASPInsiders
Moderator
MVP
Re: HOW TO: Send email using System.Net.Mail
Mar 20, 2006 06:14 AM|LINK
Hi,
you put the class MailHelper in the App_Code subfolder, create a new file or download the codefile, and in the button's click eventhandler you can call it like StrongTypes explained in his original post.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
linkyossy
Member
485 Points
98 Posts
Re: HOW TO: Send email using System.Net.Mail
Mar 20, 2006 06:44 AM|LINK