發送預先電子郵件的最佳方式 (Best way for sending advance email)


問題描述

發送預先電子郵件的最佳方式 (Best way for sending advance email)

I'd like to send email by asp.net . I use this code and it is work fine.

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);

But I'd like a custom and beautiful mail. Like emails that send from facebook, google team and etc. I know that can use html tag in mail.Body but is it good way? What is the best way ?


參考解法

方法 1:

This is ready to use code snippet which I use for sending email which contains both text content and the content based on a html template:

        // 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);

and these are key parts of the html template:

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

<img src="cid:imgA"/>

方法 2:

  

I'd like a custom and beautiful mail.

     

I know that can use html tag in mail.Body but is it good way? What is   the best way ?

I don't know exactly what is that supposed to mean, but generally, there are two ways to do it. (If we talking about images or sources in email etc..)

You can use LinkedResource class of .NET Framework.

  

Represents an embedded external resource in an email attachment, such   as an image in an HTML attachment.

Alternatively, and more simply in my opinion, if you want to use some images in your email, put the image in a public location then just reference that location in the HTML of the email.

(by NASRINBronekSoner Gönül)

參考文件

  1. Best way for sending advance email (CC BY‑SA 3.0/4.0)

#ASP.NET #html-email #sendmail #smtp #C#






相關問題

System.Reflection.Assembly.LoadFile 鎖定文件 (System.Reflection.Assembly.LoadFile Locks File)

如何在沒有全局變量的情況下一直保留我的變量? (How can I keep my variable all the time without global variables?)

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)

無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

使用 ASP.Net 教初學者 Web 開發的小項目想法 (Small projects ideas to teach beginners web development using ASP.Net)

SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)

企業庫異常處理應用程序塊和日誌記錄應用程序塊在 ASP.NET 中的正確使用 (Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET)

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)







留言討論