Sending mail in Asp.net
Submitted by jitendramcu on Monday, October 26, 2009 - 14:48.
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.
And paste the code in Default.aspx.cs page(i,e codebehind )
Mail Setting in Web.Config file.
Using this code under the configuration tag
Accessing web.config setting in our code behind page
First import System.Configuration Namespace.
- <form id="form1" runat="server">
- <div style="text-align: center">
- <div>
- <asp:Label ID="lblErrorMsg" runat="server" Text="Label"></asp:Label>
- </div>
- <table border="1">
- <tr>
- <td colspan="2" style="font-weight: 700; text-align: center; background-color: #F7C331;"> Send Mail in ASP.net through SMTP </td>
- </tr>
- <tr>
- <td style="font-weight: 700; text-align: right;"> TO </td>
- <td><asp:TextBox ID="txtTo" runat="server" Width="242px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="font-weight: 700; text-align: right;"> From </td>
- <td><asp:TextBox ID="txtFrom" runat="server" Width="242px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="font-weight: 700; text-align: right;"> CC </td>
- <td><asp:TextBox ID="txtcc" runat="server" Width="242px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="font-weight: 700; text-align: right;"> BCC </td>
- <td><asp:TextBox ID="txtbcc" runat="server" Width="242px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="font-weight: 700; text-align: right;"> Subject </td>
- <td><asp:TextBox ID="txtSub" runat="server" Width="242px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="font-weight: 700; text-align: right;"> Message </td>
- <td><asp:TextBox ID="txtMsg" runat="server" TextMode="MultiLine" Height="75px" Width="246px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="font-weight: 700"> Attachment </td>
- <td><asp:FileUpload ID="FileUpload1" runat="server" Width="244px" />
- </td>
- </tr>
- <tr>
- <td></td>
- <td><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="SendMail" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- try
- {
- //Create a smtpclient object for sending mail.
- //Create a MailMessage object for drafting mail.
- mail.From = madd;
- mail.To.Add(txtTo.Text);
- if (!string.IsNullOrEmpty(txtcc.Text.Trim()))
- {
- mail.CC.Add(txtcc.Text.Trim());
- }
- if (!string.IsNullOrEmpty(txtbcc.Text.Trim()))
- {
- mail.CC.Add(txtbcc.Text.Trim());
- }
- if (FileUpload1.HasFile == true)
- {
- string attachFile = FileUpload1.PostedFile.FileName.ToString();
- }
- mail.Subject = txtSub.Text.Trim();
- mail.Body = txtMsg.Text.Trim();
- stp.Send(mail);//sending mail using Send method of SmtpClient class.
- lblErrorMsg.Text = "Mail successfully sent.";
- }
- catch (Exception ex)
- {
- lblErrorMsg.Text = ex.Message;
- }
- <system.net>
- <mailSettings>
- <network defaultCredentials="true" host="192.168.0.1" port="25" userName=" [email protected] " password="test"/>
- </smtp>
- </mailSettings>
- </system.net>
- Configuration conf = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
- System.Net.Configuration.MailSettingsSectionGroup mailsettings = (System.Net.Configuration.MailSettingsSectionGroup)conf.GetSectionGroup("system.net/mailSettings");
- Response.Write("DefaultCredentials:->" + mailsettings.Smtp.Network.DefaultCredentials);
- Response.Write("
- Host:->" + mailsettings.Smtp.Network.Host);
- Response.Write("
- Port:->" + mailsettings.Smtp.Network.Port);
- Response.Write("
- UserName:->" + mailsettings.Smtp.Network.UserName);
- Response.Write("
- Password:->" + 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.
Comments
Superb...
Thanks for providing the code with HTML, just simply copy pasted to my project.. Thanks again... Even this website http://www.compiletimeerror.com/2013/03/c-sharp-send-email-from-c-program.html addresses something similar.. Have a look.. May help..
Add new comment
- Add new comment
- 75 views