ASPX电子邮件HTML格式

时间:2016-11-25 00:20:37

标签: c# asp.net

我有这个发送电子邮件的aspx函数。不幸的是,它没有在gmail或outlook上呈现html标签,但在我的Android手机上看起来很好。怎么解决?感谢。

protected void SendMail()
{

    var fromAddress = "device@email.com";
        var toAddress = "toEMAIL@gmail.com"; 


         const string fromPassword = "****";
        var subject = "subject";        


    string body = " <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">";
    body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">";
    body += "</HEAD><BODY>";
    body += "<b>some text some text</b><br></br>";
    body += "</BODY></HTML>";



    var smtp = new System.Net.Mail.SmtpClient();
    {

        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    smtp.Send(fromAddress, toAddress, subject, body);
}

EDITED: 所以这就是我现在所拥有的。但是编译器给出了这个错误: 第392行:mail.From = fromAddress; 编译器错误消息:CS0029:无法将类型'string'隐式转换为'System.Net.Mail.MailAddress'

 var fromAddress = "device@***";
                        var toAddress = "*****@gmail.com";
                        const string fromPassword = "******";
                        var subject = "New Project Started!";
                        string body = "<b>TA:</b>";

                        MailMessage mail= new MailMessage();
                        mail.From = fromAddress;
                        mail.To = toAddress;
                        mail.Subject = subject;
                        mail.IsBodyHtml = true;
                        mail.Body = body;

        var smtp = new System.Net.Mail.SmtpClient();
        {

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }

                        smtp.Send(mail);

编辑: 我的工作解决方案:

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

                var fromAddress = "****"; // mail Address from where you send the mail
                var toAddress = "****"; 
                const string fromPassword = "***"; //Password of your gmail address
                var subject = "New Job Created!";

                // set smtp-client with basicAuthentication
                SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
                mySmtpClient.Port = 587;
                mySmtpClient.EnableSsl = true;
                mySmtpClient.UseDefaultCredentials = false;
                System.Net.NetworkCredential basicAuthenticationInfo = new

                System.Net.NetworkCredential(fromAddress, fromPassword);
                mySmtpClient.Credentials = basicAuthenticationInfo;

                // add from,to mailaddresses
                MailAddress from = new MailAddress(fromAddress, "");
                MailAddress to = new MailAddress(toAddress, "Devision");
                MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

                // set subject and encoding
                myMail.Subject = subject;
                myMail.SubjectEncoding = System.Text.Encoding.UTF8;

                // set body-message and encoding
                myMail.Body = "<b>TEST</b>";
                myMail.BodyEncoding = System.Text.Encoding.UTF8;

                // text or html
                myMail.IsBodyHtml = true;

                mySmtpClient.Send(myMail);

1 个答案:

答案 0 :(得分:1)

创建MailMessage类。关键是指定使用属性IsBodyHtml

完成HTML的主体
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;

//other stuff

smtp.Send(mail);