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.
<form id="form1" runat="server">
<div style="text-align: center">
<div>
<asp:Label ID="lblErrorMsg" runat="server" Text="L Publish Postabel"></asp:Label>
<form id="form1" runat="server">
<div style="text-align: center">
<div>
<asp:Label ID="lblErrorMsg" runat="server" Text="L
<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>
And paste the code in Default.aspx.cs page(i,e codebehind )
try
{
SmtpClient stp = new SmtpClient();
//Create a smtpclient object for sending mail.
MailMessage mail = new MailMessage();
//Create a MailMessage object for drafting mail.
MailAddress madd = new MailAddress(txtFrom.Text);
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.Attachments.Add(new Attachment(attachFile));
}
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;
}
Mail Setting in Web.Config file.
Using this code under the configuration tag
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="jitendra@infotechsolution.com" >
<network defaultCredentials="true" host="192.168.0.1" port="25" userName=" jitendra@infotechsolution.com " password="test"/>
</smtp>
</mailSettings>
</system.net>
Accssing web.config setting in our code behind page
First import System.Configuration Namespace.
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);
No comments:
Post a Comment