现在不用邮箱的账号+密码啦,现在用账号+第三方授权码(邮箱设置里有)
首先发件方要开启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 try 17 { 18 client.Send(message); 19 } 20 catch (InvalidOperationException iex) 21 { } 22 catch (Exception ex) 23 { }
Another one, you can add multiple recipients and cc's
Email email = new Email(); email.mailFrom = "Sender's Address"; email.mailSubject = "Mail Subject"; email.mailBody = "Email Body"; email.isbodyHtml = true; //是否是HTML email.host = "smtp.qq.com";//如果是QQ邮箱则:smtp:qq.com,依次类推 email.mailToArray = new string[] { "[email protected]", "[email protected]" };//接收者邮件集合 email.mailCcArray = new string[] { "[email protected]" };//抄送者邮件集合 if (email.Send()) { Response.Write("<script type='text/javascript'>alert('发送成功!');history.go(-1)</script>");//发送成功则提示返回当前页面; } else { Response.Write("<script type='text/javascript'>alert('发送失败!');history.go(-1)</script>"); } ----------------------------------------------------------------------------- 下面是封装好的类: public class Email { /// <summary> /// 发送者 /// </summary> public string mailFrom { get; set; } /// <summary> /// 收件人 /// </summary> public string[] mailToArray { get; set; } /// <summary> /// 抄送 /// </summary> public string[] mailCcArray { get; set; } /// <summary> /// 标题 /// </summary> public string mailSubject { get; set; } /// <summary> /// 正文 /// </summary> public string mailBody { get; set; } /// <summary> /// SMTP邮件服务器 /// </summary> public string host { get; set; } /// <summary> /// 正文是否是html格式 /// </summary> public bool isbodyHtml { get; set; } /// <summary> /// 附件 /// </summary> public string[] attachmentsPath { get; set; } public bool Send() { //使用指定的邮件地址初始化MailAddress实例 MailAddress maddr = new MailAddress(mailFrom); //初始化MailMessage实例 MailMessage myMail = new MailMessage(); //Add email address to recipient address collection if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //Add email addresses to the CC recipient address collection if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } // sender address myMail.From = maddr; // title of the email myMail.Subject = mailSubject; // The encoding used for the subject content of the email myMail.SubjectEncoding = Encoding.UTF8; // email body 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 /// <summary> 2 /// Core code for sending mail 3 /// </summary> 4 /// <param name="oFFromMail"> Sending email full address </param> 5 /// <param name ="oFFromMailPwd"> Send Mailbox </param> 6 /// <param name="oFDisName"> The associated display name associated with the mailbox </param> 7 /// <param name="toMail"> Recipient mailbox Full address </param> 8 /// <param name="oFSubject"> Subject </param> 9 /// <param name="oFBody">Email content </param> 10 /// <returns> Return whether the sending is successful </returns> 11 private void sendMailMethod ( string oFBody) 12 { 13 try 14 { 15 16 string FromMail = " [email protected] " ; // [email protected] 17 string toMail = " [email protected] " ; // Accepted email address [email protected] 18 // 1: Set server 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 = "您好啊,这是一封测试邮件的内容!<br />" + 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 }
这些代码,是来源于网络后做了部分修改的;
所以现在还还给网络..............
困。。。。。。明天还要上班。。。。。