SMTP邮件发送
C#发送HTML格式邮件
支持邮件抄送
邮件密送
附件形式
using System;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web.Configuration;
using System.Collections.Generic;
namespace JsonsHelper
{
///
/// Smtp配置
///
public class SmtpConfig
{
///
/// SMTP在web.config的默认配置
///
public SmtpConfig()
{
setWebConfigBindding();
ContentEncoding = Encoding.Default;
}
private void setWebConfigBindding()
{
try
{
MailSettingsSectionGroup sectionGroup = (MailSettingsSectionGroup)WebConfigurationManager.OpenWebConfiguration("~/").GetSectionGroup("system.net/mailSettings");
if (sectionGroup == null)
{
SmtpServer = "localhost";
Port = 25;
SSLConnect = false;
}
else
{
if (sectionGroup.Smtp.Network.Host != "")
{
SmtpServer = sectionGroup.Smtp.Network.Host;
}
Port = sectionGroup.Smtp.Network.Port;
UserName = sectionGroup.Smtp.Network.UserName;
Password = sectionGroup.Smtp.Network.Password;
if (sectionGroup.Smtp.Network.DefaultCredentials == true)
{
Credentials = null;
}
else
{
Credentials = new NetworkCredential(UserName, Password);
}
}
}
catch (Exception)
{
}
}
private string smtpServerField;
///
/// 发送邮件服务器
///
public string SmtpServer
{
get { return smtpServerField; }
set { smtpServerField = value; }
}
private int portField = 25;
///
/// 服务器连接端口,默认为25。
///
public int Port
{
get { return portField; }
set { portField = value; }
}
private string userNameField;
///
/// 连接用户名
///
public string UserName
{
get { return userNameField; }
set { userNameField = value; }
}
private string passwordField;
///
/// 连接密码
///
public string Password
{
get { return passwordField; }
set { passwordField = value; }
}
private bool sslConnectField = false;
///
/// 是否是安全套接字连接,默认为否。
///
public bool SSLConnect
{
get { return sslConnectField; }
set { sslConnectField = value; }
}
private Encoding contentEncodingField;
///
/// 邮件内容编码
///
public Encoding ContentEncoding
{
get { return contentEncodingField; }
set { contentEncodingField = value; }
}
private NetworkCredential credentialsField;
///
/// 访问凭据
///
public NetworkCredential Credentials
{
get { return credentialsField; }
set { credentialsField = value; }
}
}
///
/// SMTP邮件发送
///
public class SmtpMail
{
///
/// 发送HTML格式邮件(UTF8)
///
public static string MailTo(SmtpConfig config,
MailAddress AddrFrom, MailAddress AddrTo,
MailAddressCollection cc, MailAddressCollection bCC,
string Subject, string BodyContent, bool isHtml, List attC)
{
MailMessage msg = new MailMessage(AddrFrom, AddrTo);
#region 抄送
if (cc != null && cc.Count > 0)
{
foreach (MailAddress cAddr in cc)
{
msg.CC.Add(cAddr);
}
}
#endregion
#region 密送
if (bCC != null && bCC.Count > 0)
{
foreach (MailAddress cAddr in bCC)
{
msg.Bcc.Add(cAddr);
}
}
#endregion
#region 附件列表
if (attC != null && attC.Count > 0)
{
foreach (Attachment item in attC)
{
msg.Attachments.Add(item);
}
}
#endregion
msg.IsBodyHtml = isHtml;
msg.Priority = MailPriority.High;
msg.Subject = Subject;
msg.SubjectEncoding = config.ContentEncoding;
msg.BodyEncoding = config.ContentEncoding;
msg.Body = BodyContent;
SmtpClient client = new SmtpClient(config.SmtpServer, config.Port);
if (config.Credentials != null)
{
client.Credentials = config.Credentials;
}
client.EnableSsl = config.SSLConnect;
try
{
client.Send(msg);
return "0";
}
catch (Exception exp)
{
return exp.Message;
}
}
///
/// 发送HTML格式邮件
///
/// SMTP配置
/// 发件人邮箱
/// 收件人邮箱
/// 主题
/// 内容
///
public static string MailTo(SmtpConfig config,
MailAddress AddrFrom, MailAddress AddrTo,
string Subject, string BodyContent)
{
return MailTo(config, AddrFrom, AddrTo, null, null, Subject, BodyContent, true, null);
}
}
}