c# 发送html邮件,C#发送HTML格式邮件

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);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值