C# 通过smtp服务器进行邮件发送 MailHelper
标签:sms 邮件发送 smtp c#
C# 通过smtp服务器进行邮件发送 MailHelper.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Mail;
///
/// MailHelper 的摘要说明
///
public class MailHelper
{
public MailHelper()
{
}
public string addres_mail = Common.GetConfig("mail", "MailConfig.xml");
public string addres_smtp = Common.GetConfig("smtp", "MailConfig.xml");
public string addres_mailpwd = Common.GetConfig("mailpwd", "MailConfig.xml");
//发送邮件
public static bool SendEmail(string userEmail, string subject, string content)
{
SendSms sms = new SendSms();
bool bfg = false;
try
{
WriteLog("C://Log//SMTP//", "发信邮箱:" + sms.addres_mail + " 发信密码:" + sms.addres_mailpwd + " 发信smtp:" + sms.addres_smtp + " Email:" + userEmail + "\r\n");
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.From = new MailAddress(sms.addres_mail, "越界门户网站-密码找回", Encoding.GetEncoding("utf-8"));
msg.To.Add(userEmail);
msg.BodyEncoding = Encoding.GetEncoding("utf-8");
msg.SubjectEncoding = Encoding.GetEncoding("utf-8");
msg.Subject = subject;
msg.Body = content;
//// 添加附件
string sfile = "附件地址";
mail.Attachments.Add(new Attachment(sfile));
SmtpClient mail = new SmtpClient();//实例
mail.Host = sms.addres_smtp; //发信主机
mail.Port = 25;
mail.UseDefaultCredentials = true;
mail.Credentials = new NetworkCredential(sms.addres_mail, sms.addres_mailpwd);
mail.Send(msg);
bfg = true;
}
catch (Exception ex)
{
WriteLog("C://Log//Error//", "错误信息 Message:" + ex.Message + " Source:" + ex.Source + " StackTrace:" + ex.StackTrace + "\r\n");
}
return bfg;
}
///
/// 写日志
///
public static void WriteLog(string LogPath, string msg)
{
try
{
string toFileFoder = LogPath;
string toFileFullPath = LogPath + " " +
DateTime.Now.ToString("yyyyMMdd") + ".Log";
DirectoryInfo di = new DirectoryInfo(toFileFoder);
if (!di.Exists)
{
di.Create();
}
System.IO.StreamWriter sw = System.IO.File.AppendText(toFileFullPath);
sw.WriteLine(msg);
sw.Close();
}
catch
{ }
}
}
C# 通过smtp服务器进行邮件发送 MailHelper
标签:sms 邮件发送 smtp c#
原文地址:http://blog.csdn.net/fuyifang/article/details/40503549
评论