Mail客户端
示例代码
public void SentTextMail()
{
MailClient client = new MailClient("mail-config");
client.SetReceivers("liqf01@mingyuanyun.com")
.SetCC("liqf01@mingyuanyun.com")
.SetSubject("MailClient_DEMO--TEXT---" + DateTime.Now.ToTimeString())
.AddAttachment(@"files/asp.net.jpg", "image/jpeg")
.SetBody("aaaaaaaaaaaaaaaaaa")
.Send();
}
public void SendHtmlMail()
{
string html = @"
<html>
<head><title>MailClient_DEMO</title></head>
<body><h1>MailClient_DEMO</h1><body>
</html>";
MailClient client = new MailClient("mail-config");
client.SetReceivers("liqf01@mingyuanyun.com")
.SetCC("liqf01@mingyuanyun.com")
.SetSubject("MailClient_DEMO--HTML---" + DateTime.Now.ToTimeString())
.AddAttachment(@"files/asp.net.jpg", "image/jpeg")
.SetHtmlBody(html)
.Send();
}
指定收件人
指定【收件人】有3种方式,可参考下面示例代码:
【抄送人】的使用方式相同,所以没有特别指定。
client
.SetReceivers("liqf01@mingyuanyun.com", "fangw@mingyuanyun.com", "yangmc@mingyuanyun.com", "412537239@qq.com")
.SetSubject("Test MimeKit" + DateTime.Now.ToTimeString())
.SetBody("aaaaaaaaaaaaaaaaaa")
.Send();
client
.SetReceivers("李奇峰1 <liqf01@mingyuanyun.com>", "方武2 <fangw@mingyuanyun.com>",
"杨敏超3 <yangmc@mingyuanyun.com>", "杨敏超4 <412537239@qq.com>")
.SetSubject("Test MimeKit" + DateTime.Now.ToTimeString())
.SetBody("aaaaaaaaaaaaaaaaaa")
.Send();
client
.SetReceivers( new NameValue("李奇峰1", "liqf01@mingyuanyun.com"), new NameValue("方武2", "fangw@mingyuanyun.com"),
new NameValue("杨敏超3", "yangmc@mingyuanyun.com"), new NameValue("杨敏超4", "412537239@qq.com") )
.SetSubject("Test MimeKit" + DateTime.Now.ToTimeString())
.SetBody("aaaaaaaaaaaaaaaaaa")
.Send();
SMTP服务器配置
在发送邮件前,请先在配置服务中注册SMTP服务器参数,可参考下图:
参数类型定义
public class SmtpConfig
{
/// <summary>
/// 服务器地址
/// </summary>
public string Host { get; set; }
/// <summary>
/// TCP端口
/// </summary>
public int Port { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 是否使用SSL连接
/// </summary>
public bool IsSSL { get; set; }
}
参数值配置示例
Host=smtp.exmail.qq.com;Port=587;UserName=xxx@mingyuanyun.com;Password=xxxxxx;IsSSL=1