Below is a code snippet for sending emails through C#

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
103
104
105
106
107
108
109
110

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace TestAutomationFramework.Common
{
    public class EmailHelper
    {
        public static void Send(string host, EmailRequest request)
        {
            ValidEmailRequest(request);

            using (var mailMessage = new MailMessage())
            {
                mailMessage.Subject = request.Subject;
                mailMessage.Body = request.Body;
                mailMessage.IsBodyHtml = request.IsBodyHtml;
                mailMessage.From = new MailAddress(request.From);

                request.To.ForEach(t => mailMessage.To.Add(t));
                request.Cc.ForEach(c => mailMessage.CC.Add(c));
                request.Bcc.ForEach(b => mailMessage.Bcc.Add(b));
                request.Attachments.ForEach(a => mailMessage.Attachments.Add(new Attachment(a)));

                var smtpClient = new SmtpClient { Host = host };

                smtpClient.Send(mailMessage);
            }
        }

        public static void ValidEmailRequest(EmailRequest emailRequest)
        {
            if (!IsValidEmailAddress(emailRequest.From))
                throw new Exception($"Email request From:{emailRequest.From} is not a valid email address.");

            emailRequest.To?.ForEach(e =>
            {
                if (!IsValidEmailAddress(e))
                {
                    throw new Exception($"Email request To:{e} is not a valid email address.");
                }
            });

            emailRequest.Cc?.ForEach(e =>
            {
                if (!IsValidEmailAddress(e))
                {
                    throw new Exception($"Email request Cc:{e} is not a valid email address.");
                }
            });

            emailRequest.Bcc?.ForEach(e =>
            {
                if (!IsValidEmailAddress(e))
                {
                    throw new Exception($"Email request Bcc:{e} is not a valid email address.");
                }
            });

            emailRequest.Attachments?.ForEach(a =>
            {
                if (!IsValidAttachment(a))
                {
                    throw new Exception($"Email request Attachment:{a} is not accessible.");
                }
            });
        }

        private static bool IsValidAttachment(string attachment)
        {
            return File.Exists(attachment);
        }

        private static bool IsValidEmailAddress(string emailAddress)
        {
            if (string.IsNullOrWhiteSpace(emailAddress))
                return false;

            //https://msdn.microsoft.com/en-us/library/01escwtf(v=vs.110).aspx
            try
            {
                return Regex.IsMatch(emailAddress,
                      @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                      @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
                      RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
            }
            catch (RegexMatchTimeoutException)
            {
                return false;
            }
        }
    }

    public class EmailRequest
    {
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsBodyHtml { get; set; }
        public string From { get; set; }
        public List<string> To { get; set; }
        public List<string> Cc { get; set; }
        public List<string> Bcc { get; set; }
        public List<string> Attachments { get; set; }
    }

}

Comments