diff --git a/Shtirlitz.Library/Sender/MailInfo.cs b/Shtirlitz.Library/Sender/MailInfo.cs new file mode 100644 index 0000000..31106ae --- /dev/null +++ b/Shtirlitz.Library/Sender/MailInfo.cs @@ -0,0 +1,64 @@ +namespace Shtirlitz.Sender +{ + /// + /// mail settings + /// + public class MailInfo + { + private string emailTo; + + public string EmailTo + { + get { return emailTo; } + set { emailTo = value; } + } + + private string subject; + + public string Subject + { + get { return subject; } + set { subject = value; } + } + + private string body; + + public string Body + { + get { return body; } + set { body = value; } + } + + private string host; + + public string Host + { + get { return host; } + set { host = value; } + } + + private int port; + + public int Port + { + get { return port; } + set { port = value; } + } + + private string login; + + public string Login + { + get { return login; } + set { login = value; } + } + + private string password; + + public string Password + { + get { return password; } + set { password = value; } + } + } +} diff --git a/Shtirlitz.Library/Sender/SmtpMailer.cs b/Shtirlitz.Library/Sender/SmtpMailer.cs new file mode 100644 index 0000000..830871d --- /dev/null +++ b/Shtirlitz.Library/Sender/SmtpMailer.cs @@ -0,0 +1,68 @@ +using System.Threading; +//using System.Web; +using System.Net; +using System.Net.Mail; +using System.Net.Mime; +using Shtirlitz.Common; + +namespace Shtirlitz.Sender +{ + /// + /// Sends email via SMTP + /// + public class SmtpMailer : ISender + { + private SmtpClient smtp; + private MailMessage message; + private MailInfo mailInfo; + + public SmtpMailer(MailInfo mailInfo) + { + this.mailInfo = mailInfo; + } + + public void Send(string archiveFilename, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) + { + InitializeSmtp(); + CreateMessage(); + AttachFile(archiveFilename); + smtp.Send(message); + message.Dispose(); + } + + /// + /// Log in to the SMTP server + /// + private void InitializeSmtp() + { + smtp = new SmtpClient(mailInfo.Host, mailInfo.Port); + smtp.Credentials = new NetworkCredential(mailInfo.Login, mailInfo.Password); + } + + /// + /// Create a message + /// + private void CreateMessage() + { + message = new MailMessage(); + message.From = new MailAddress(mailInfo.Login); + message.To.Add(new MailAddress(mailInfo.EmailTo)); + message.Subject = mailInfo.Subject; + message.Body = mailInfo.Body; + } + + /// + /// Attach a file + /// + private void AttachFile(string attachFileName) + { + Attachment attach = new Attachment(attachFileName, MediaTypeNames.Application.Octet); + // Adding information to a file + ContentDisposition disposition = attach.ContentDisposition; + disposition.CreationDate = System.IO.File.GetCreationTime(attachFileName); + disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachFileName); + disposition.ReadDate = System.IO.File.GetLastAccessTime(attachFileName); + message.Attachments.Add(attach); + } + } +} diff --git a/Shtirlitz.Library/Shtirlitz.Library.csproj b/Shtirlitz.Library/Shtirlitz.Library.csproj index 613ea69..fe53113 100644 --- a/Shtirlitz.Library/Shtirlitz.Library.csproj +++ b/Shtirlitz.Library/Shtirlitz.Library.csproj @@ -46,6 +46,8 @@ + + diff --git a/Shtirlitz.Tests/Shtirlitz.Tests.csproj b/Shtirlitz.Tests/Shtirlitz.Tests.csproj index 2e1f13f..776224d 100644 --- a/Shtirlitz.Tests/Shtirlitz.Tests.csproj +++ b/Shtirlitz.Tests/Shtirlitz.Tests.csproj @@ -48,6 +48,7 @@ + diff --git a/Shtirlitz.Tests/SmtpMailerTests.cs b/Shtirlitz.Tests/SmtpMailerTests.cs new file mode 100644 index 0000000..1280df4 --- /dev/null +++ b/Shtirlitz.Tests/SmtpMailerTests.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using Shtirlitz.Sender; +using Xunit; + +namespace Shtirlitz.Tests +{ + public class SmtpMailerTests : ShtirlitzBaseTestClass + { + private static MailInfo mailInfo = new MailInfo(); + + public SmtpMailerTests() + : this(new SmtpMailer(mailInfo)) + { + mailInfo.Subject = "Hi!"; + mailInfo.Body = "bla bla bla"; + mailInfo.EmailTo = "test@gmail.com"; + mailInfo.Host = "smtp.mail.ru"; + mailInfo.Port = 25; + mailInfo.Login = "test@mail.ru"; + mailInfo.Password = "*****"; + + } + + public SmtpMailerTests(SmtpMailer smtpMailer) + : base (null, new List { smtpMailer }) + { } + + [Fact] + public void SendTheArchiveFile() + { + // act and assert + Assert.DoesNotThrow(() => RunSynchronously(true)); + } + } +}