SendEmail
///?? <summary>
??????????????? ///?? 發送郵件
??????????????? ///?? </summary>
??????????????? ///?? <param?? name= "strSmtpServer "> smtp地址 </param>
??????????????? ///?? <param?? name= "UserName "> 用戶名 </param>
??????????????? ///?? <param?? name= "Password "> 密碼 </param>
??????????????? ///?? <param?? name= "strFrom "> 發信人地址 </param>
??????????????? ///?? <param?? name= "strto "> 收信人地址 </param>
??????????????? ///?? <param?? name= "strSubject "> 郵件標題 </param>
??????????????? ///?? <param?? name= "strBody "> 郵件正文 </param>
??????????????? public?? static?? void?? SendMail(string?? strSmtpServer,?? string?? UserName,?? string?? Password,?? string?? strFrom,?? string?? strto,?? string?? strSubject,?? string?? strBody,?? string?? strFileName)
??????????????? {
??????????????????????? //生成一個?? 使用SMTP發送郵件的客戶端對象
??????????????????????? System.Net.Mail.SmtpClient?? client?? =?? new?? System.Net.Mail.SmtpClient(strSmtpServer);
??????????????????????? //表示以當前登錄用戶的默認憑據進行身份驗證
??????????????????????? client.UseDefaultCredentials?? =?? true;
?????????
??????????????????????? //包含用戶名和密碼
??????????????????????? client.Credentials?? =?? new?? System.Net.NetworkCredential(UserName,?? Password);
????????????????????????? //指定如何發送電子郵件。
????????????????????????? //Network???????????????????????????????????????????? 電子郵件通過網絡發送到?? SMTP?? 服務器。????
????????????????????????? //PickupDirectoryFromIis?????????????? 將電子郵件復制到挑選目錄,然后通過本地?? Internet?? 信息服務?? (IIS)?? 傳送。????
????????????????????????? //SpecifiedPickupDirectory?????????? 將電子郵件復制到?? SmtpClient.PickupDirectoryLocation?? 屬性指定的目錄,然后由外部應用程序傳送。????
??????????????????????? client.DeliveryMethod?? =?? System.Net.Mail.SmtpDeliveryMethod.Network;
??????????????????????? //建立郵件對象??
??????????????????????? System.Net.Mail.MailMessage?? message?? =?? new?? System.Net.Mail.MailMessage(strFrom,?? strto,?? strSubject,strBody);
???????????????????????
??????????????????????? //定義郵件正文,主題的編碼方式
??????????????????????? message.BodyEncoding?? =?? System.Text.Encoding.GetEncoding( "gb2312 ");
??????????????????????? message.SubjectEncoding?? =?? System.Text.Encoding.GetEncoding( "gb2312 ");
?????????????????????
??????????????????????? //獲取或設置一個值,該值指示電子郵件正文是否為?? HTML。??
??????????????????????? message.IsBodyHtml?? =?? false;
???????????????????????
??????????????????????? //指定郵件優先級
?????????
??????????????????????? message.Priority?? =?? System.Net.Mail.MailPriority.Normal;
??????????????????????? //添加附件
??????????????????????? //System.Web.Mail.MailAttachment?? mailAttachment=new?? System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");??
??????????????????????? if?? (strFileName?? !=?? " "?? &&?? strFileName?? !=?? null)
??????????????????????? {
??????????????????????????????? Attachment?? data?? =?? new?? Attachment(strFileName);
??????????????????????????????? message.Attachments.Add(data);
??????????????????????? }
???????????????????
???????????????????????
??????????????????????? //發件人身份驗證,否則163?? 發不了
??????????????????????? client.Credentials?? =???? new?? System.Net.NetworkCredential(strFrom,?? Password);
??????????????????????? //發送
??????????????????????? client.Send(message);
??????????????? }
??????? }
??????????????? ///?? 發送郵件
??????????????? ///?? </summary>
??????????????? ///?? <param?? name= "strSmtpServer "> smtp地址 </param>
??????????????? ///?? <param?? name= "UserName "> 用戶名 </param>
??????????????? ///?? <param?? name= "Password "> 密碼 </param>
??????????????? ///?? <param?? name= "strFrom "> 發信人地址 </param>
??????????????? ///?? <param?? name= "strto "> 收信人地址 </param>
??????????????? ///?? <param?? name= "strSubject "> 郵件標題 </param>
??????????????? ///?? <param?? name= "strBody "> 郵件正文 </param>
??????????????? public?? static?? void?? SendMail(string?? strSmtpServer,?? string?? UserName,?? string?? Password,?? string?? strFrom,?? string?? strto,?? string?? strSubject,?? string?? strBody,?? string?? strFileName)
??????????????? {
??????????????????????? //生成一個?? 使用SMTP發送郵件的客戶端對象
??????????????????????? System.Net.Mail.SmtpClient?? client?? =?? new?? System.Net.Mail.SmtpClient(strSmtpServer);
??????????????????????? //表示以當前登錄用戶的默認憑據進行身份驗證
??????????????????????? client.UseDefaultCredentials?? =?? true;
?????????
??????????????????????? //包含用戶名和密碼
??????????????????????? client.Credentials?? =?? new?? System.Net.NetworkCredential(UserName,?? Password);
????????????????????????? //指定如何發送電子郵件。
????????????????????????? //Network???????????????????????????????????????????? 電子郵件通過網絡發送到?? SMTP?? 服務器。????
????????????????????????? //PickupDirectoryFromIis?????????????? 將電子郵件復制到挑選目錄,然后通過本地?? Internet?? 信息服務?? (IIS)?? 傳送。????
????????????????????????? //SpecifiedPickupDirectory?????????? 將電子郵件復制到?? SmtpClient.PickupDirectoryLocation?? 屬性指定的目錄,然后由外部應用程序傳送。????
??????????????????????? client.DeliveryMethod?? =?? System.Net.Mail.SmtpDeliveryMethod.Network;
??????????????????????? //建立郵件對象??
??????????????????????? System.Net.Mail.MailMessage?? message?? =?? new?? System.Net.Mail.MailMessage(strFrom,?? strto,?? strSubject,strBody);
???????????????????????
??????????????????????? //定義郵件正文,主題的編碼方式
??????????????????????? message.BodyEncoding?? =?? System.Text.Encoding.GetEncoding( "gb2312 ");
??????????????????????? message.SubjectEncoding?? =?? System.Text.Encoding.GetEncoding( "gb2312 ");
?????????????????????
??????????????????????? //獲取或設置一個值,該值指示電子郵件正文是否為?? HTML。??
??????????????????????? message.IsBodyHtml?? =?? false;
???????????????????????
??????????????????????? //指定郵件優先級
?????????
??????????????????????? message.Priority?? =?? System.Net.Mail.MailPriority.Normal;
??????????????????????? //添加附件
??????????????????????? //System.Web.Mail.MailAttachment?? mailAttachment=new?? System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");??
??????????????????????? if?? (strFileName?? !=?? " "?? &&?? strFileName?? !=?? null)
??????????????????????? {
??????????????????????????????? Attachment?? data?? =?? new?? Attachment(strFileName);
??????????????????????????????? message.Attachments.Add(data);
??????????????????????? }
???????????????????
???????????????????????
??????????????????????? //發件人身份驗證,否則163?? 發不了
??????????????????????? client.Credentials?? =???? new?? System.Net.NetworkCredential(strFrom,?? Password);
??????????????????????? //發送
??????????????????????? client.Send(message);
??????????????? }
??????? }
轉載于:https://www.cnblogs.com/yanglin/archive/2008/04/12/1150301.html
總結