推荐一个不错的api参考资料网站:api之家http://www.apihome.cn/
using System;
using System.Collections.Generic;
using System.Text;
namespace ArtOL.Model
{
public class EMail
{
private bool _IsHtml;
/// <summary>
/// 电子邮件发送的类型,如果是true,则发送的是HTML邮件,如果是false,则是普通文本
/// </summary>
public bool IsHtml
{
get { return _IsHtml; }
set { _IsHtml = value; }
}
private string _Subject;
/// <summary>
/// 电子邮件的主题
/// </summary>
public string Subject
{
get { return _Subject; }
set { _Subject = value; }
}
private string _ReceiveUser;
/// <summary>
/// 收件人邮件地址,如果是多个收件人,则以逗号(半角的逗号)","来进行分隔
/// </summary>
public string ReceiveUser
{
get { return _ReceiveUser; }
set
{
if (IsSendGroup)
{
if (value.IndexOf(',') == -1)
throw new Exception("是群发邮件,收件人邮件地址列表,必须用逗号分隔!");
}
_ReceiveUser = value;
}
}
private bool _IsSendGroup;
/// <summary>
/// 是否群发邮件.如果是群发,则收件人邮件地址列表,用逗号分隔
/// </summary>
public bool IsSendGroup
{
get { return _IsSendGroup; }
set { _IsSendGroup = value; }
}
private string _Text;
/// <summary>
/// 发送的正文文本
/// </summary>
public string Text
{
get { return _Text; }
set { _Text = value; }
}
}
}
以上为实体类
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using ArtOL.Model;
using System.Configuration;
namespace ArtOL.Common
{
public class Mail
{
/// <summary>
/// 发送邮件的地址
/// </summary>
public string FormEmail = System.Configuration.ConfigurationManager.AppSettings["FormMail"];
/// <summary>
/// 发送邮件地址的SMTP格式地址
/// </summary>
public string SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
/// <summary>
/// 发送邮件的用户名
/// </summary>
public string UserName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
/// <summary>
/// 发送邮件的用户密码
/// </summary>
public string UserPwd = System.Configuration.ConfigurationManager.AppSettings["UserPwd"];
/// <summary>
/// 发送电子邮件的端口号
/// </summary>
public int Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
public bool Send(EMail email)
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(FormEmail);
//判断是否为群发
if (email.IsSendGroup)
{
string[] AddList = email.ReceiveUser.Split(',');
for (int i = 0; i < AddList.Length; i++)
mailMsg.To.Add(AddList[i]);
}
else
mailMsg.To.Add(email.ReceiveUser);
mailMsg.Subject = email.Subject;
mailMsg.IsBodyHtml = email.IsHtml;
mailMsg.SubjectEncoding = Encoding.UTF8;
mailMsg.BodyEncoding = Encoding.UTF8;
mailMsg.Body = email.Text;
return SendMail(mailMsg);
}
private bool SendMail(MailMessage mail)
{
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = false;
smtpClient.Host = SmtpServer;
smtpClient.Port = Port;
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, UserPwd);
try
{
smtpClient.Send(mail);
return true;
}
catch(Exception ee)
{
//发送错误写日志
ArtOL.Common.SqlHelper.WriteLog(ee.Message, "发送电子邮件错误信息");
return false;
}
}
}
}
推荐一个不错的api参考资料网站:api之家http://www.apihome.cn/