HOW TO: Send email using System.Net.Mail

Rate It (22)

Last post 08-01-2007 8:01 AM by Johnny be good. 173 replies.

Sort Posts:

  • HOW TO: Send email using System.Net.Mail

    03-14-2006, 2:16 AM
    Locked

    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 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>

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: HOW TO: Send email using System.Net.Mail

    03-17-2006, 11:30 PM
    Locked
    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.
  • Re: HOW TO: Send email using System.Net.Mail

    03-17-2006, 11:37 PM
    Locked

    IdiosMachi wrote:
    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. Thanks for pointing that out. That's one of those little tidbits I missed in .NET 2.0.

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: HOW TO: Send email using System.Net.Mail

    03-18-2006, 5:08 AM
    Locked
    • Loading...
    • lchrennew
    • Joined on 12-25-2005, 3:41 AM
    • Posts 67
    thx, I love program
  • Re: HOW TO: Send email using System.Net.Mail

    03-18-2006, 1:21 PM
    Locked
    • Loading...
    • deokule2003
    • Joined on 12-05-2005, 6:09 PM
    • Pune, India
    • Posts 356
    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.
  • Re: HOW TO: Send email using System.Net.Mail

    03-19-2006, 5:30 PM
    Locked

    deokule2003 wrote:
    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.

    HTH,
    Ryan

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: HOW TO: Send email using System.Net.Mail

    03-20-2006, 1:56 AM
    Locked
    • Loading...
    • deokule2003
    • Joined on 12-05-2005, 6:09 PM
    • Pune, India
    • Posts 356
    Thank you for suggestion.

    Regards

    Kuldeep Deokule

    MCSD.NET
    Blog: http://dkuldeep.blogspot.com

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: HOW TO: Send email using System.Net.Mail

    03-20-2006, 2:08 AM
    Locked
    • Loading...
    • linkyossy
    • Joined on 02-09-2006, 6:52 AM
    • Posts 97

    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

  • Re: HOW TO: Send email using System.Net.Mail

    03-20-2006, 2:14 AM
    Locked
    • Loading...
    • XIII
    • Joined on 07-01-2002, 3:59 AM
    • Essen, Belgium
    • Posts 9,125
    • Moderator
      TrustedFriends-MVPs

    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.

    Kris van der Mast [MVP] || I got my MVP renewal for 2008!
  • Re: HOW TO: Send email using System.Net.Mail

    03-20-2006, 2:44 AM
    Locked
    • Loading...
    • linkyossy
    • Joined on 02-09-2006, 6:52 AM
    • Posts 97
    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
  • Re: HOW TO: Send email using System.Net.Mail

    03-20-2006, 2:48 AM
    Locked

    The file download comes with the files as it should be placed in your project. Also, when you add a class file in Visual Studio, it will prompt you to place it in the App_Code folder. Otherwise, create a folder in the root of the project named App_Code and place it there. How you call the helper is in my original thread.

    HTH,
    Ryan

    Ryan Olshan
    Microsoft MVP, ASP.NET
    Blog | Group | Website | Strong Coders Community

    How to ask a question
  • Re: HOW TO: Send email using System.Net.Mail

    03-20-2006, 2:57 AM