00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.Net;
00005 using System.Net.Mail;
00006
00007
00008 namespace MarekMailSystem2
00009 {
00015 public class SmtpEmailSender : EmailSender
00016 {
00017 protected SmtpClient client;
00018
00019 public SmtpEmailSender()
00020 {
00021 client = new SmtpClient();
00022
00023 }
00024
00025 public string Host
00026 {
00027 get { return client.Host; }
00028 set { client.Host = value; }
00029 }
00030
00031 public int Port
00032 {
00033 get { return client.Port; }
00034 set { client.Port = value; }
00035 }
00036
00037 public bool Ssl
00038 {
00039 get { return client.EnableSsl; }
00040 set { client.EnableSsl = value; }
00041 }
00042
00051 public bool UseDefaultCredentials
00052 {
00053 get { return client.UseDefaultCredentials; }
00054 set { client.UseDefaultCredentials = value; }
00055 }
00056
00057 private string userName;
00058
00059 public string UserName
00060 {
00061 get { return userName; }
00062 set { userName = value; updateCredentials(); }
00063 }
00064
00065 private string password;
00066
00067 public string Password
00068 {
00069 get { return password; }
00070 set { password = value; updateCredentials(); }
00071 }
00072
00073 private void updateCredentials()
00074 {
00075 client.Credentials = userName == null || password == null ? null :
00076 new NetworkCredential(userName, password);
00077 }
00078
00085 public override void Send(Email email)
00086 {
00087
00088 try
00089 {
00090 client.Send(email.ConstructMailMessage());
00091
00092
00093
00094
00095
00096 email.SendingStatus = new EmailStatus(StatusCode.Sent);
00097 }
00098 catch (FormatException ex)
00099 {
00100 email.SendingStatus = new EmailStatus(StatusCode.InvalidFormat, ex.Message);
00101 }
00102 catch (ArgumentNullException ex)
00103 {
00104 email.SendingStatus = new EmailStatus(StatusCode.ArgumentNull, ex.Message);
00105 }
00106 catch (ArgumentOutOfRangeException ex)
00107 {
00108 email.SendingStatus = new EmailStatus(StatusCode.NoRecipients, ex.Message);
00109 }
00110 catch (ObjectDisposedException ex)
00111 {
00112 email.SendingStatus = new EmailStatus(StatusCode.ObjectDisposed, ex.Message);
00113 }
00114 catch (InvalidOperationException ex)
00115 {
00116 email.SendingStatus = new EmailStatus(StatusCode.InvalidOperation, ex.Message);
00117 }
00118 catch (SmtpFailedRecipientsException ex)
00119 {
00120 email.SendingStatus = new EmailStatus(StatusCode.SmtpFailed, ex.StatusCode, ex.Message);
00121 }
00122 catch (SmtpException ex)
00123 {
00124 email.SendingStatus = new EmailStatus(StatusCode.SmtpFailed, ex.StatusCode, ex.Message);
00125 }
00126 email.SendingTime = DateTime.Now;
00127
00128 }
00129
00130
00131
00132
00133
00134
00135 public void SendSchedule(Email email, DateTime time)
00136
00137
00138 {
00139 throw new Exception("Not implemented.");
00140 }
00141
00142 }
00143 }