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