using System; using System.Net.Mail; using System.Net; namespace Bruce.Mail { public class MailFactory { private static SmtpClient client; public static void SetSmtp(string smtpHost, int smtpPort, string username, string password) { SetSmtp(smtpHost, smtpPort); client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(username, password); } public static void SetSmtp(string smtpHost, int smtpPort) { client = new SmtpClient(smtpHost, smtpPort); } public static void SetSmtp() { client = new SmtpClient(); } private MailMessage message; public MailMessage Message { get { return this.message; } } public MailFactory() { this.message = new MailMessage(); } public MailFactory(MailAddress from, MailAddress to) { this.message = new MailMessage(from, to); } public MailFactory(string fromName, string fromAddress, string toName, string toAddress) { MailAddress from = string.IsNullOrEmpty(fromName) ? new MailAddress(fromAddress) : new MailAddress(fromAddress, fromName); MailAddress to = string.IsNullOrEmpty(toName) ? new MailAddress(toAddress) : new MailAddress(toAddress, toName); this.message = new MailMessage(from, to); } public void Write(string subject, string body) { this.message.Subject = subject; this.message.IsBodyHtml = false; this.message.Body = body; this.Send(); } public Exception Send() { try { if (client == null) SetSmtp(); client.Send(this.message); return null; } catch (Exception ex) { return ex; } } } }
using System; using System.Net.Mail; using System.Collections.Generic; using System.Threading; namespace Bruce.Mail { public abstract class PostMan { private static Queue<MailAddress> Recivers = new Queue<MailAddress>(); public static void AddReciver(MailAddress address) { Recivers.Enqueue(address); } public static void AddReciver(string address) { Recivers.Enqueue(new MailAddress(address)); } private int checkInterval; public int CheckInterval { get { return this.checkInterval; } set { this.checkInterval = value; } } private bool flag = true; public bool Flag { get { return this.flag; } set { this.flag = value; } } public void Work() { while (Flag) { if (Recivers.Count == 0) { Thread.Sleep(30000); } else { MailAddress to = Recivers.Dequeue(); Deliver(to); } } } abstract void Deliver(MailAddress to); } }