转的
发送邮件的发信人邮箱必须使用smtp协议的邮箱,例如163、126、sina 等邮箱(但是163、126的早期注册的用户使用了些协议,126邮箱从06年6六月份后的注册的邮箱都不可用,而sina邮箱里可以自己设置是否启用smtp协议)其它邮箱类同
以sina为例 smtp服务器为:smtp.sina.com
protected
void
Button1_Click(
object
sender, EventArgs e)
{
if (SendEmailForGetpass( " 收件人名称 " , " 收件人的邮箱地址 " , " 发件人的服务器 " , " 发件人名称 " , " 发件人的邮箱地址 " , " 发件人密码 " , " 邮件标题 " , " 邮件的内容 " ))
{
Label1.Text = " 成功 " ;
}
else
{
Label1.Text = " 失败 " ;
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="toname"> 收件人名称 </param>
/// <param name="toemail"> 收件人邮件地址 </param>
/// <param name="smtpclient"> 发件人的伺服器 </param>
/// <param name="fromname"> 发件人名称 </param>
/// <param name="fromemail"> 发件人邮件地址 </param>
/// <param name="password"> 发件人邮箱密码 </param>
/// <param name="subject"> 邮件标题 </param>
/// <param name="body"> 邮件主体 </param>
/// <returns></returns>
public bool SendEmailForGetpass( string toname, string toemail, string smtpclient, string fromname, string fromemail, string password, string subject, string body)
{
try
{
MailAddress from = new MailAddress(fromemail, fromname);
MailAddress to = new MailAddress(toemail, toname);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;
message.Priority = MailPriority.High;
message.IsBodyHtml = true ;
SmtpClient client = new SmtpClient(smtpclient); // 伺服器,如"smtp.163.com"
client.Credentials = new System.Net.NetworkCredential(fromemail, password);
client.Send(message);
return true ;
}
catch (Exception ex)
{
return false ;
}
}
{
if (SendEmailForGetpass( " 收件人名称 " , " 收件人的邮箱地址 " , " 发件人的服务器 " , " 发件人名称 " , " 发件人的邮箱地址 " , " 发件人密码 " , " 邮件标题 " , " 邮件的内容 " ))
{
Label1.Text = " 成功 " ;
}
else
{
Label1.Text = " 失败 " ;
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="toname"> 收件人名称 </param>
/// <param name="toemail"> 收件人邮件地址 </param>
/// <param name="smtpclient"> 发件人的伺服器 </param>
/// <param name="fromname"> 发件人名称 </param>
/// <param name="fromemail"> 发件人邮件地址 </param>
/// <param name="password"> 发件人邮箱密码 </param>
/// <param name="subject"> 邮件标题 </param>
/// <param name="body"> 邮件主体 </param>
/// <returns></returns>
public bool SendEmailForGetpass( string toname, string toemail, string smtpclient, string fromname, string fromemail, string password, string subject, string body)
{
try
{
MailAddress from = new MailAddress(fromemail, fromname);
MailAddress to = new MailAddress(toemail, toname);
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;
message.Priority = MailPriority.High;
message.IsBodyHtml = true ;
SmtpClient client = new SmtpClient(smtpclient); // 伺服器,如"smtp.163.com"
client.Credentials = new System.Net.NetworkCredential(fromemail, password);
client.Send(message);
return true ;
}
catch (Exception ex)
{
return false ;
}
}