using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace Tango.TFS
{
///
/// Represents an email client for sending emails to team members.
///
///
public class TeamFoundationEmailClient : ITeamFoundationEmailClient
{
///
/// Gets or sets the SMTP address.
///
public String SMTP { get; set; }
///
/// Gets or sets the SMTP port.
///
public int Port { get; set; }
///
/// Gets or sets the credentials used to authenticate against the SMTP server.
///
public NetworkCredential Credentials { get; set; }
///
/// Initializes a new instance of the class.
///
public TeamFoundationEmailClient()
{
SMTP = "smtp-mail.outlook.com";
Port = 587;
}
///
/// Initializes a new instance of the class.
///
/// The credentials.
public TeamFoundationEmailClient(NetworkCredential credentials) : this()
{
Credentials = credentials;
}
///
/// Initializes a new instance of the class.
///
/// The SMTP.
/// The port.
/// The credentials.
public TeamFoundationEmailClient(String smtp, int port, NetworkCredential credentials) : this(credentials)
{
SMTP = smtp;
Port = port;
}
///
/// Sends the specified email.
///
/// The email.
///
public Task Send(Email email)
{
return Task.Factory.StartNew(() =>
{
SmtpClient client = new SmtpClient(SMTP);
client.Port = Port;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
NetworkCredential credentials = Credentials;
client.EnableSsl = true;
client.Credentials = credentials;
var mail = new MailMessage();
mail.From = new MailAddress(email.From.Email);
foreach (var member in email.To)
{
mail.To.Add(new MailAddress(member.Email));
}
foreach (var file in email.Attachments)
{
mail.Attachments.Add(new System.Net.Mail.Attachment(new FileStream(file.FilePath, FileMode.Open), file.Name));
}
mail.Subject = email.Subject;
mail.Body = email.Body;
mail.IsBodyHtml = email.IsBodyHTML;
//mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
client.Send(mail);
});
}
}
}