0

I have written the below code to send an email wherein the message body is a html page. I don't get any error when i run this code but do not get any mail. I tried putting body as simple message string then i received the email but not as message body as html page. What is going wrong? please help.

    protected void btnSend_Click(object sender, EventArgs e)
    {
        SendHTMLMail();
    }

    public void SendHTMLMail()
    {
        //var path = Server.MapPath("~/test/HTMLPage.htm");
        StreamReader reader = new StreamReader(Server.MapPath("~/expo_crm/test/HTMLPage.htm"));
        string readFile = reader.ReadToEnd();
        string myString = "";
        myString = readFile;
SmtpClient smtp = new SmtpClient
        {
            Host = "mail.abc.com", // smtp server address here…
            Port = 25,
            EnableSsl = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new System.Net.NetworkCredential("[email protected]", "xxxx"),
            Timeout = 30000,
        };
        MailMessage message = new MailMessage("[email protected]", "[email protected]", " html ", myString);
        message.IsBodyHtml = true;
        smtp.Send(message);
    }
3
  • Are you sure your server is enabled to send emails? Have you checked your spam? Commented Feb 20, 2015 at 11:36
  • yes my server is enabled Commented Feb 20, 2015 at 11:37
  • assuming you've eyeballed the contents of myString to make sure that it's got a sensible value? (is valid html etc)? Commented Feb 20, 2015 at 11:39

3 Answers 3

1

Most of the time mail server checks the html content and marks as SPAM mail based on the html tags like links, images etc in the mail. Make sure that you have low number of HTML tags probably no external links, images and try again to send mail.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is working code, if this helps

public void SendEmail(ListDictionary email)
        {
            try
            {
                var msg = new MailMessage {From = new MailAddress(_emailUsername, _emailFrom), BodyEncoding = Encoding.UTF8, Subject = Convert.ToString(email["SUBJECT"]), Priority = MailPriority.Normal};
                //
                var emailTo = (List<string>) email["TO"];
                var emailCc = (List<string>) email["CC"];
                var emailBcc = (List<string>) email["BCC"];
                foreach (var to in emailTo.Where(to => to.Length > 1))
                    msg.To.Add(to);
                foreach (var cc in emailCc.Where(cc => cc.Length > 1))
                    msg.CC.Add(cc);
                foreach (var bcc in emailBcc.Where(bcc => bcc.Length > 1))
                    msg.Bcc.Add(bcc);
                //
                msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Convert.ToString(email["BODY_TEXT"]), Encoding.UTF8, "text/plain"));
                msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Convert.ToString(email["BODY_HTML"]), Encoding.UTF8, "text/html"));
                //
                new SmtpClient
                {
                    Credentials = new NetworkCredential(_emailUsername, _emailPassword),
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    EnableSsl = true,
                    Host = "smtp.gmail.com"
                }.Send(msg);
            }
            catch (Exception e)
            {
                L.Get().Fatal("Failed", e);
            }
        }

Comments

1

Could it be that you're setting the body before you set it to Html?

Worth trying using the full object constructor (new MailMessage(){ IsBodyHtml = true, Body = myString}) or setting the properties one at a time to make sure...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.