<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
程式碼案例一:
private void button1_Click(object sender, EventArgs e) { string myMaillAdress = ""; string myMaillPassword = ""; string myMaillMessage = ""; string toMaillAdress = ""; QQEmail qq_mial = new QQEmail(); bool send_status = qq_mial.IsSendEmail(myMaillAdress, toMaillAdress, myMaillMessage, myMaillPassword); } class QQEmail { public bool IsSendEmail(string FromAddress, string ToAddress, string Message, string Pwd) { MailAddress Address = new MailAddress(FromAddress); MailMessage mail = new MailMessage(); mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Subject = "這是" + FromAddress + "傳送的一段資訊:"; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.Body = Message; mail.IsBodyHtml = true; mail.Priority = System.Net.Mail.MailPriority.Low; mail.To.Add(ToAddress); mail.From = Address; SmtpClient smtp = new SmtpClient("smtp.qq.com", 25);//smtp支援的伺服器是smtp.qq.com,伺服器埠是25,587也行 smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential(FromAddress, Pwd);//切記這兩個資料一定要填對 try { smtp.Send(mail); return true; } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; } } }
程式碼案例二:
using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; namespace BLL { public class emailHandle { private string _serviceType = "SMTP"; private string _host; /// <summary> /// 傳送者郵箱 /// </summary> public string From { get; set; } /// <summary> /// 接收者郵箱列表 /// </summary> public List<string> To { get; set; } /// <summary> /// 抄送者郵箱列表 /// </summary> public string[] Cc { get; set; } /// <summary> /// 祕抄者郵箱列表 /// </summary> public string[] Bcc { get; set; } /// <summary> /// 郵件主題 /// </summary> public string Subject { get; set; } /// <summary> /// 郵件內容 /// </summary> public string Body { get; set; } /// <summary> /// 是否是HTML格式 /// </summary> public bool IsBodyHtml { get; set; } /// <summary> /// 附件列表 /// </summary> public string[] Attachments { get; set; } /// <summary> /// 郵箱服務型別(Pop3,SMTP,IMAP,MAIL等),預設為SMTP /// </summary> public string ServiceType { get { return _serviceType; } set { _serviceType = value; } } /// <summary> /// 郵箱伺服器,如果沒有定義郵箱伺服器,則根據serviceType和Sender組成郵箱伺服器 /// </summary> public string Host { get { return _host; } set { _host = value; } } /// <summary> /// 郵箱賬號(預設為傳送者郵箱的賬號) /// </summary> public string UserName { get; set; } /// <summary> /// 郵箱密碼(預設為傳送者郵箱的密碼),預設格式GB2312 /// </summary> public string Password { get; set; } /// <summary> /// 郵箱優先順序 /// </summary> public MailPriority MailPriority { get; set; } /// <summary> /// 郵件正文編碼格式 /// </summary> public Encoding Encoding { get; set; } /// <summary> /// 構造引數,傳送郵件,使用方法備註:公開方法中呼叫 /// </summary> public int Send() { var mailMessage = new MailMessage(); //讀取To 接收者郵箱列表 try { if (this.To != null && this.To.Count > 0) { foreach (string to in this.To) { if (string.IsNullOrEmpty(to)) continue; mailMessage.To.Add(new MailAddress(to.Trim())); } } //讀取Cc 抄送者郵件地址 if (this.Cc != null && this.Cc.Length > 0) { foreach (var cc in this.Cc) { if (string.IsNullOrEmpty(cc)) continue; mailMessage.CC.Add(new MailAddress(cc.Trim())); } } //讀取Attachments 郵件附件 if (this.Attachments != null && this.Attachments.Length > 0) { foreach (var attachment in this.Attachments) { if (string.IsNullOrEmpty(attachment)) continue; mailMessage.Attachments.Add(new Attachment(attachment)); } } //讀取Bcc 祕抄人地址 if (this.Bcc != null && this.Bcc.Length > 0) { foreach (var bcc in this.Bcc) { if (string.IsNullOrEmpty(bcc)) continue; mailMessage.Bcc.Add(new MailAddress(bcc.Trim())); } } //讀取From 傳送人地址 mailMessage.From = new MailAddress(this.From); //郵件標題 Encoding encoding = Encoding.GetEncoding("GB2312"); mailMessage.Subject = this.Subject; //郵件正文是否為HTML格式 mailMessage.IsBodyHtml = this.IsBodyHtml; //郵件正文 mailMessage.Body = this.Body; mailMessage.BodyEncoding = this.Encoding; //郵件優先順序 mailMessage.Priority = this.MailPriority; //傳送郵件程式碼實現 var smtpClient = new SmtpClient { Host = this.Host, EnableSsl = true, Credentials = new NetworkCredential(this.UserName, this.Password) }; //加這段之前用公司郵箱傳送報錯:根據驗證過程,遠端證書無效 //加上後解決問題 ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }; //認證 smtpClient.Send(mailMessage); return 1; } catch (Exception ex) { var loger = LogManager.GetLogger(typeof(emailHandle)); loger.Info(string.Format("傳送郵件異常,收信郵箱:{0}", this.To[0]), ex); return -1; } } } }
到此這篇關於C#呼叫QQ_Mail傳送郵件的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45