摘要:C# 利用Gmail 寄信並夾帶檔案
01 SmtpClient MySmtp = new SmtpClient("smtp.gmail.com",587);
02 MySmtp.UseDefaultCredentials = false;
03 MySmtp.Credentials = new NetworkCredential("帳號@gmail.com", "密碼");//設定帳號密碼
04 MySmtp.EnableSsl = true; //smtp 是否使用 SSL
05
06 MailMessage mms = new MailMessage();
07 mms.IsBodyHtml = true;//內容是不是HTML
08 mms.From = new MailAddress("帳號@gmail.com", "Gmail寄test", Encoding.UTF8);
09 mms.Sender = new MailAddress("帳號@gmail.com ", " Gmail寄test", Encoding.UTF8);
10
11 mms.Subject = "通知回報郵件";//標題
12 mms.SubjectEncoding = Encoding.UTF8;
13 mms.To.Add(new MailAddress("帳號@hotmail.com", "test", Encoding.UTF8));//收件者
14
15 mms.Body = "測試"; //內容
16
17 DirectoryInfo thisdir = new DirectoryInfo(@"c:\a\");
18 FileInfo[] filelist = thisdir.GetFiles();
19 foreach (FileInfo dir in filelist)
20 {
21 mms.Attachments.Add(new Attachment(dir.FullName));
22 }
23 // 設定 DeliveryMethod 的傳送信件方法 (共有3種) 參考
24 //MySmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
25 //MySmtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
26 //MySmtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
27 MySmtp.Send(mms);//發送
02 MySmtp.UseDefaultCredentials = false;
03 MySmtp.Credentials = new NetworkCredential("帳號@gmail.com", "密碼");//設定帳號密碼
04 MySmtp.EnableSsl = true; //smtp 是否使用 SSL
05
06 MailMessage mms = new MailMessage();
07 mms.IsBodyHtml = true;//內容是不是HTML
08 mms.From = new MailAddress("帳號@gmail.com", "Gmail寄test", Encoding.UTF8);
09 mms.Sender = new MailAddress("帳號@gmail.com ", " Gmail寄test", Encoding.UTF8);
10
11 mms.Subject = "通知回報郵件";//標題
12 mms.SubjectEncoding = Encoding.UTF8;
13 mms.To.Add(new MailAddress("帳號@hotmail.com", "test", Encoding.UTF8));//收件者
14
15 mms.Body = "測試"; //內容
16
17 DirectoryInfo thisdir = new DirectoryInfo(@"c:\a\");
18 FileInfo[] filelist = thisdir.GetFiles();
19 foreach (FileInfo dir in filelist)
20 {
21 mms.Attachments.Add(new Attachment(dir.FullName));
22 }
23 // 設定 DeliveryMethod 的傳送信件方法 (共有3種) 參考
24 //MySmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
25 //MySmtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
26 //MySmtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
27 MySmtp.Send(mms);//發送
參考http://www.dotblogs.com.tw/nobel12/archive/2010/07/27/16834.aspx
大家一起加入blogads 賺零用錢!!