一、首先需要配置web.config文件:
<
<mailSettings>
<smtp from="Emailname">
<network host="smtp. userName="用户名" password="密码"
port="25" defaultCredentials="false"/>
</smtp>
</mailSettings>
</
二、然后编写发送邮件的函数:
//// <summary>
/// </summary>
/// <param name="mailto">收件人地址。如:receiver@
/// <param name="mailsubject">邮件标题</param>
/// <param name="mailbody">邮件正文</param>
/// <param name="mailFrom">邮件发送人地址。如:sender@
/// <returns></returns>
public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom)
{
try
{
//邮件发送人地址
from = new (mailFrom);
//如test@,初步测试,用test@不行,用163的邮件服务器,就必须用163邮箱的用户名
//收件人地址
to = new (mailto);//如test@
mail = new (from, to);
mail.Subject = mailsubject;
mail.Body = mailbody;
//以下设置服务器
mySmth = new ();
mySmth.Send(mail);
mail.Dispose();
return true;
}
catch
{
return false;
}
}
三、最后就是对函数的调用了:
//自动发送邮件
string mailSubject = "会员密码";
string mailBody = 正文+数据库里的密码;
string mailFrom = ConfigurationManager.AppSettings["SendMail"];
if (MySendMail(this.txtEmail.Text, mailSubject, mailBody, mailFrom))
{
...
//发送成功,进行相应处理
}
else
{
...
//发送失败,进行相应处理
return;
}
[[it] 本帖最后由 lqw 于 2008-6-12 17:36 编辑 [/it]]