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");
|
}
|
}
|
}
|
}
|