aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.TFS/TeamFoundationEmailClient.cs
blob: dfd357a2433d37ee9ebc646845fb7fb73524210f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
{
    /// <summary>
    /// Represents an email client for sending emails to team members.
    /// </summary>
    /// <seealso cref="Tango.TFS.ITeamFoundationEmailClient" />
    public class TeamFoundationEmailClient : ITeamFoundationEmailClient
    {
        /// <summary>
        /// Gets or sets the SMTP address.
        /// </summary>
        public String SMTP { get; set; }

        /// <summary>
        /// Gets or sets the SMTP port.
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// Gets or sets the credentials used to authenticate against the SMTP server.
        /// </summary>
        public NetworkCredential Credentials { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamFoundationEmailClient"/> class.
        /// </summary>
        public TeamFoundationEmailClient()
        {
            SMTP = "smtp-mail.outlook.com";
            Port = 587;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamFoundationEmailClient"/> class.
        /// </summary>
        /// <param name="credentials">The credentials.</param>
        public TeamFoundationEmailClient(NetworkCredential credentials) : this()
        {
            Credentials = credentials;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamFoundationEmailClient"/> class.
        /// </summary>
        /// <param name="smtp">The SMTP.</param>
        /// <param name="port">The port.</param>
        /// <param name="credentials">The credentials.</param>
        public TeamFoundationEmailClient(String smtp, int port, NetworkCredential credentials) : this(credentials)
        {
            SMTP = smtp;
            Port = port;
        }

        /// <summary>
        /// Sends the specified email.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        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);
            });
        }
    }
}