Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

The following is my code for sending mail with html body. it is working in the localhost. but when i uploaded to my windows server, it does not send email. without html file, i can send a mail from my server. I uploaded aspx file and html file and webconfig file in the same directory.

private string PopulateBody(string userName, string phone, string email, string message)
    {
        string body = string.Empty;
        using (System.IO.StreamReader reader = new System.IO.StreamReader(Server.MapPath("~/bodyPage.htm")))
        {
            body = reader.ReadToEnd();
        }
        body = body.Replace("{Name}", userName);
        body = body.Replace("{Phoneno}", phone);
        body = body.Replace("{Email}", email);
        body = body.Replace("{Message}", message);
        return body;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            // Sender e-mail address.
            Msg.From = new MailAddress(txtEmail.Text);
            // Recipient e-mail address.
            Msg.To.Add("info@dhuvara.com");
            Msg.Subject = txtEmail.Text;
            string message = PopulateBody(txtName.Text,txtphone.Text,txtEmail.Text,txtMessage.Text);
            Msg.Body = message;
            Msg.CC.Add("baskey2@gmail.com");
            Msg.IsBodyHtml = true;
            // your remote SMTP server IP.
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.live.com"; 
            smtp.Port = 587;
            Msg.Priority = MailPriority.Normal;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("info@dhuvara.com", "SolaiMail");
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            //Msg = null;
             lbltxt.Text = "Thanks for Contact us";
            // Clear the textbox valuess
            txtName.Text = "";
          //  txtSubject.Text = "";
            txtMessage.Text = "";
            txtEmail.Text = "";
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} Exception caught.", ex.ToString());
        }
Posted
Comments
BK 4 code 26-Dec-13 4:53am    
Can you please give me what exception or error you have got ?
baskaran chellasamy 26-Dec-13 5:05am    
No error through in code while in server. but i does not send a mail.
www.dhuvara.com/sub/asp/Default3.aspx this is the page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900