62,041
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Mail;
using Gazx.InvestmentBank.CommonBLL.Config;
using log4net;
namespace Gazx.InvestmentBank.SystemServices.Email
{
public class SmtpHelper
{
private static readonly ILog logger = LogManager.GetLogger("Email");
public static void SendEmail(string to, string subject, string body, ArrayList attachmentList)
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
try
{
// 获取邮件参数
string from = SmtpConfigManager.ConfigInfo.MailFrom;
string displayName = SmtpConfigManager.ConfigInfo.DisplayName;
// 填写邮件基本信息
MailAddress fromAddress = new MailAddress(from, displayName);
message.From = fromAddress;
message.To.Add(to);
message.Subject = subject;
message.Body = body;
// 添加附件
foreach (string file in attachmentList)
{
if (File.Exists(file))
{
Attachment attachment = new Attachment(file);
message.Attachments.Add(attachment);
}
}
// 设置邮件发送服务器
smtp.Host = SmtpConfigManager.ConfigInfo.Host;
smtp.Port = SmtpConfigManager.ConfigInfo.Port;
// 邮件发送验证
if (SmtpConfigManager.ConfigInfo.CertificateRequired)
{
smtp.UseDefaultCredentials = false;
NetworkCredential credential = new NetworkCredential();
credential.UserName = SmtpConfigManager.ConfigInfo.UserName;
credential.Password = SmtpConfigManager.ConfigInfo.Password;
smtp.Credentials = credential;
}
// 发送邮件
smtp.Send(message);
}
catch (Exception ex)
{
logger.Info(ex.ToString());
}
finally
{
message.Dispose();
}
}
}
}