日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#通过SMTP发送邮件代码示例

發布時間:2023/12/10 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#通过SMTP发送邮件代码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、新建SMTP.cs類庫文件

public class SMTP

? ? {

? ? ? ? /// <summary>

? ? ? ? /// SMTP服務器

? ? ? ? /// </summary>

? ? ? ? public string smtp { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// SMTP服務器端口

? ? ? ? /// </summary>

? ? ? ? public int port { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// 發件人

? ? ? ? /// </summary>

? ? ? ? public string from { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// 發件人密碼

? ? ? ? /// </summary>

? ? ? ? public string password { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// 郵件主題

? ? ? ? /// </summary>

? ? ? ? public string subject { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// 郵件主題

? ? ? ? /// </summary>

? ? ? ? public string body { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// 收件人郵箱

? ? ? ? /// </summary>

? ? ? ? public string strto { get; set; }

? ? ? ? /// <summary>

? ? ? ? /// 抄送郵箱

? ? ? ? /// </summary>

? ? ? ? public List<string>? ? strcc=new List<string>();

? ? ? ? /// <summary>

? ? ? ? /// 發送郵件

? ? ? ? /// </summary>

? ? ? ? public void SendMail()

? ? ? ? {

? ? ? ? ? ? SmtpClient client = new SmtpClient();

? ? ? ? ? ? client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式? ??

? ? ? ? ? ? client.Host = this.smtp;//郵件服務器

? ? ? ? ? ? client.UseDefaultCredentials = false;

? ? ? ? ? ? client.EnableSsl = true;

? ? ? ? ? ? client.Credentials = new System.Net.NetworkCredential(this.from, this.password);//用戶名、密碼

? ? ? ? ? ? client.Port = this.port;

? ? ? ? ? ? //client.EnableSsl = true;

? ? ? ??

? ? ? ? ? ? var msg = new MailMessage();

? ? ? ? ? ? msg.From = new MailAddress(this.from);

? ? ? ? ? ? msg.To.Add(strto);

? ? ? ? ? ? if (this.strcc!=null&& this.strcc.Count>0 )

? ? ? ? ? ? {

? ? ? ? ? ? ? ? foreach (string OneStrcc in strcc)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? msg.CC.Add(OneStrcc);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? ? ? msg.Subject = subject;//郵件標題? ?

? ? ? ? ? ? msg.Body = body;//郵件內容? ?

? ? ? ? ? ? msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼? ?

? ? ? ? ? ? msg.IsBodyHtml = true;//是否是HTML郵件? ?

? ? ? ? ? ? msg.Priority = MailPriority.High;//郵件優先級? ?


? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? client.Send(msg);

? ? ? ? ? ? }

? ? ? ? ? ? catch (SmtpException ex)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw ex;

? ? ? ? ? ? }

? ? ? ? }

? ? }

2、使用示例

try

? ? ? ? {

? ? ? ? ? ? var theSMTP = new SMTP

? ? ? ? ? ? {

? ? ? ? ? ? ? ? smtp = ConfigurationManager.AppSettings["smtp"],

? ? ? ? ? ? ? ? port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]),

? ? ? ? ? ? ? ? from = ConfigurationManager.AppSettings["from"],

? ? ? ? ? ? ? ? password = ConfigurationManager.AppSettings["password"],

? ? ? ? ? ? ? ? subject = "主題",

? ? ? ? ? ? ? ? body = "內容"

? ? ? ? ? ? };

? ? ? ? ? ? theSMTP.strto ="xxx@qq.com";

? ? ? ? ? ? theSMTP.strcc.Add("xxx@qq.com");

? ? ? ? ? ? theSMTP.SendMail();

? ? ? ? }

? ? ? ? catch (Exception ex)

? ? ? ? {


? ? ? ? }


總結

以上是生活随笔為你收集整理的C#通过SMTP发送邮件代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。