bool success = false;
try
{
client.Send(mail);
success = true;
}
catch (Exception ex)
{
LogTextHelper.Error(ex);
//throw;
}
上面利用.net的SmtpClient发送邮件操作的完整代码如下:
复制代码 代码如下:
/// <summary>
/// 发送外部邮件(系统配置,系统邮件)
/// </summary>
/// <param>发送邮件信息</param>
/// <returns></returns>
public CommonResult Send(MailInfo mailInfo)
{
CommonResult result = new CommonResult();
try
{
AppConfig config = new AppConfig();
string MailDomain = config.AppConfigGet("MailDomain");
string MailUsername = config.AppConfigGet("MailUsername");
string MailPassword = config.AppConfigGet("MailPassword");
string MailPort = config.AppConfigGet("MailPort");
string MailFrom = config.AppConfigGet("MailFrom");
int port = 25;
int.TryParse(MailPort, out port);
SmtpSettingInfo settingInfo = new SmtpSettingInfo(MailDomain, port,
MailUsername, MailPassword, MailFrom);
result.Success = PrivateSendEmail(mailInfo, settingInfo);
}
catch (Exception ex)
{
result.ErrorMessage = ex.Message;
throw;
}
return result;
}
/// <summary>
/// 通用发送邮件操作
/// </summary>
private static bool PrivateSendEmail(MailInfo mailInfo, SmtpSettingInfo settingInfo)
{
string toEmails = mailInfo.ToEmail;
string bcc = "";
mailInfo.RecipientBCC.ForEach(obj => bcc += string.Format("{0},", obj));
bcc = bcc.Trim(',');
string cc = "";
mailInfo.RecipientCC.ForEach(obj => cc += string.Format("{0},", obj));
cc = cc.Trim(',');
MailMessage mail = new MailMessage(settingInfo.MailFrom, toEmails);
if (!string.IsNullOrEmpty(bcc))
{
mail.Bcc.Add(bcc);
}
if (!string.IsNullOrEmpty(cc))
{
mail.CC.Add(cc);
}