邮件发送(C#)
2014-01-23 12:12
239 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
namespace Haitai
{
public class EmailClient
{
public EmailClient(string host, int port, bool enableSsl, string username, string password)
{
_host = host;
_port = port;
_enableSsl = enableSsl;
_username = username;
_password = password;
}
public event SendCompletedEventHandler SendCompleted;
private string _host;
private int _port;
private bool _enableSsl;
private string _username;
private string _password;
private SmtpClient _smtpClient;
private SmtpClient SmtpClient
{
get
{
if (_smtpClient == null)
{
_smtpClient = new SmtpClient();
_smtpClient.Host = _host;
_smtpClient.Port = _port;
if (_username != null && _password != null)
{
_smtpClient.Credentials = new NetworkCredential(_username, _password);
}
_smtpClient.EnableSsl = _enableSsl;
_smtpClient.SendCompleted += (sender, e) =>
{
if (SendCompleted != null)
{
SendCompleted(this, e);
}
};
}
return _smtpClient;
}
}
private MailMessage ComposeMessage(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from, fromName, Encoding.UTF8);
foreach (string to in toList.Keys)
{
message.To.Add(new MailAddress(to, toList[to], Encoding.UTF8));
}
if (ccList != null)
{
foreach (string cc in ccList.Keys)
{
message.CC.Add(new MailAddress(cc, ccList[cc], Encoding.UTF8));
}
}
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
if (attachments != null)
{
foreach (string name in attachments.Keys)
{
message.Attachments.Add(new Attachment(attachments[name], name));
}
}
return message;
}
public void Send(string from, string fromName, string to, string toName, string subject, string body)
{
Dictionary<string, string> toList = new Dictionary<string, string>();
toList.Add(to, toName);
Send(from, fromName, toList, null, subject, body, null);
}
public void Send(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
SmtpClient.Send(ComposeMessage(from, fromName, toList, ccList, subject, body, attachments));
}
public MailMessage SendAsync(string from, string fromName, string to, string toName, string subject, string body)
{
Dictionary<string, string> toList = new Dictionary<string, string>();
toList.Add(to, toName);
return SendAsync(from, fromName, toList, null, subject, body, null);
}
public MailMessage SendAsync(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
MailMessage message = ComposeMessage(from, fromName, toList, ccList, subject, body, attachments);
SmtpClient.SendAsync(message, message);
return message;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
namespace Haitai
{
public class EmailClient
{
public EmailClient(string host, int port, bool enableSsl, string username, string password)
{
_host = host;
_port = port;
_enableSsl = enableSsl;
_username = username;
_password = password;
}
public event SendCompletedEventHandler SendCompleted;
private string _host;
private int _port;
private bool _enableSsl;
private string _username;
private string _password;
private SmtpClient _smtpClient;
private SmtpClient SmtpClient
{
get
{
if (_smtpClient == null)
{
_smtpClient = new SmtpClient();
_smtpClient.Host = _host;
_smtpClient.Port = _port;
if (_username != null && _password != null)
{
_smtpClient.Credentials = new NetworkCredential(_username, _password);
}
_smtpClient.EnableSsl = _enableSsl;
_smtpClient.SendCompleted += (sender, e) =>
{
if (SendCompleted != null)
{
SendCompleted(this, e);
}
};
}
return _smtpClient;
}
}
private MailMessage ComposeMessage(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(from, fromName, Encoding.UTF8);
foreach (string to in toList.Keys)
{
message.To.Add(new MailAddress(to, toList[to], Encoding.UTF8));
}
if (ccList != null)
{
foreach (string cc in ccList.Keys)
{
message.CC.Add(new MailAddress(cc, ccList[cc], Encoding.UTF8));
}
}
message.Subject = subject;
message.SubjectEncoding = Encoding.UTF8;
message.Body = body;
message.BodyEncoding = Encoding.UTF8;
if (attachments != null)
{
foreach (string name in attachments.Keys)
{
message.Attachments.Add(new Attachment(attachments[name], name));
}
}
return message;
}
public void Send(string from, string fromName, string to, string toName, string subject, string body)
{
Dictionary<string, string> toList = new Dictionary<string, string>();
toList.Add(to, toName);
Send(from, fromName, toList, null, subject, body, null);
}
public void Send(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
SmtpClient.Send(ComposeMessage(from, fromName, toList, ccList, subject, body, attachments));
}
public MailMessage SendAsync(string from, string fromName, string to, string toName, string subject, string body)
{
Dictionary<string, string> toList = new Dictionary<string, string>();
toList.Add(to, toName);
return SendAsync(from, fromName, toList, null, subject, body, null);
}
public MailMessage SendAsync(string from, string fromName, Dictionary<string, string> toList, Dictionary<string, string> ccList, string subject, string body, Dictionary<string, Stream> attachments)
{
MailMessage message = ComposeMessage(from, fromName, toList, ccList, subject, body, attachments);
SmtpClient.SendAsync(message, message);
return message;
}
}
}
相关文章推荐
- c#发送邮件(情人节凌晨测试的第一个小模块)
- c# 发送邮件
- c#.net 下的邮件发送(含附件)
- C#使用SMTP协议发送邮件(腾讯企业邮箱)
- win2008 64位系统 C#实现发送邮件(ASP.NET)
- 【C#】Smtp发送邮件
- C#发送Email邮件方法总结
- c# 实现邮件发送
- c#中发送邮件
- 上一篇c#和outlook发送邮件使用的file类的部分代码
- C#发送邮件的类(不用WEB.MAIL类)
- 使用C#发送邮件
- C#发送邮件
- c# 使用Codosys.dll(CDO)发送邮件
- c#socket发送邮件详解
- C# 使用smtp发送邮件
- 关于C#发送邮件的换行
- C# 连接 Exchange 发送邮件
- C# 发送邮件示例
- C# 取电信公网IP并发送邮件