How to Send Email using Asp.Net with C#
<div id="404message" class="popup_block">
<div class="text_fild"> Name </div>
<div class="text_fild"><input type="text" id="txtName" runat="server" size="30" /> <asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtName" ValidationGroup="sendmail"
ErrorMessage="Required Field" ForeColor="Red" Font-Size="XX-Small"></asp:RequiredFieldValidator>
</div>
<div class="text_fild"> Email</div>
<div class="text_fild"><input type="text" id="txtEmail" runat="server" size="30" /><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ValidationGroup="sendmail"
ErrorMessage="Required Field" Font-Size="XX-Small"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationGroup="sendmail"
ControlToValidate="txtEmail" ErrorMessage="Invalid Email" ForeColor="Red" Font-Size="XX-Small"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</div>
<div class="text_fild"> Phone</div>
<div class="text_fild"><input type="text" id="txtPhone" runat="server" size="30" /><asp:RequiredFieldValidator ValidationGroup="sendmail"
ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPhone" ForeColor="Red" Font-Size="XX-Small"
ErrorMessage="Required Field"></asp:RequiredFieldValidator>
</div>
<div class="text_fild"><textarea name="" cols="8" rows="8" id="Comment" runat="server" style="width: 432px; height: 149px;" ></textarea>
<asp:RequiredFieldValidator ValidationGroup="sendmail"
ID="RequiredFieldValidator4" runat="server" ControlToValidate="Comment" ForeColor="Red" Font-Size="XX-Small"
ErrorMessage="Required Field"></asp:RequiredFieldValidator>
</div>
<div class="clr"></div>
<div class="text_fild">
<asp:ImageButton ID="ImageButton1" ImageUrl="/Sitefinity/WebsiteTemplates/ABG/App_Themes/ABG/global/images/cancel_but.png"
runat="server" onclick="imgCancel_Click" CausesValidation="False" />
<asp:ImageButton ID="imgSendMail" ImageUrl="/Sitefinity/WebsiteTemplates/ABG/App_Themes/ABG/global/images/submit_but.png" ValidationGroup="sendmail"
runat="server" onclick="imgSendMail_Click" />
</div>
</div>
Code View
protected void imgSendMail_Click(object sender, ImageClickEventArgs e)
{
send_Mail();
}
protected bool send_Mail()
{
string strBody;
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
// setup Smtp authentication
// System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(fromId, "Test1234");
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("abc@gmail.com", "abc123");
client.UseDefaultCredentials = false;
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtEmail.Value );
msg.To.Add(new MailAddress("xyz@gmail.com"));
msg.Subject = "Ask A Question";
msg.IsBodyHtml = true;
string str=Comment.Value ;
msg.Body = string.Format(str);
try
{
client.Send(msg);
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script language='javascript'>RefreshParent()</script>");
txtEmail.Value = "";
txtName.Value = "";
txtPhone.Value = "";
Comment.Value = "";
return true;
}
catch (Exception ex)
{
throw ex;
//return false;
}
return true;
}
Design View
<div id="404message" class="popup_block">
<div class="text_fild"> Name </div>
<div class="text_fild"><input type="text" id="txtName" runat="server" size="30" /> <asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtName" ValidationGroup="sendmail"
ErrorMessage="Required Field" ForeColor="Red" Font-Size="XX-Small"></asp:RequiredFieldValidator>
</div>
<div class="text_fild"> Email</div>
<div class="text_fild"><input type="text" id="txtEmail" runat="server" size="30" /><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ValidationGroup="sendmail"
ErrorMessage="Required Field" Font-Size="XX-Small"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationGroup="sendmail"
ControlToValidate="txtEmail" ErrorMessage="Invalid Email" ForeColor="Red" Font-Size="XX-Small"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</div>
<div class="text_fild"> Phone</div>
<div class="text_fild"><input type="text" id="txtPhone" runat="server" size="30" /><asp:RequiredFieldValidator ValidationGroup="sendmail"
ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPhone" ForeColor="Red" Font-Size="XX-Small"
ErrorMessage="Required Field"></asp:RequiredFieldValidator>
</div>
<div class="text_fild"><textarea name="" cols="8" rows="8" id="Comment" runat="server" style="width: 432px; height: 149px;" ></textarea>
<asp:RequiredFieldValidator ValidationGroup="sendmail"
ID="RequiredFieldValidator4" runat="server" ControlToValidate="Comment" ForeColor="Red" Font-Size="XX-Small"
ErrorMessage="Required Field"></asp:RequiredFieldValidator>
</div>
<div class="clr"></div>
<div class="text_fild">
<asp:ImageButton ID="ImageButton1" ImageUrl="/Sitefinity/WebsiteTemplates/ABG/App_Themes/ABG/global/images/cancel_but.png"
runat="server" onclick="imgCancel_Click" CausesValidation="False" />
<asp:ImageButton ID="imgSendMail" ImageUrl="/Sitefinity/WebsiteTemplates/ABG/App_Themes/ABG/global/images/submit_but.png" ValidationGroup="sendmail"
runat="server" onclick="imgSendMail_Click" />
</div>
</div>
Code View
protected void imgSendMail_Click(object sender, ImageClickEventArgs e)
{
send_Mail();
}
protected bool send_Mail()
{
string strBody;
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
// setup Smtp authentication
// System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(fromId, "Test1234");
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("abc@gmail.com", "abc123");
client.UseDefaultCredentials = false;
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtEmail.Value );
msg.To.Add(new MailAddress("xyz@gmail.com"));
msg.Subject = "Ask A Question";
msg.IsBodyHtml = true;
string str=Comment.Value ;
msg.Body = string.Format(str);
try
{
client.Send(msg);
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script language='javascript'>RefreshParent()</script>");
txtEmail.Value = "";
txtName.Value = "";
txtPhone.Value = "";
Comment.Value = "";
return true;
}
catch (Exception ex)
{
throw ex;
//return false;
}
return true;
}
No comments:
Post a Comment