4

我想通过 asp.net 发送电子邮件。我使用此代码,它工作正常。

mail.From = new MailAddress("mail@gmail.com", "sender", System.Text.Encoding.UTF8);
string to = Session["Umail"].ToString();
mail.To.Add(to);
mail.IsBodyHtml = true;

mail.SubjectEncoding = Encoding.UTF8;
mail.BodyEncoding = Encoding.GetEncoding("utf-8");
mail.Subject = "subject";
mail.Body = "body" ;
SmtpClient smtp = new SmtpClient("Smtp.gmail.Com", 587);
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("mail@gmail.com", "pass"); smtp.Send(mail);

但我想要一封定制的漂亮邮件。就像从 facebook、google 团队等发送的电子邮件一样。我知道可以在其中使用 html 标签,mail.Body但这是好方法吗?什么是最好的方法 ?

4

2 回答 2

4

这是可以使用的代码片段,我用于发送包含文本内容和基于 html 模板的内容的电子邮件:

        // first we create a plain text version and set it to the AlternateView
        // then we create the HTML version
        MailMessage msg = new MailMessage();

        msg.From = new MailAddress("from@email", "From Name");
        msg.Subject = "Subject";
        msg.To.Add("to@email");
        msg.BodyEncoding = Encoding.UTF8;

        String plainBody = "Body of plain email";

        //first we create the text version
        AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
        msg.AlternateViews.Add(plainView);

        //now create the HTML version
        MailDefinition message = new MailDefinition();
        message.BodyFileName = "~/MailTemplates/template1.htm";
        message.IsBodyHtml = true;
        message.From = "from@email";
        message.Subject = "Subject";

        //Build replacement collection to replace fields in template1.htm file
        ListDictionary replacements = new ListDictionary();
        replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username

        //now create mail message using the mail definition object
        //the CreateMailMessage object takes a source control object as the last parameter,
        //if the object you are working with is webcontrol then you can just pass "this",
        //otherwise create a dummy control as below.
        MailMessage msgHtml = message.CreateMailMessage("to@email", replacements, new LiteralControl());

        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");

//example of a linked image        
        LinkedResource imgRes = new LinkedResource(Server.MapPath("~/MailTemplates/images/imgA.jpg"), System.Net.Mime.MediaTypeNames.Image.Jpeg);
        imgRes.ContentId = "imgA";
        imgRes.ContentType.Name = "imgA.jpg";
        imgRes.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
        htmlView.LinkedResources.Add(imgRes);

        msg.AlternateViews.Add(htmlView);

        //sending prepared email
        SmtpClient smtp = new SmtpClient();//It reads the SMPT params from Web.config
        smtp.Send(msg);

这些是 html 模板的关键部分:

<p>Username: <%USERNAME%></p>

<img src="cid:imgA"/>
于 2013-05-04T13:50:21.007 回答
1

我想要一封定制的漂亮邮件。

我知道可以在其中使用 html 标签,mail.Body但这是好方法吗?什么是最好的方法 ?

我不知道这到底是什么意思,但一般来说,有两种方法可以做到这一点。(如果我们在电子邮件等中谈论图像或来源……)

您可以使用LinkedResource.NET Framework 的类。

表示电子邮件附件中嵌入的外部资源,例如 HTML 附件中的图像。

或者,在我看来,更简单的是,如果您想在电子邮件中使用某些图像,请将图像放在公共位置,然后只需在电子邮件的 HTML 中引用该位置即可。

于 2013-05-04T11:25:59.743 回答