现在不用邮箱的账号+密码啦,现在用账号+第三方授权码(邮箱设置里有)
首先发件方要开启qq邮箱里的什么stmp和pop3的服务(反正是前2个)
先来第一个:比较简单的
1 SmtpClient client = new SmtpClient("smtp.qq.com"); 2 client.EnableSsl = true; 3 client.UseDefaultCredentials = false; 4 client.Credentials = new System.Net.NetworkCredential("发件人邮箱地址", "第三方授权码"); 5 MailAddress from = new MailAddress("发件人邮箱地址", "来自二大爷的快递", Encoding.UTF8);//初始化发件人 6 MailAddress to = new MailAddress("收件人邮箱地址", "", Encoding.UTF8);//初始化收件人 ;收件人不分是qq还是136邮箱 7 //设置邮件内容 8 MailMessage message = new MailMessage(from, to); 9 message.Body = "邮件内容";10 message.BodyEncoding = System.Text.Encoding.UTF8;11 message.Subject = "标题";12 message.SubjectEncoding = System.Text.Encoding.UTF8;13 message.IsBodyHtml = false;14 15 //发送邮件 16 try17 {18 client.Send(message);19 }20 catch (InvalidOperationException iex)21 { }22 catch (Exception ex)23 { }
再来一个,可以添加多个收件人和抄送人的
Email email = new Email(); email.mailFrom = "发送人地址"; email.mailSubject = "邮件主题"; email.mailBody = "邮件内容"; email.isbodyHtml = true; //是否是HTML email.host = "smtp.qq.com";//如果是QQ邮箱则:smtp:qq.com,依次类推 email.mailToArray = new string[] { "XXOO@qq.com", "OOXX@163.com" };//接收者邮件集合 email.mailCcArray = new string[] { "PaPa@qq.com" };//抄送者邮件集合 if (email.Send()) { Response.Write("");//发送成功则提示返回当前页面; } else { Response.Write(""); }-----------------------------------------------------------------------------下面是封装好的类: public class Email { ////// 发送者 /// public string mailFrom { get; set; } ////// 收件人 /// public string[] mailToArray { get; set; } ////// 抄送 /// public string[] mailCcArray { get; set; } ////// 标题 /// public string mailSubject { get; set; } ////// 正文 /// public string mailBody { get; set; } ////// SMTP邮件服务器 /// public string host { get; set; } ////// 正文是否是html格式 /// public bool isbodyHtml { get; set; } ////// 附件 /// public string[] attachmentsPath { get; set; } public bool Send() { //使用指定的邮件地址初始化MailAddress实例 MailAddress maddr = new MailAddress(mailFrom); //初始化MailMessage实例 MailMessage myMail = new MailMessage(); //向收件人地址集合添加邮件地址 if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //向抄送收件人地址集合添加邮件地址 if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } //发件人地址 myMail.From = maddr; //电子邮件的标题 myMail.Subject = mailSubject; //电子邮件的主题内容使用的编码 myMail.SubjectEncoding = Encoding.UTF8; //电子邮件正文 myMail.Body = mailBody; //电子邮件正文的编码 myMail.BodyEncoding = Encoding.Default; myMail.Priority = MailPriority.High; myMail.IsBodyHtml = isbodyHtml; //在有附件的情况下添加附件 try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("在添加附件时有错误:" + err); } SmtpClient client = new SmtpClient(); //设置SMTP邮件服务器--看好下面的这三个顺序:UseDefaultCredentials会造成啥顺序报错 client.Host = host; client.EnableSsl = true; client.UseDefaultCredentials = false; //指定发件人的邮件地址和密码以验证发件人身份 client.Credentials = new System.Net.NetworkCredential(mailFrom, "邮箱设置里的授权码"); //client.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd); try { //将邮件发送到SMTP邮件服务器 client.Send(myMail); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } }
1 ///2 /// 发送邮件核心代码 3 /// 4 /// 发送邮箱全地址 5 /// 发送邮箱 6 /// 与邮箱关联的关联显示名 7 /// 收件人邮箱全地址 8 /// 邮件标题 9 /// 邮件内容10 ///返回是否发送成功 11 private void sendMailMethod(string oFBody)12 {13 try14 {15 16 string FromMail = "发送人@qq.com";//123456@qq.com17 string toMail = "收件人@163.com";//接受的邮件地址 123321@qq.com18 //1:设置服务器19 SmtpClient sendmsg = new SmtpClient("smtp.qq.com");20 sendmsg.EnableSsl = true;21 sendmsg.UseDefaultCredentials = false;22 sendmsg.Credentials = new System.Net.NetworkCredential("发送人@qq.com", "授权码");23 MailAddress from = new MailAddress(FromMail, "FromProzkb", Encoding.UTF8);//初始化发件人 24 MailAddress to = new MailAddress(toMail, "", Encoding.UTF8);//初始化收件人25 //设置邮件26 MailMessage mailmsg = new MailMessage(from,to);27 mailmsg.Subject = "OjbkTitle";28 mailmsg.Body = "您好啊,这是一封测试邮件的内容!" + oFBody;29 mailmsg.BodyEncoding = System.Text.Encoding.UTF8;30 mailmsg.IsBodyHtml = true;31 32 sendmsg.Send(mailmsg);33 34 }35 catch (Exception ex)36 {37 throw new Exception(ex.Message, ex);38 }39 }
这些代码,是来源于网络后做了部分修改的;
所以现在还还给网络..............
困。。。。。。明天还要上班。。。。。