您的位置:首页 > 编程语言 > C#

c# 发送邮件(附加文件)

2014-04-16 13:37 260 查看
#region 发送邮件
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="email"></param>
/// <param name="emailMix"></param>
protected Boolean SendEmail(string username, string email, string emailMix)//用户名,邮箱,标题
{
string UrlEmailMix = System.Web.HttpUtility.UrlEncode(emailMix);
string strSmtpServer = SysConfig.StrSmtpServer;
string strFrom = SysConfig.StrFrom;//发送方帐号
string strFromPass = SysConfig.StrFromPass;//密码
string strto = email;
string strSubject = SysConfig.StrSubject;
string url = HttpContext.Current.Request.Url.Authority;
//string lowerUrl = url.ToLower();

string filePath = "Http://" + url + SysConfig.ApplicationPath + "";
string strBody = "内容";

if (SendSMTPEMail(strSmtpServer, strFrom, strFromPass, strto, strSubject, strBody))
{
return true;
}
else
{
return false;
}

}
#endregion 发送取回密码邮件

#region 发送邮件接口
/// <summary>
/// 发送邮件接口
/// </summary>
/// <param name="strSmtpServer">邮件服务器,例如:mail.mycompany.com</param>
/// <param name="strFrom">用来发送邮件的email,例如:you@yourcompany.com</param>
/// <param name="strFromPass">用来发送邮件的rmail密码</param>
/// <param name="strto">发送地址email</param>
/// <param name="strSubject">邮件标题</param>
/// <param name="strBody">邮件内容</param>
protected Boolean SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
//       mail.To = "me@mycompany.com";
//mail.From = "you@yourcompany.com";
//SmtpMail.SmtpServer = "mail.mycompany.com";
try
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);

message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
string ServerFileName = Server.MapPath("../ExcelFile/" + filename + "");//文件路径
message.Attachments.Add(new Attachment(ServerFileName));
client.Send(message);
}
catch (Exception er)
{
return false;
}
return true;
}
#endregion 发送邮件接口
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: