Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Shtirlitz.Library/Sender/MailInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace Shtirlitz.Sender
{
/// <summary>
/// mail settings
/// </summary>
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; }
}
}
}
68 changes: 68 additions & 0 deletions Shtirlitz.Library/Sender/SmtpMailer.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Sends email via SMTP
/// </summary>
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();
}

/// <summary>
/// Log in to the SMTP server
/// </summary>
private void InitializeSmtp()
{
smtp = new SmtpClient(mailInfo.Host, mailInfo.Port);
smtp.Credentials = new NetworkCredential(mailInfo.Login, mailInfo.Password);
}

/// <summary>
/// Create a message
/// </summary>
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;
}

/// <summary>
/// Attach a file
/// </summary>
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);
}
}
}
2 changes: 2 additions & 0 deletions Shtirlitz.Library/Shtirlitz.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
<Compile Include="Reporter\WmiDumper.cs" />
<Compile Include="Sender\ArchiveRemover.cs" />
<Compile Include="Sender\ClipboardCopier.cs" />
<Compile Include="Sender\MailInfo.cs" />
<Compile Include="Sender\SmtpMailer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
1 change: 1 addition & 0 deletions Shtirlitz.Tests/Shtirlitz.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ShtirlitzBaseTestClass.cs" />
<Compile Include="ShtirlitzTests.cs" />
<Compile Include="SmtpMailerTests.cs" />
<Compile Include="StageRunnerTests.cs" />
<Compile Include="WmiDumperTests.cs" />
</ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions Shtirlitz.Tests/SmtpMailerTests.cs
Original file line number Diff line number Diff line change
@@ -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<ISender> { smtpMailer })
{ }

[Fact]
public void SendTheArchiveFile()
{
// act and assert
Assert.DoesNotThrow(() => RunSynchronously(true));
}
}
}