62,236
社区成员




public static bool ValidateServerCertificate(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
//发送邮件服务器
SmtpClient client = new SmtpClient();
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
public void SendMails()
{
MailAddress from = new MailAddress("jbj12@sina.com");
MailAddress to = new MailAddress("hd13@sina.com");
MailMessage objMailMessage = new MailMessage(from, to);
objMailMessage.Subject = "SMTP";
objMailMessage.Body = "邮件测试";
SmtpClient objSmtpClient = new SmtpClient("smtp.sina.com");
objSmtpClient.Credentials = new System.Net.NetworkCredential("jbj12", "密码");
objSmtpClient.Send(objMailMessage);
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="email.aspx.cs" Inherits="_email" ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 393px">
收信:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
主题:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
内容:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine"
Width="336px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="发送" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
<table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false">
<tr>
<td align="center" style="width: 400px">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="恭喜您,发表成功!"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text="返回" OnClick="Button2_Click" /></td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//倒入命名空间
using System.Net;
using System.Net.Mail;
public partial class _email : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
////设置发件人信箱,及显示名字
MailAddress from = new MailAddress("邮箱地址", "名字");
//设置收件人信箱,及显示名字
MailAddress to = new MailAddress("邮箱地址", "名字");
//创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to);
oMail.Subject = TextBox2.Text; //邮件标题
oMail.Body = TextBox3.Text; //邮件内容
oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高
//发送邮件服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com"; //指定邮件服务器
client.EnableSsl = true;
client.Port = 587; //端口要设置成587
client.Credentials = new NetworkCredential("邮箱用户名", "密码");//指定服务器邮件,及密码
//发送
client.Send(oMail); //发送邮件
Label1.Text = "恭喜你!邮件发送成功。";
oMail.Dispose(); //释放资源
TABLE1.Visible = false;
Table2.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
//返回,继续发送
Response.Redirect(Request.Url.ToString());
TABLE1.Visible = true;
Table2.Visible = false;
}
}