C#发送邮件
C#使用MailMessage来发送邮件,拿去直接调用。代码:static string strEmailFrom = ConfigurationManager.AppSettings["SendFromEMailAddress"];
static string strEmailPass = ConfigurationManager.AppSettings["SendFromEMailPass"];
static string strEmailSmtp = ConfigurationManager.AppSettings["SendFromEMailSmtp"];
/// <summary>
/// 发送邮件.net2005
/// </summary>
/// <param name="strEMailTo">发送邮件的目标邮箱地址,必须是“XXX@XX.XX”格式,如“to@mail.com”</param>
/// <param name="strSendMailBox">发送邮件使用的邮箱地址,从配置文件中获取</param>
/// <param name="strSendMailPassword">发送邮件使用的邮箱地址的密码,从配置文件中获取</param>
/// <param name="strMailTitle">邮件标题</param>
/// <param name="strMailContent">邮件正文</param>
/// <param name="strSmtpServerName">使用配置文件中的AppSettings["SendFromEMailSmtp"]做为smtp地址</param>
/// <param name="mailEncoding">邮件使用的编码,如果设为null或空,将使用UTF8做为编码</param>
/// <returns>true:邮件发送成功,false:邮件发送失败</returns>
public static bool SendEMail(string strEMailTo, string strMailTitle, string strMailContent, System.Text.Encoding mailEncoding)
{
bool bRes = false;
if (string.Empty == strEMailTo)
{
return bRes;
throw new Exception("发送邮件的目标邮箱地址不能为空!");
}
try
{
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(strEmailFrom, strEMailTo);
mailMessage.Subject = strMailTitle; //发送邮件主题
mailMessage.Body = strMailContent;//邮件内容
mailMessage.IsBodyHtml = true;
if (mailEncoding != null)
{
mailMessage.BodyEncoding = mailEncoding;
mailMessage.SubjectEncoding = mailEncoding;
}
else
{
//默认使用UTF8编码
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
}
System.Net.Mail.SmtpClient stmpClient = new System.Net.Mail.SmtpClient();
stmpClient.Host = strEmailSmtp;
stmpClient.UseDefaultCredentials = false;
stmpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
stmpClient.Credentials = new System.Net.NetworkCredential(strEmailFrom, strEmailPass);
stmpClient.Send(mailMessage);
bRes = true;
}
catch (System.Web.HttpException ehttp)
{
throw ehttp;
}
catch (Exception ex)
{
throw ex;
}
return bRes;
}
其中 strEmailFrom 、strEmailPass、strEmailSmtp三个变量为配置文件中配好的,邮箱地址,邮箱密码,邮箱SMTP服务器
看看 kkkkkkkkkkkkk lllll :D:D:D:D 77 恩,用C#发送邮件挺方便的~ 真不错 谢谢!
页:
[1]