How to Send an Email in C# After Configuring the Server

Introduction

This article describes how to send an email in C#. For sending the email, you need to configure the email services on the server.

Note: For more info about configuration read how to configure mail server.

To explain this article, I will use the following procedure after configuring the server:

  • Create a new website and add a page into it.
  • Add 3 textboxes into the page for receiver Email-ID, subject of Email and message of Email and a button for sending the mail.
  • Write some code in the ".cs" file to send the mail with some text for the button's Click event.
The following are the details of the preceding procedure.

 Step 1: Create a new empty web site named "MailServer".

MailServer.jpg

Step 2:  Add a new Page named "SendingMail.aspx".

SendingMail.jpg

  • Add a Button with Onclick event (for sending the Email) on the page.

  1. <table>  
  2.             <tr>  
  3.                 <td>Mail To:-</td>  
  4.                 <td>  
  5.                     <asp:TextBox ID="txtEmail" runat="server" Width="200px">  
  6.                     </asp:TextBox></td>  
  7.             </tr>  
  8.             <tr>  
  9.                 <td>Subject:-</td>  
  10.                 <td>  
  11.                     <asp:TextBox ID="txtSubject" runat="server" Width="200px">  
  12.                     </asp:TextBox></td>  
  13.             </tr>  
  14.             <tr>  
  15.                 <td>Message:-</td>  
  16.                 <td>  
  17.                     <asp:TextBox ID="txtmessagebody" runat="server" TextMode="MultiLine" Height="200px" Width="400px">  
  18.                     </asp:TextBox></td>  
  19.             </tr>  
  20.             <tr>  
  21.                 <td colspan="2" align="center">  
  22.                     <asp:Button ID="btn_send" runat="server" Text="Send Mail"  
  23.                         OnClick="btn_send_Click" /></td>  
  24.             </tr>  
  25.         </table>   

For sending the Email.jpg

  • Add the 3 namespaces on top of the ".cs" file.
  1. using System.Net.Mail;  
  2. using System.IO;  
  3. using System.Text;  
  • Write the code to sending the email on the click event of the button.
  1.    protected void btn_send_Click(object sender, EventArgs e)  
  2.     {  
  3.         try  
  4.         {  
  5.             MailMessage message = new MailMessage();  
  6.             message.To.Add(txtEmail.Text);// Email-ID of Receiver  
  7.             message.Subject = txtSubject.Text;// Subject of Email  
  8. // Email-ID of Sender  
  9.             message.From = new System.Net.Mail.MailAddress("[email protected]");  
  10.             message.Body = txtmessagebody.Text;// body of Email       
  11.             SmtpClient SmtpMail = new SmtpClient();  
  12. //name or IP-Address of Host used for SMTP transactions  
  13.             SmtpMail.Host = "Your Host";   
  14.             SmtpMail.Port = 25;//Port for sending the mail  
  15. //username/password of network, if apply  
  16.             SmtpMail.Credentials = new System.Net.NetworkCredential("""");  
  17.             SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;  
  18.             SmtpMail.EnableSsl = false;  
  19.             SmtpMail.ServicePoint.MaxIdleTime = 0;  
  20.             SmtpMail.ServicePoint.SetTcpKeepAlive(true, 2000, 2000);  
  21.             message.BodyEncoding = Encoding.Default;  
  22.             message.Priority = MailPriority.High;  
  23.             message.IsBodyHtml = true;  
  24.             SmtpMail.Send(message); //Smtpclient to send the mail message  
  25.             Response.Write("Email has been sent");  
  26.         }  
  27.         catch (Exception ex)  
  28.         { Response.Write("Failed"); }  
  29.     }  
Note: the "SmtpMail.Host" value will be your hosting name or IP Address and "SmtpMail.Port" will also vary.

SmtpMail Host.jpg

Step 3:  Run the page in the server that will be like:
Page on server.jpg
  • After filling in the valid Email ID, Subject of Email and message of Email, click on the "Send mail" button to send the email.
Page on server 2.jpg
Result: Now you can see that, I (the receiver) got the email.

Page on server 2 result.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all