HOW TO: Send email using System.Net.Mail

Rate It (28)

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
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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>

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

    03-17-2006, 11:30 PM
    Locked
    • Member
      575 point Member
    • IdiosMachi
    • Member since 12-18-2005, 3:20 AM
    • Posts 115
    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
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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.

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

    03-18-2006, 5:08 AM
    Locked
    • Member
      361 point Member
    • lchrennew
    • Member since 12-25-2005, 3:41 AM
    • Posts 77
    thx, I love program
  • Re: HOW TO: Send email using System.Net.Mail

    03-18-2006, 1:21 PM
    Locked
    • Participant
      1,788 point Participant
    • deokule2003
    • Member since 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
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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

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

    03-20-2006, 1:56 AM
    Locked
    • Participant
      1,788 point Participant
    • deokule2003
    • Member since 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
    • Member
      485 point Member
    • linkyossy
    • Member since 02-09-2006, 6:52 AM
    • Posts 98

    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
    • All-Star
      124,796 point All-Star
    • XIII
    • Member since 07-01-2002, 3:59 AM
    • Essen, Belgium
    • Posts 13,761
    • ASPInsiders
      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.

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

    03-20-2006, 2:44 AM
    Locked
    • Member
      485 point Member
    • linkyossy
    • Member since 02-09-2006, 6:52 AM
    • Posts 98
    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
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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

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

    03-20-2006, 2:57 AM
    Locked
    • All-Star
      124,796 point All-Star
    • XIII
    • Member since 07-01-2002, 3:59 AM
    • Essen, Belgium
    • Posts 13,761
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    linkyossy wrote:
    how do I do that when I have the code in the page. Please help.

    Hi,

    when you just copied the method SendMailMessage in your page you can call it from the button's click eventhandler by just calling the method SendMailMessage with the correct parameters filled in.

    Grz, Kris.

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

    03-20-2006, 3:02 AM
    Locked
    • Member
      485 point Member
    • linkyossy
    • Member since 02-09-2006, 6:52 AM
    • Posts 98
    Thanks I copied the two files into the APP_SubFolder and called the following code

    MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")

    But it gave error. Error Message: MailHelper is not declared. Please Help. Thanks

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

    03-20-2006, 3:04 AM
    Locked
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    linkyossy wrote:
    Thanks I copied the two files into the APP_SubFolder and called the following code ... But it gave error. Error Message: MailHelper is not declared.

    There should be only 1 file in there, the code file. Your aspx file should be outside the App_Code folder. Also, the folder should be named App_Code, not APP_SubFolder.

    HTH,
    Ryan

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

    03-20-2006, 3:23 AM
    Locked
    • Member
      485 point Member
    • linkyossy
    • Member since 02-09-2006, 6:52 AM
    • Posts 98

    I have adjusted the app_code name and copied the mailHelper.vb file into the App_Code folder and I still get  the same error message. I used the code this way.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
    End Sub

    Error Message: MailHelper is not declared. Thanks. PS. I copied the webconfig file in the webconfig section.

Page 1 of 12 (174 items) 1 2 3 4 5 Next > ... Last »