阅读全文并下载实例:http://www.cckan.net/forum.php?mod=viewthread&tid=56
这个例子很简单,初学者可以看一下,写的很明白,注释都加了大家看一下就行了, 呵呵
实现的方法如下:
Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
MailMessage objMail = new MailMessage();
//发件人地址
objMail.From = new MailAddress(txtForm.Text.ToString().Trim());
//邮件标题
objMail.Subject = txttitle.Text.ToString().Trim();
//邮件标题编码
objMail.SubjectEncoding = System.Text.Encoding.UTF8;
//邮件内容
objMail.Body = txtBody.Text.ToString().Trim();
//邮件内容编码
objMail.BodyEncoding = System.Text.Encoding.UTF8;
//收件人地址在这里可以加多个
objMail.To.Add(txtTO.Text.ToString().Trim());
//用SMTP发送邮件的方式
SmtpClient client = new SmtpClient();
//用户名和密码
client.Credentials = new System.Net.NetworkCredential(txtForm.Text.ToString().Trim(), txtPwd.Text.ToString().Trim());
//服务器名
client.Host = "SMTP.163.com";
//发送
client.Send(objMail);
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "sufei", "发送成功");
}
catch (Exception ex)
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "sufei", ex.Message.ToString().Trim());
}
}
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
MailMessage objMail = new MailMessage();
//发件人地址
objMail.From = new MailAddress(txtForm.Text.ToString().Trim());
//邮件标题
objMail.Subject = txttitle.Text.ToString().Trim();
//邮件标题编码
objMail.SubjectEncoding = System.Text.Encoding.UTF8;
//邮件内容
objMail.Body = txtBody.Text.ToString().Trim();
//邮件内容编码
objMail.BodyEncoding = System.Text.Encoding.UTF8;
//收件人地址在这里可以加多个
objMail.To.Add(txtTO.Text.ToString().Trim());
//用SMTP发送邮件的方式
SmtpClient client = new SmtpClient();
//用户名和密码
client.Credentials = new System.Net.NetworkCredential(txtForm.Text.ToString().Trim(), txtPwd.Text.ToString().Trim());
//服务器名
client.Host = "SMTP.163.com";
//发送
client.Send(objMail);
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "sufei", "发送成功");
}
catch (Exception ex)
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "sufei", ex.Message.ToString().Trim());
}
}
}
也可以写在方法里面,这样用起来方便一点点
Code
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="MailFromAddress">发件人地址</param>
/// <param name="MailFromUserName">用户名</param>
/// <param name="MailFormPwd">密码</param>
/// <param name="MailTitle">邮件标题</param>
/// <param name="MailBody">邮件内容</param>
/// <param name="MailTo">收件人地址</param>
/// <returns>发送成功返回True否则返回False</returns>
public static Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo)
{
try
{
MailMessage objMail = new MailMessage();
//发件人地址
objMail.From = new MailAddress(MailFromAddress);
//邮件标题
objMail.Subject = MailTitle;
//邮件标题编码
objMail.SubjectEncoding = System.Text.Encoding.UTF8;
//邮件内容
objMail.Body = MailBody;
//邮件内容编码
objMail.BodyEncoding = System.Text.Encoding.UTF8;
//收件人地址在这里可以加多个
objMail.To.Add(MailTo);
//用SMTP发送邮件的方式
SmtpClient client = new SmtpClient();
//用户名和密码
client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd);
string StrMail = "";
for (int i = 0; i < MailFromAddress.Length; i++)
{
if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@")
{
StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1);
}
}
//服务器名
client.Host = "smtp." + StrMail.ToString().Trim();
//发送
client.Send(objMail);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="MailFromAddress">发件人地址</param>
/// <param name="MailFromUserName">用户名</param>
/// <param name="MailFormPwd">密码</param>
/// <param name="MailTitle">邮件标题</param>
/// <param name="MailBody">邮件内容</param>
/// <param name="MailTo">收件人地址</param>
/// <returns>发送成功返回True否则返回False</returns>
public static Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo)
{
try
{
MailMessage objMail = new MailMessage();
//发件人地址
objMail.From = new MailAddress(MailFromAddress);
//邮件标题
objMail.Subject = MailTitle;
//邮件标题编码
objMail.SubjectEncoding = System.Text.Encoding.UTF8;
//邮件内容
objMail.Body = MailBody;
//邮件内容编码
objMail.BodyEncoding = System.Text.Encoding.UTF8;
//收件人地址在这里可以加多个
objMail.To.Add(MailTo);
//用SMTP发送邮件的方式
SmtpClient client = new SmtpClient();
//用户名和密码
client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd);
string StrMail = "";
for (int i = 0; i < MailFromAddress.Length; i++)
{
if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@")
{
StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1);
}
}
//服务器名
client.Host = "smtp." + StrMail.ToString().Trim();
//发送
client.Send(objMail);
return true;
}
catch (Exception)
{
return false;
}
}
使用方法
Code
try
{
if (MessageSendServices.SendMail(txtFrom.Text.ToString().Trim(), txtFromPwd.Text.ToString().Trim(), txtTitle.Text.ToString().Trim(), txtNeiRong.Text.ToString().Trim(), txtTo.Text.ToString().Trim()))
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "one", "<span style='color:red; font-size: 18px;'>" + "邮件发送成功" + "</span>");
}
else
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "one", "<span style='color:red; font-size: 18px;'>" + "邮件发送失败" + "</span>");
}
}
catch (Exception)
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "one", "<span style='color:red; font-size: 18px;'>" + "下发时发生异常信息" + "</span>");
}
try
{
if (MessageSendServices.SendMail(txtFrom.Text.ToString().Trim(), txtFromPwd.Text.ToString().Trim(), txtTitle.Text.ToString().Trim(), txtNeiRong.Text.ToString().Trim(), txtTo.Text.ToString().Trim()))
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "one", "<span style='color:red; font-size: 18px;'>" + "邮件发送成功" + "</span>");
}
else
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "one", "<span style='color:red; font-size: 18px;'>" + "邮件发送失败" + "</span>");
}
}
catch (Exception)
{
ClientScriptManager csm = this.ClientScript;
csm.RegisterStartupScript(this.GetType(), "one", "<span style='color:red; font-size: 18px;'>" + "下发时发生异常信息" + "</span>");
}