jonesd
2017-06-29 33563d0faca1f0dfc402a4ac7a479a1c38434f93
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
 
namespace SendEmail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private bool IsValidEmailAddress(string emailAddress)
        { // From http://msdn.microsoft.com/en-us/library/01escwtf.aspx
            return Regex.IsMatch(emailAddress, @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                                               @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
        }
 
        private void buttonSend_Click(object sender, EventArgs e)
        {
            bool emailSent = false;
 
            // Code taken from EvtMgt.AlertEmailService AlertEmailService.cs SendEmail()
            if ((IsValidEmailAddress(textBoxFrom.Text)) && (IsValidEmailAddress(textBoxTo.Text)) && (textBoxSubject.Text.Length > 0))
            {
                try
                {
                    SmtpClient smtp = new System.Net.Mail.SmtpClient();
                    smtp.Host = textBoxServer.Text;
                    smtp.Port = Convert.ToInt32(textBoxPort.Text);
 
                    MailMessage emailmsg = new System.Net.Mail.MailMessage();
                    emailmsg.From = new System.Net.Mail.MailAddress(textBoxFrom.Text);
                    emailmsg.To.Add(textBoxTo.Text);
                    emailmsg.Body = textBoxBody.Text;
                    emailmsg.IsBodyHtml = false; ;
                    emailmsg.Subject = textBoxSubject.Text;
 
                    DateTime startTime = DateTime.Now;
 
                    smtp.Send(emailmsg);
 
                    textBoxTimeElapsed.Text = String.Format("{0} seconds", (DateTime.Now - startTime).TotalSeconds);
                    emailSent = true;
                }
                catch (System.Exception ex)
                {
                    string message = string.Empty;
 
                    if (ex.InnerException == null)
                    {
                        message = "Exception " + ex.Message + " - " + ex.Source;
                    }
                    else
                    {
                        message = "Inner Exception " + ex.InnerException.Message;
                    }
 
                    MessageBox.Show(message);
                }
            }
            else
            {
                MessageBox.Show("Invalid email address in From or To or empty Body");
            }
        }
    }
}