发邮件
页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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> 收件人:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br /> 标题:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br /> 内容:<br /> <asp:TextBox ID="TextBox3" runat="server" Height="206px" TextMode="MultiLine" Width="248px"></asp:TextBox><br /><br /> <asp:Button ID="Button1" runat="server" Text="发送" /> </div> </form> </body> </html>
后台
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click;//发送按钮 } //发送按钮开始 void Button1_Click(object sender, EventArgs e) { //1、引用命名空间using System.Net; using System.Net.Mail; //创建SMTP服务调用类 SmtpClient smtp = new SmtpClient("smtp.qq.com"); smtp.EnableSsl = true; //发件人 MailAddress come=new MailAddress("账户"); //收件人 MailAddress to=new MailAddress(TextBox1.Text); //创建邮件对象 MailMessage mail=new MailMessage(come,to); //邮件对象的内容 mail.Subject=TextBox2.Text;//邮件标题 mail.Body=TextBox3.Text;//邮件主体 //注册证书,验证发件人邮箱密码 NetworkCredential net = new NetworkCredential("账户", "密码"); //将证书关联到服务器对象等待验证 smtp.Credentials = net; //调用服务类 smtp.Send(mail); } //发送按钮结束 }