Sending mail in Asp.net

Language
For sending a mail in Asp.net,import System.Net.Mail Namespace. This is a simple application which u can use as a feedback form or contact us page. Create a simple default.aspx page with following code.
  1. <form id="form1" runat="server">
  2.   <div style="text-align: center">
  3.     <div>
  4.       <asp:Label ID="lblErrorMsg" runat="server" Text="Label"></asp:Label>
  5.     </div>
  6.     <table border="1">
  7.       <tr>
  8.         <td colspan="2" style="font-weight: 700; text-align: center; background-color: #F7C331;"> Send Mail in ASP.net through SMTP </td>
  9.       </tr>
  10.       <tr>
  11.         <td style="font-weight: 700; text-align: right;"> TO </td>
  12.         <td><asp:TextBox ID="txtTo" runat="server" Width="242px"></asp:TextBox>
  13.         </td>
  14.       </tr>
  15.       <tr>
  16.         <td style="font-weight: 700; text-align: right;"> From </td>
  17.         <td><asp:TextBox ID="txtFrom" runat="server" Width="242px"></asp:TextBox>
  18.         </td>
  19.       </tr>
  20.       <tr>
  21.         <td style="font-weight: 700; text-align: right;"> CC </td>
  22.         <td><asp:TextBox ID="txtcc" runat="server" Width="242px"></asp:TextBox>
  23.         </td>
  24.       </tr>
  25.       <tr>
  26.         <td style="font-weight: 700; text-align: right;"> BCC </td>
  27.         <td><asp:TextBox ID="txtbcc" runat="server" Width="242px"></asp:TextBox>
  28.         </td>
  29.       </tr>
  30.       <tr>
  31.         <td style="font-weight: 700; text-align: right;"> Subject </td>
  32.         <td><asp:TextBox ID="txtSub" runat="server" Width="242px"></asp:TextBox>
  33.         </td>
  34.       </tr>
  35.       <tr>
  36.         <td style="font-weight: 700; text-align: right;"> Message </td>
  37.         <td><asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" Height="75px" Width="246px"></asp:TextBox>
  38.         </td>
  39.       </tr>
  40.       <tr>
  41.         <td style="font-weight: 700"> Attachment </td>
  42.         <td><asp:FileUpload ID="FileUpload1" runat="server" Width="244px" />
  43.         </td>
  44.       </tr>
  45.       <tr>
  46.         <td></td>
  47.         <td><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="SendMail" />
  48.         </td>
  49.       </tr>
  50.     </table>
  51.   </div>
  52. </form>
And paste the code in Default.aspx.cs page(i,e codebehind )
  1. try
  2.             {
  3.                 SmtpClient stp = new SmtpClient();
  4.                 //Create a smtpclient object for sending mail.
  5.                 MailMessage mail = new MailMessage();
  6.                 //Create a MailMessage object for drafting mail.
  7.                 MailAddress madd = new MailAddress(txtFrom.Text);
  8.                 mail.From = madd;
  9.                 mail.To.Add(txtTo.Text);
  10.  
  11.                 if (!string.IsNullOrEmpty(txtcc.Text.Trim()))
  12.                 {
  13.                     mail.CC.Add(txtcc.Text.Trim());
  14.                 }
  15.                 if (!string.IsNullOrEmpty(txtbcc.Text.Trim()))
  16.                 {
  17.                     mail.CC.Add(txtbcc.Text.Trim());
  18.                 }
  19.  
  20.                 if (FileUpload1.HasFile == true)
  21.                 {
  22.                     string attachFile = FileUpload1.PostedFile.FileName.ToString();
  23.                     mail.Attachments.Add(new Attachment(attachFile));
  24.                 }
  25.  
  26.                 mail.Subject = txtSub.Text.Trim();
  27.                 mail.Body = txtMsg.Text.Trim();
  28.  
  29.                 stp.Send(mail);//sending mail using Send method of SmtpClient class.
  30.                 lblErrorMsg.Text = "Mail successfully sent.";
  31.             }
  32.             catch (Exception ex)
  33.             {
  34.                 lblErrorMsg.Text = ex.Message;
  35.             }
Mail Setting in Web.Config file. Using this code under the configuration tag
  1. <system.net>
  2. <mailSettings>
  3. <smtp deliveryMethod="Network" from="[email protected]" >
  4. <network defaultCredentials="true" host="192.168.0.1" port="25" userName=" [email protected] " password="test"/>
  5.  
  6. </smtp>
  7. </mailSettings>
  8. </system.net>
Accessing web.config setting in our code behind page First import System.Configuration Namespace.
  1.  Configuration conf = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
  2.    System.Net.Configuration.MailSettingsSectionGroup mailsettings = (System.Net.Configuration.MailSettingsSectionGroup)conf.GetSectionGroup("system.net/mailSettings");
  3.    Response.Write("DefaultCredentials:-&gt;" + mailsettings.Smtp.Network.DefaultCredentials);
  4.    Response.Write("
  5. Host:-&gt;" + mailsettings.Smtp.Network.Host);
  6.    Response.Write("
  7. Port:-&gt;" + mailsettings.Smtp.Network.Port);
  8.    Response.Write("
  9. UserName:-&gt;" + mailsettings.Smtp.Network.UserName);
  10.    Response.Write("
  11. Password:-&gt;" + mailsettings.Smtp.Network.Password);

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Comments

Pls. Help I need payroll management system in asp.net C#. Thanks in advance..

Thanks....I m in search of this type of code only .Thank you

This code give namespace error System.Web.Mail.MailMessage

thanks a lot for providing the hole program......

Add new comment