summaryrefslogtreecommitdiffstats
path: root/src/Utilities/Email.cs
blob: 248db327647cec3a4094a191d2afbfa6a11075a1 (plain) (blame)
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
using System;
using System.Net.Mail;
using System.Net;

namespace IOL.Fagprove.Utilities
{
    public class Email
    {
        public string Recepient { get; set; }
        public string Message { get; set; }
        public string Title { get; set; }
        public bool Valid { get; private set; }
        public bool Sent { get; private set; }

        public bool Send()
        {
            if (!Validate()) return false;
            var client = new SmtpClient("smtp.server.com", 587)
            {
                EnableSsl = true,
                Credentials = new NetworkCredential("user", "pass")
            };
            var from = new MailAddress("bot@bottesen.no", "Hal", System.Text.Encoding.UTF8);
            var to = new MailAddress(Recepient);
            var message = new MailMessage(from, to)
            {
                Body = Message,
                BodyEncoding = System.Text.Encoding.UTF8,
                Subject = Title,
                SubjectEncoding = System.Text.Encoding.UTF8
            };
#if DEBUG
            Console.WriteLine(Message);
#else
            client.Send(message);
#endif
            Sent = true;
            return true;
        }

        private bool Validate()
        {
            if (Recepient.IsMissing()
                || Title.IsMissing()
                || Message.IsMissing()
                || !Recepient.IsEmail())
            {
                Valid = false;
                return false;
            }

            Valid = true;
            return true;
        }
    }
}