Wednesday, 12 March 2014

ASP.NET: Send mail html format - Email settings for godday hosting

  1. It is always better to check email settings with godaddy technical support.
  2. You have to use the following relay server as the mail server: relay-hosting.secureserver.net
  3. You do not need to provide any user name & password for this relay server but you need an valid from email address. You have to use default credentials:smtp.UseDefaultCredentials = true; 
  4. You have to turn off ssl connection as it does not support secure connection: smtp.EnableSsl = false;
asp.net web.config file settings:
<appSettings>
    <!--email hosting for godaddy website-->
    <add key="MailServer" value="relay-hosting.secureserver.net"/>
    <add key="Port" value="25"/>
    <!--godaddy hosting does not need username & password-->
    <!--<add key="UserName" value="info@ete-consulting.co.uk"/>
    <add key="Password" value="ete214"/>-->
    <add key="FromMail" value="masuds_mail@yahoo.com"/>
    <add key="SendToMail" value="info@ete-consulting.co.uk" />
  </appSettings>
 
  <system.web>
    <!--custome error mode need to be off or remote only-->
    <customErrors mode="Off"/>
    <!--asp.net c# web application shows error:
    The application attempted to perform an operation not allowed by the security policy. 
    To grant this application the required permission please contact your system administrator or
    change the application's trust level in the configuration file. -->
    <trust level="Full" originUrl=""/>
   

config.cs class file:
public static string SendMail(string subject, string body)
    {
        //function: send html email using asp.net c#
        SmtpClient smtp = new SmtpClient();
        smtp.Host = Config.MailServer;
        smtp.Port = Config.Port;
        //credentials need to set to true for godaddy hosting
        smtp.UseDefaultCredentials = true;
        //godaddy does not support Secure connection
        smtp.EnableSsl = false;
        //smtp.Credentials = new System.Net.NetworkCredential(Config.UserName, Config.Password);
        MailMessage mailMsg = new MailMessage();
        mailMsg.To.Add(Config.SendToMail);
        mailMsg.From = new MailAddress(Config.FromMail);
        mailMsg.Subject = subject;
        mailMsg.Body = body;
        //send html email message.
        //you will be able to use html tag in the message to
        mailMsg.IsBodyHtml = true;
        string msg = null;
        try
        {
            smtp.Send(mailMsg);
        }
        catch (Exception ex)
        {
            msg = ex.Message;
            //throw ex;
        }

        return msg;
    }

aspx.cs coding to send email html formatted
protected void btnStartProject_Click(object sender, EventArgs e)
    {
        //in order to be able to see the html elements in aspx.cs use the runat="server" attribute
        string message, subject, body, name, gender;
        subject = slProjectType.Value;
        name = firstName.Value + " " + lastName.Value;
        if (male.Checked == true)
            gender = "Male";
        else
            gender = "Female";
       
       
        body = "<p>Dear sir,</p>"
            + "<p>I am interested to start a "
            + slProjectType.Value
            + " with your company http://www.ete-consulting.co.uk </P>"

            + "<p><h3>My personal details:</h3>"
            + "</p><p>First name: "
            + firstName.Value
            + "</p><p> Last name: "
            + lastName.Value
            + "</p><p>Gender: "
            + gender
            + "</p><p>My country: "
            + slCountrySelector.Value

            + "</p><p><h3>My contact details</h3>"
            + "</p><p>mobile: "
            + telephone.Value
            + "</p><p>email: "
            + email.Value

            + "</p><p><h3>Project details</h3>"
            + "</p><p>Project type: "
            + slProjectType.Value
            +"</p><p>Here is my project details (Message from the customer): "
            + txtProjectBrief.Value
            + "</p><p>Thank you</p>"
            + name
            ;
        //call the email function
        message = Config.SendMail(subject, body);
       
        if (message == null)
            lblMessage.InnerText = "Email sent succefully.";
        else
            lblMessage.InnerText = "Sorry, your email was not sent to the sender. Please try again.";
       
        //show the email successfull message
        lblMessage.InnerHtml = message;

    }

Troubleshoot godaddy website error messages
If the MailServer host name is wrong it shows error:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 202.xx.xx.xx:80
If the user credentials are used, it shows error:
the remote certificate is invalid according to the validation procedure
If EnableSsl is used it shows error:
server does not support secure connection

No comments:

Post a Comment