Saturday, January 22, 2011

Sending Mail Using C# and SMTP (Simple Mail Transfer Protocol)

The SMTP or Simple Mail Transfer Protocol is described in RFC 821.This application protocols are used to send email over the Internet.

The DOTNET framework already contains an SMTP class in the System.Net.Mail Namespace called SMTP Mail. This class is sufficient for sending email over the Internet.

Step 1:
First open Visual Studio and select New Website and Language C#. Give name as Mailsend.aspx.Copy and Paste the below Code.
    <div>
    <table width="80%" align="center" bgcolor="#ffffff" style="border: 1px dotted #99CC33;font-family:Verdana;font-size:11px;">
    <tr>
    <td bgcolor="#95c03f" colspan="2">
     <strong><span style="font-family:Trebuchet MS;font-size:13px;">Sending Mail Using C# and SMTP (Simple Mail Transfer Protocol)</span></strong>
    </td>
    </tr>
<tr>
    <td align="left" colspan="2">
        <asp:Label ID="lblError" runat="server"></asp:Label></td>
    </tr>
    <tr>
    <td align="right">
        To Address:</td>
    <td align="left">
        <asp:TextBox ID="txtToAddress" runat="server" Width="389px"></asp:TextBox></td>
    </tr>
        <tr>
            <td align="right">
                Subject:</td>
            <td align="left">
                <asp:TextBox ID="txtSubject" runat="server" Width="389px"></asp:TextBox></td>
        </tr>
        <tr>
            <td align="right">
                Body:</td>
            <td align="left">
                <asp:TextBox ID="txtBody" runat="server" Height="98px" TextMode="MultiLine" Width="389px"></asp:TextBox></td>
        </tr>
  <tr>
            <td align="right">
                Attach a file: </td>
            <td align="left">
                <asp:FileUpload ID="fuAttachFile" runat="server" Width="395px" /></td>
        </tr>
        <tr>
            <td align="right">
            </td>
            <td align="left">
                <asp:Button ID="btnSend" runat="server" Text="Send" /></td>
        </tr>
    </table>
    </div>

The GUI Representation of above code.


 
Step2:
          Copy and Paste the below Code under Button Click Event.Remember it is under Mailsend.aspx.cs.

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMsg = new MailMessage("FromAddress", txtToAddress.Text);
            mailMsg.Subject = txtSubject.Text;
            mailMsg.SubjectEncoding = Encoding.UTF8;

            ///add mail body

            mailMsg.Body = txtBody.Text;
            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.IsBodyHtml = true;

//Add Attachments
            if (fuAttachFile.FileName != "")
            {
                mailMsg.Attachments.Add(new Attachment(fuAttachFile.PostedFile.FileName));
            }
            //Client Credential Details

            System.Net.Mail.SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("From Address", "Password");
            client.Port = 587;//gmail port no           
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;

            //client.SendCompleted += new SendCompletedEventHandler  (client_SendCompleted);
            //object userState = mailMessage;

            lblError.Text = "Sending....";
            //For Synchronous Execution....
            client.Send(mailMsg);
            //For process to run Asynchronously....
            //client.SendAsync(mailMessage, userState);
            lblError.Text = "Mail Has Been Sent Successfully";
        }

        catch (System.Net.Mail.SmtpException ex)
        {
            lblError.Text = "Mail Error : " + ex.Message;
        }
        catch (System.NullReferenceException ex)
        {
            lblError.Text = ex.Message;
        }
    }

Now You Can Send Mails through your code.Please Give To address,Subject and Body Click On Send Button.

Conclusion:
                   I Hope this code will be Usefull.
           

6 comments:

  1. Kindly Help me to set the maximum length of body text in email?

    ReplyDelete
  2. Hi you can set maximum length property for Body textbox

    ReplyDelete
  3. Really Nice Information,Thank You Very Much For Sharing.
    Web Designing Company

    ReplyDelete
  4. Great post! I am see the .Net programming with SMTP coding levels for the step by step execute the transfer protocols coding.I am gather these coding has more information.

    Dotnet Training in Chennai

    ReplyDelete