发送邮件代码:
注意修改发信人地址。发信人。发信人邮箱用户名。发信人邮箱密码
前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
收信人邮箱:<asp:TextBox ID="txtSender" runat="server"></asp:TextBox><br />
标题:<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox><br />
主题:<asp:TextBox ID="txtContent" runat="server"></asp:TextBox><br />
<asp:Button ID="btnOk" runat="server" Text="Button" οnclick="btnOk_Click" />
</div>
</form>
</body>
</html>
后台代码:
protected void btnOk_Click(object sender, EventArgs e)
{
try
{
//编码暂硬性规定为GB2312
Encoding encoding = Encoding.GetEncoding(936);
System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(new MailAddress("发件人邮箱", "发件人", encoding),//第一个是发信人的地址,第二个参数是发信人
new MailAddress(txtSender.Text));//收信人邮箱
Message.SubjectEncoding = encoding;
Message.Subject = txtTitle.Text;//标题
Message.BodyEncoding = encoding;
Message.Body = txtContent.Text; //主体
SmtpClient smtpClient = new SmtpClient("smtp.163.com");//信箱服务器
smtpClient.Credentials = new NetworkCredential("发信人邮箱用户名", "发信人密码");//信箱的用户名和密码
smtpClient.Timeout = 999999;
smtpClient.Send(Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}