tmp/test-doxy-mms/MarekMailSystem/MarekMailSystem2/SmtpEmailSender.cs

Go to the documentation of this file.
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             //client.EnableSsl = true; // TODO: to jest trymczasowo tylko dla testow gmaila..
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                 //TODO: poniewaz dopiero teraz konstruujemy MailMessage, to trzeba lapac tu tez
00092                 //takie wyjatki jak: pole 'to' nie reprezentuje poprawnego adresu email..
00093                 //pobawic sie w rzucanie i lapanie takich rzeczy!
00094                 //bo to nie bedzie chyba zaden z juz przewidzianych wyjatkow...
00095                 //juz niby zrobilem. Ale przydalyby sie jeszcze testy (czy inne wyjatki nie wyskocza...)
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             // TODO: jednak tej metody tu nie bedzie. to damy w innej klasie lub w klasie dziedziczacej.
00137             // bo tak bedzie czysciej..
00138         {
00139             throw new Exception("Not implemented.");            
00140         }
00141 
00142     }
00143 }

Generated on Thu Oct 25 11:32:52 2007 for Marek Mail System by  doxygen 1.5.3-20071008