发送邮件
<%@ Page Language="C#" Debug=true 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> <h1>发送邮件</h1> 收件人:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br /> 标 题:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br /> 内 容:<asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></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) { //创建外部的smtp服务客户端对象 SmtpClient smtp = new SmtpClient("smtp.sina.cn");
//方法1:
//创建发信人对象
//MailAddress fr = new MailAddress("18560812711@sina.cn");
//创建收信人对象
//MailAddress to = new MailAddress(TextBox1.Text.Trim());
//创建邮件对象
//MailMessage mess = new MailMessage(fr,to);
//mess.Subject = TextBox2.Text.Trim();
//mess.Body = TextBox3.Text.Trim();
//创建证书对象 NetworkCredential cred = new NetworkCredential("18560812711@sina.cn", "hq1234561"); //服务器证书指向 smtp.Credentials = cred; //将邮件发送到指定服务器
//方法1:
//smtp.Send(mess);
smtp.Send("18560812711@sina.cn", TextBox1.Text.Trim(), TextBox2.Text.Trim(), TextBox3.Text); } }