完整程序:
有時候需要系統發一些帶圖片的郵件給用戶,一般的做法是將郵件中的圖版放到公網上,然後在HTML郵件中指定絕對地址.但這樣做有一些bug,就是outlook2003等其他一些客戶端會禁止你看這些圖片.
現在最好的做法是將圖版放在郵件中,發送一個帶圖的HTML郵件給客戶.
下面是 .NET 發郵件中的關鍵代碼.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace cutEmpAccount
{
class EmailSimple
{
private static string _content = @"<p>系統維護人員:<br />
<img src='cid:[attid]' />
取消帳號出錯,請跟進。</p>";
public static void sentEmail(string empId, string name, string deptCode, string deptName)
{
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = false;
smtp.Host = "smtp.extjs.org.cn";
smtp.Port = 25;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential("fatjames", "fatjames");
MailMessage em = new MailMessage();
em.Subject = "停帳號出錯通知";
em.SubjectEncoding = Encoding.GetEncoding(936);
em.From = new MailAddress("[email protected]", "停帳號系統",Encoding.GetEncoding(936));
em.To.Add(System.Configuration.ConfigurationSettings.AppSettings["ErrEmail"]);
em.IsBodyHtml = true;
em.BodyEncoding = Encoding.GetEncoding(936);
string fileName = System.IO.Directory.GetCurrentDirectory().ToString() + "//PA.JPG";
LinkedResource lrImage = new LinkedResource(fileName, "image/jpeg");
icontent = icontent.Replace("[attid]", lrImage.ContentId);
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(icontent, null, "text/html");
htmlBody.LinkedResources.Add(lrImage);
em.AlternateViews.Add(htmlBody);
smtp.Send(em);
}
}
}
|