Hi,
I used this below coding to send email but hosting & mail services are in different service providers.This code is working in my local but it's not working in hosting server.
Please guide me to resolve this.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
//using System.Web.Mail;
using System.Net.Mail;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Drawing;
public partial class bi_contact : System.Web.UI.Page
{
public string revenue1, itexp1, about1, strFilename, str;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lblerror.Visible = false;
}
}
protected void but_submit_click(object sender, EventArgs e)
{
try
{
str = "<div style='width:600px; margin:0px auto;'>";
str += "<div style='width:600px; height:auto; border-top:1px solid #dfdfdf; border-left:1px solid #dfdfdf; border-right:1px solid #dfdfdf; border-bottom:3px solid #da251e; float:left;'><img src='http://circularedge.com/image/theme_admin.png'></div>";
str += "<div style='width:560px; height:auto; border-left:1px solid #dfdfdf; border-right:1px solid #dfdfdf; padding:20px 20px; background-color:#f7f7f7; float:left;'>";
str += "<table width='450' border='0'>";
str += "<tr><td colspan=2><span style='font-family:Arial; font-size:12px; color:#2A4E98; font-weight:bold;'>Dear Sir / Madam</span></td></tr>";
str += "<tr><td align=left colspan=2><span style='font-family:Arial; font-size:12px; color:#000000;text-indent:40px;'></span></td></tr>";
str += "<tr><td align=left colspan=2> </td></tr>";
str += "<tr><td align=left colspan=2><b><u>My Contact Details :</u></b></td></tr>";
str += "<tr><td align=left width='280'> First Name : </td><td align=left width='280'> " + txt_name.Text + "</td></tr>";
str += "<tr><td align=left> Email : </td><td align=left> " + txt_email.Text + "</td></tr>";
str += "<tr><td align=left> Company Name : </td><td align=left> " + txt_company.Text + "</td></tr>";
str += "<tr><td align=left> Comments: </td><td align=left> " + txt_comments.Text + "</td></tr>";
str += "<tr><td align=left> Title : </td><td align=left> " + h_page.Value + "</td></tr>";
str += "<tr><td align=left colspan=2> </td></tr>";
str += "<tr><td align=left colspan=2> </td></tr>";
str += "<tr><td align=left colspan=2>Thanks & Regards,</td></tr>";
str += "<tr><td align=left colspan=2> " + txt_name.Text + "</td></tr>";
str += "</table></div>";
str += "<div style=' float:left; width:586px; height:30px; border-top:3px solid #da251e; background-color:#dfdfdf;border-left:1px solid #c5c5c5; border-right:1px solid #c5c5c5; border-bottom:1px solid #c5c5c5; float:left; line-height:30px; padding-left:15px; font-family:Arial; font-size:10px; color:#828282;'> � Copyright 2014 Circular Edge, Inc. All rights reserved.</div></div>";
int status = sendMail("Mail from" + "-" + h_page.Value, str);
if (status == 1)
{
lblerror.Visible = true;
lblerror.Text = "Mail sent Successfully!!!";
//lblerror.BackColor = System.Drawing.Color.Green;
txt_name.Text = "";
txt_company.Text = "";
txt_comments.Text = "";
txt_email.Text = "";
if (status == 1)
{
if (Session["amtex_code"] != null)
{
Session["amtex_code"] = null;
}
//Response.Redirect("thanks.aspx?flag=job");
}
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(GetType(), "Msg", "<script>alert(" + ex.Message + ")</script>");
}
}
public int sendMail(string subject, string body)
{
try
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(txt_email.Text, "webmaster@circularedge.com", subject, body);
System.Net.Mail.MailMessage msg1 = new System.Net.Mail.MailMessage("webmaster@circularedge.com", txt_email.Text, subject, body);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.emailsrvr.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential("webmaster@circularedge.com", "Edge2013");
//Add this line to bypass the certificate validation
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
};
msg.IsBodyHtml = true;
msg.Priority = System.Net.Mail.MailPriority.Normal;
msg1.IsBodyHtml = true;
msg1.Priority = System.Net.Mail.MailPriority.Normal;
smtpClient.Send(msg);
smtpClient.Send(msg1);
return (1);
}
catch (Exception ex)
{
// lblerror.Text = "Fill the (*) Required fields!!!";
// lblerror.BackColor = System.Drawing.Color.Red;
return (0);
}
}
}