欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#利用system.net发送html格式邮件

程序员文章站 2024-02-24 16:52:40
复制代码 代码如下:using system;using system.text;using system.net;using system.net.mail;using...

复制代码 代码如下:

using system;
using system.text;
using system.net;
using system.net.mail;
using system.net.mime;

namespace leestudio.basic
{
/// <summary>
/// 邮件发送类
/// </summary>
public class sendemail
{
private mailmessage mailmessage = new mailmessage(); //实例化一个邮件类

#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="toaddresses">收件人地址(多个以,号分开)</param>
/// <param name="fromaddress">发件人地址</param>
/// <param name="title">主题</param>
/// <param name="body">正文</param>
public sendemail(string toaddresses, string fromaddress, string title, string body)
: this(toaddresses, fromaddress, "", "", title, body, false)
{
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="toaddress">收件人地址</param>
/// <param name="fromaddress">发件人地址</param>
/// <param name="toname">收件人名字</param>
/// <param name="fromname">发件人姓名</param>
/// <param name="title">主题</param>
/// <param name="body">正文</param>
/// <param name="isbodyhtml">正文是否为html格式</param>
public sendemail(string toaddress, string fromaddress, string toname, string fromname, string title, string body, bool isbodyhtml)
{
mailmessage.from = new mailaddress(fromaddress, fromname, encoding.getencoding(936));
if (toname.equals(""))
mailmessage.to.add(toaddress);
else
mailmessage.to.add(new mailaddress(toaddress, toname, encoding.getencoding(936)));

mailmessage.subject = title;
mailmessage.subjectencoding = encoding.getencoding(936);

mailmessage.body = body;
mailmessage.isbodyhtml = isbodyhtml;
mailmessage.bodyencoding = encoding.getencoding(936);
}
#endregion

/// <summary>
/// 设置smtp,并且将邮件发送出去
/// 所有参数都设置完成后再调用该方法
/// </summary>
/// <param name="password">发件人密码</param>
/// <param name="smtphost">smtp服务器地址</param>
public void setsmtp(string password, string smtphost)
{
setsmtp(mailmessage.from.address, password, smtphost, 25, false, mailpriority.normal);
}

/// <summary>
/// 设置smtp,并且将邮件发送出去
/// 所有参数都设置完成后再调用该方法
/// </summary>
/// <param name="address">发件人地址(必须为真实有效的email地址)</param>
/// <param name="password">发件人密码</param>
/// <param name="smtphost">smtp服务器地址</param>
/// <param name="smtpport">smtp服务器的端口</param>
/// <param name="isenablessl">smtp服务器是否启用ssl加密</param>
/// <param name="priority">邮件的优先级</param>
public void setsmtp(string address, string password, string smtphost, int smtpport, bool isenablessl, mailpriority priority)
{
smtpclient smtp = new smtpclient();
smtp.deliverymethod = smtpdeliverymethod.network;
smtp.credentials = new networkcredential(address, password);
smtp.host = smtphost;
smtp.port = smtpport;
smtp.enablessl = isenablessl;

mailmessage.priority = priority;
smtp.send(mailmessage); //发送邮件
}

#region //设置邮件地址
/// <summary>
/// 设置更多收件人
/// </summary>
/// <param name="toaddresses">收件人地址</param>
public void setmoretoaddress(string toaddresses)
{
mailmessage.to.add(toaddresses);
}

/// <summary>
/// 设置更多收件人
/// </summary>
/// <param name="toaddresses">收件人地址</param>
/// <param name="toname">收件人名字</param>
public void setmoretoaddress(string toaddress, string toname)
{
mailmessage.to.add(new mailaddress(toaddress, toname, encoding.getencoding(936)));
}

/// <summary>
/// 设置抄送者(多个以,号分开)
/// </summary>
/// <param name="ccaddresses">抄送者地址</param>
public void setcarboncopyfor(string ccaddresses)
{
mailmessage.cc.add(ccaddresses);
}

/// <summary>
/// 设置抄送者
/// </summary>
/// <param name="ccaddress">抄送者地址</param>
/// <param name="ccname">抄送者名字</param>
public void setcarboncopyfor(string ccaddress, string ccname)
{
mailmessage.bcc.add(new mailaddress(ccaddress, ccname, encoding.getencoding(936)));
}

/// <summary>
/// 设置密送者(多个以,号分开)
/// </summary>
/// <param name="bccaddresses">密送者</param>
public void setblindcarboncopyfor(string bccaddresses)
{
mailmessage.bcc.add(bccaddresses);
}

/// <summary>
/// 设置密送者
/// </summary>
/// <param name="bccaddress">密送者</param>
/// <param name="bccname">密送者名字</param>
public void setblindcarboncopyfor(string bccaddress, string bccname)
{
mailmessage.bcc.add(new mailaddress(bccaddress, bccname, encoding.getencoding(936)));
}
#endregion

#region 添加附件
/// <summary>
/// 添加附件(自动识别文件类型)
/// </summary>
/// <param name="filename">单个文件的路径</param>
public void attachments(string filename)
{
mailmessage.attachments.add(new attachment(filename));
}

/// <summary>
/// 添加附件(默认为富文本rtf格式)
/// </summary>
/// <param name="filename">单个文件的路径</param>
public void attachmentsforrtf(string filename)
{
mailmessage.attachments.add(new attachment(filename, mediatypenames.application.rtf));
}
#endregion
}
}