WebForm 发送邮箱
首先在设置发件邮箱的SMTP服务,以新浪邮箱为例:设置区----客户端pop/imap/smtp----“POP3/SMTP服务”和“IMAP4服务/SMTP服务”设置为启动。
引用using System.Net; using System.Net.Mail;两大类
方法:用SmtpClient的send方法,可以倒推出来。
页面端:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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> <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>
代码区:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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) { string recipient = TextBox1.Text;//收件人 string title=TextBox2.Text;//邮箱标题 string content = TextBox3.Text;//邮箱内容 SmtpClient smtp = new SmtpClient("smtp.sina.cn");//创建发送邮件的客户端对象 //创建MailAddress对象,分别构建成发送者和接收者 MailAddress from = new MailAddress("15169205610m@sina.cn");//设定发件人 MailAddress to = new MailAddress(recipient);//设定收件人 MailMessage mess = new MailMessage(from,to);//创建对象出来,发现里面需要两个MailAddress对象,分别是发送者和接受者 mess.Subject = title; //填充邮件标题行 mess.Body = content; //填充邮件内容 NetworkCredential cred = new NetworkCredential("15169205610m@sina.cn", "这个邮箱的密码");//创建发送者的验证证书 smtp.Credentials = cred;//邮件客户端证书设置 Credentials中的提示有错误,没法倒推出来,直接记住 smtp.Send(mess);//调用客户端对象的发送方法,看到里面需要一个MailMessage对象 } }
完!!!