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)

            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>&nbsp;</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 : &nbsp;</td><td align=left width='280'> " + txt_name.Text + "</td></tr>";
            str += "<tr><td align=left> Email : &nbsp;</td><td align=left> " + txt_email.Text + "</td></tr>";
            str += "<tr><td align=left> Company Name : &nbsp;</td><td align=left> " + txt_company.Text + "</td></tr>";
            str += "<tr><td align=left> Comments: &nbsp;</td><td align=left> " + txt_comments.Text + "</td></tr>";
            str += "<tr><td align=left> Title : &nbsp;</td><td align=left> " + h_page.Value + "</td></tr>";
            str += "<tr><td align=left colspan=2>&nbsp;</td></tr>";
            str += "<tr><td align=left colspan=2>&nbsp;</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;'>&nbsp; � 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;

        catch (Exception ex)

            ClientScript.RegisterStartupScript(GetType(), "Msg", "<script>alert(" + ex.Message + ")</script>");

    public int sendMail(string subject, string body)

            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;


            return (1);

        catch (Exception ex)
            // lblerror.Text = "Fill the (*) Required fields!!!";
            // lblerror.BackColor = System.Drawing.Color.Red;
            return (0);


Recommended Answers

All 7 Replies

This code is working in my local but it's not working in hosting server.

Do you perhaps have to whitelist the server you are sending from?

Support team said, code is in error.

Did they say anything less cryptic? They should be able to see whether it is the authentication or certificate or whatever.

They didn't say anything about this

Support team said, code is in error

hmm.. you'd expect that if the code was the problem, it wouldnt work on your local system. Have you tried using your provider's smtp relay server? --just to validate that your code is good and you are able to send mail via their system instead of one outside of their control?

When I run the code,it shows error in Remote certificate validation.So, I added bypass certificate code lines in my coding then it runs good.

But Initially it was not running in server.

I still prefer using Gmail or Hotmail accounts for sending mail from ASP.NET. They are most trusted...

const string sender = "mtyide@gmail.com";
var from = new System.Net.Mail.MailAddress(sender, "Yandisa Mtyide");
var to = new System.Net.Mail.MailAddress(email, email);
var bcc = new System.Net.Mail.MailAddress("postmaster@paaanda.com", "postmaster@paaanda.com");

var client = new System.Net.Mail.SmtpClient
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Timeout = 30 * 1000,
Credentials = new System.Net.NetworkCredential(@from.Address, "**************")

using (var message = new System.Net.Mail.MailMessage())
            message.From = @from;

            message.Subject = someSubject;
            message.Body = someMessage;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.