2013-05-04 115 views
4

我想通過asp.net發送電子郵件。我使用這個代碼,它工作正常。發送預先電子郵件的最佳方式

mail.From = new MailAddress("[email protected]", "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("[email protected]", "pass"); smtp.Send(mail); 

但我想要一個自定義和美麗的郵件。就像從Facebook,谷歌團隊等發送的電子郵件一樣。我知道可以在mail.Body中使用html標籤,但它是一種好方法嗎?什麼是最好的方法 ?

+0

是的,您應該使用HTML描述。我不確定你還期待什麼其他答案? – 2013-05-04 11:21:55

+0

我想用這樣的風格和形象。我認爲在c#代碼中使用大量的html和css標籤並不好。我可以創建單獨的html文件並附加它? – NASRIN 2013-05-04 11:27:25

+0

不,您必須將這些樣式包含在電子郵件本身中。有一個這樣的閱讀:http://css-tricks.com/using-css-in-html-emails-the-real-story/ – 2013-05-04 11:37:31

回答

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("[email protected]", "From Name"); 
     msg.Subject = "Subject"; 
     msg.To.Add("[email protected]"); 
     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 = "[email protected]"; 
     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("[email protected]", 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"/> 
+0

感謝您的回答,我想要的這段代碼... 我測試了它,它運行良好,但是當我想在郵件的頂部使用圖片時,在gmail中它只附加它並在yahoo郵件中附加並在底部添加圖片的郵件。 爲什麼我的郵件沒有在正確的位置添加此圖片? – NASRIN 2013-05-05 06:05:23

+0

我認爲這是你的模板的問題。我剛剛測試過這段代碼,發送到Gmail賬戶,結果是圖像被放置在正確的位置(我改變了它的位置)。在我的模板中,img標籤被放到html表格中,以便定位它(舊的方式,而不是css)。我會提到LinkedResource將圖像嵌入到電子郵件中(而不是通過外部URL)。 – Bronek 2013-05-05 10:36:31

1

我想要一個自定義和漂亮的郵件。

我知道可以在mail.Body中使用html標籤,但它是好方法嗎?什麼是 最好的方法?

我不知道究竟那是什麼意思,但通常有兩種方法來做到這一點。 (如果我們談論電子郵件中的圖像或源代碼等)。

您可以使用.NET Framework的類LinkedResource

表示電子郵件附件中的嵌入式外部資源,如 作爲HTML附件中的圖像。

或者,更簡單地在我看來,如果你想在你的電子郵件使用一些圖片,把圖像中的公共位置,然後只是引用電子郵件的HTML該位置。

相關問題