asp.net发送邮件
时间:2010-09-30 来源:逐、风
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="fromMail">发送人邮箱地址</param>
/// <param name="fromMailPws">发送人邮箱密码</param>
/// <param name="fromName">发送人姓名</param>
/// <param name="toMail">收件人邮箱地址</param>
/// <param name="ccMail">抄送人邮箱地址</param>
/// <param name="bccMail">密送人邮箱地址</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件内容</param>
/// <param name="isBodyHtml">是否为HTML格式的内容</param>
/// <returns></returns>
private bool SendMail(string fromMail,string fromMailPws,string fromName, string toMail, string ccMail, string bccMail, string subject, string body, bool isBodyHtml)
{
try
{
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress(fromMail, fromName, System.Text.Encoding.UTF8);
myMail.To.Add(toMail);
myMail.Subject = subject;
myMail.Body = body;
myMail.IsBodyHtml = isBodyHtml;
////附件
// string ServerFileName = "";
// if (this.upfile.PostedFile.ContentLength != 0)
// {
// string upFileName = this.upfile.PostedFile.FileName;
// string[] strTemp = upFileName.Split('.');
// string upFileExp = strTemp[strTemp.Length - 1].ToString();
// ServerFileName = Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
// this.upfile.PostedFile.SaveAs(ServerFileName);
// myMail.Attachments.Add(new Attachment(ServerFileName));
// }
string smtp = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
//域名是否可用
string DomainEnabled = Utility.GetAppSettingsValue("DomainEnabled");
if (DomainEnabled.ToLower() == "false")
smtp = "192.168.1.119";
SmtpClient sc = new SmtpClient(smtp);
sc.Credentials = new System.Net.NetworkCredential(fromMail, fromMailPws);//邮箱账号与密码
sc.Send(myMail); //发送邮件
return true;
}
catch (Exception e32)
{
Response.Write(e32.ToString());
return false;
}
}