smtp 发信类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace Niunan.Shop.Utility
{
/// <summary>邮件发送类
///
/// </summary>
public class email
{
//using System.Net.Mail;
// using System.Net;
/// <summary>发送email,默认是25端口.用法:SendMail("邮件标题", "你的密码是:" , 收件人地址, "发信人完整邮箱地址", "发信人完整邮箱地址", "密码", "smtp.qq.com(QQ的smtp地址,不同的域名邮箱地址不通)")
///
/// </summary>
/// <param name="title">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="toAdress">收件人</param>
/// <param name="fromAdress">发件人</param>
/// <param name="userName">发件用户名</param>
/// <param name="userPwd">发件密码</param>
/// <param name="smtpHost">smtp地址</param>
public static void SendMail(string title, string body, string toAdress, string fromAdress,
string userName, string userPwd, string smtpHost)
{
try
{
MailAddress to = new MailAddress(toAdress);
MailAddress from = new MailAddress(fromAdress);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.IsBodyHtml = true; // 如果不加上这句那发送的邮件内容中有HTML会原样输出
message.Subject = title; message.Body = body;
SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = true;
smtp.Port = 25;
smtp.Credentials = new NetworkCredential(userName, userPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = smtpHost;
//message.To.Add(toAdress); //用 To.Add方法能增加收件箱的号码
smtp.Send(message);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
使用方法: 点击获取密码之后
protected void btnGet_Click(object sender, EventArgs e) { string username = Request.QueryString["username"]; string answer = txtAnswer.Text.Trim(); if (answer.Length == 0) { Niunan.Shop.Utility.Tool.alert("请填写答案", this.Page); return; } if (string.IsNullOrEmpty(username)) { Niunan.Shop.Utility.Tool.alert("用户名不能为空", "getpwd1.aspx", this.Page); return; } Niunan.Shop.Model.User u = new DAL.UserDAO().GetModel(username); if (u != null) { if (answer == u.answer) //如果答案相同,开始发信 { Niunan.Shop.Utility.email.SendMail("您在 牛腩购物网的密码已经取回", "您的密码是:" + u.password, u.email,
"[email protected]", "[email protected]", "这里输入邮箱的密码", "smtp.qq.com"); Niunan.Shop.Utility.Tool.alert("您的密码已经发送到您的邮箱"+u.email+"中", "default.aspx", this.Page); } else { Niunan.Shop.Utility.Tool.alert("答案错误", this.Page); return; } } else { Niunan.Shop.Utility.Tool.alert("用户名不存在", "getpwd1.aspx", this.Page); return; } }