using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class Cemail : System.Web.UI.Page { private string sfrom = "111111@qq.com";//发送人邮箱 private string sfromer = "发送人"; private string stoer = "收件人"; private string sSMTPHost = "smtp.qq.com";//SMTP服务器 private string sSMTPuser = "111111@qq.com";//用户登录名 private string sSMTPpass = "授权码";//登陆密码-新版之后的QQ邮箱都是使用授权码,需要到邮箱-设置-账户里面找到-生成授权码-复制进来; protected void Page_Load(object sender, EventArgs e) { } protected void btnSend_Click(object sender, EventArgs e) { string sto = txtSto.Text.Trim(); string cc = txtCC.Text.Trim(); string title = txtTitle.Text.Trim(); string content = txtContent.Text.Trim(); if (string.IsNullOrEmpty(sto) || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content)) { Response.Write("<script>alert('收件人,标题,内容不能为空')</script>"); } else { MailAddress from = new MailAddress(sfrom, sfromer);//发件人地址对象 MailAddress to = new MailAddress(sto, stoer);//收件人地址对象 MailMessage oMail = new MailMessage(from, to); oMail.Subject = title;//设置邮件标题 oMail.Body = content;//设置邮件文本内容 oMail.IsBodyHtml = false;//设置为HTML格式 oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//正文编码 oMail.Priority = MailPriority.High;//优先级 if (cc != "") { if (cc.ToLower().IndexOf(';') > 0) { cc = cc.Substring(0, cc.Length - 1); string[] acc = cc.Split(';'); foreach (var c in acc) { oMail.CC.Add(c); } } else { oMail.CC.Add(cc); } } SmtpClient client=new SmtpClient(); client.EnableSsl = true;//由于使用了授权码必须设置该属性为true client.Host = sSMTPHost;//指定SMTP服务器 client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);//邮箱的用户名和密码,注意使用qq邮箱时密码使用的是授权码 client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 try { client.Send(oMail); Response.Write("<script>alert('发送成功')</script>"); } catch { Response.Write("<script>alert('发送失败')</script>"); } finally { oMail.Dispose(); } } } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Cemail.aspx.cs" Inherits="WebApplication1.Cemail" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>收件人邮箱</td> <td> <asp:TextBox ID="txtSto" runat="server"></asp:TextBox></td> </tr> <tr> <td>抄送</td> <td> <asp:TextBox ID="txtCC" runat="server"></asp:TextBox> </td> </tr> <tr> <td>标题</td> <td> <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td> </tr> <tr> <td>内容</td> <td> <asp:TextBox ID="txtContent" runat="server" Height="136px" TextMode="MultiLine" Width="254px"></asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="Button1" runat="server" Text="重置" /></td> <td> <asp:Button ID="btnSend" runat="server" Text="提交" OnClick="btnSend_Click" /></td> </tr> </table> </div> </form> </body> </html>
qq邮箱设置教程
----到这里就ok了,其实主要和像sohu之类邮箱发送邮件不同的是
一:在代码里面设置SmtpClient的EnableSsl属性为true
二:设置QQ邮箱账户中的POP3/SMTP服务,另外密码也是要使用授权码