-------------------------------------------------------------------------------------------------------------------------
Mail helper class
-------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Web;
public class MailHelper
{
/// <summary>
/// This helper class sends an email message using the System.Net.Mail namespace
/// </summary>
/// <param name="fromEmail">Sender email address</param>
/// <param name="toEmail">Recipient email address</param>
/// <param name="bcc">Blind carbon copy email address</param>
/// <param name="cc">Carbon copy email address</param>
/// <param name="subject">Subject of the email message</param>
/// <param name="body">Body of the email message</param>
/// <param name="attachment">File to attach</param>
#region Static Members
public static void SendMailMessage(string toEmail, string fromEmail, string bcc, string cc, string subject, string body, List<string> attachmentFullPath)
{
//create the MailMessage object
MailMessage mMailMessage = new MailMessage();
//set the sender address of the mail message
if (!string.IsNullOrEmpty(fromEmail))
{
mMailMessage.From = new MailAddress(fromEmail);
}
//set the recipient address of the mail message
mMailMessage.To.Add(new MailAddress(toEmail));
//set the blind carbon copy address
if (!string.IsNullOrEmpty(bcc))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
//set the carbon copy address
if (!string.IsNullOrEmpty(cc))
{
mMailMessage.CC.Add(new MailAddress(cc));
}
//set the subject of the mail message
if (!string.IsNullOrEmpty(subject))
{
mMailMessage.Subject = "UBH Web Application Notification" ;
}
else
{
mMailMessage.Subject = subject;
}
//set the body of the mail message
mMailMessage.Body = body;
//set the format of the mail message body
mMailMessage.IsBodyHtml = false;
//set the priority
mMailMessage.Priority = MailPriority.Normal;
//add any attachments from the filesystem
foreach (var attachmentPath in attachmentFullPath)
{
Attachment mailAttachment = new Attachment(attachmentPath);
mMailMessage.Attachments.Add(mailAttachment);
}
//create the SmtpClient instance
SmtpClient mSmtpClient = new SmtpClient();
//send the mail message
mSmtpClient.Send(mMailMessage);
}
/// <summary>
/// Determines whether an email address is valid.
/// </summary>
/// <param name="emailAddress">The email address to validate.</param>
/// <returns>
///
<c>true</c> if the email address is valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsValidEmailAddress(string emailAddress)
{
// An empty or null string is not valid
if (String.IsNullOrEmpty(emailAddress))
{
return (false);
}
// Regular expression to match valid email address
string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
// Match the email address using a regular expression
Regex re = new Regex(emailRegex);
if (re.IsMatch(emailAddress))
return (true);
else
return (false);
}
#endregion
}
-------------------------------------------------------------------------------------------------------------------------
Code behind
-------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Data;
namespace MailSentCracker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void btnsent_Click(object sender, RoutedEventArgs e)
{
bool status = true;
status = Sendmail();
if (status == true)
{
MessageBox.Show("Sent Succesfully! Life ginga lala.. he he..");
}
else
{
MessageBox.Show("Some thing gone wrong or Fill the Required fileds (from,to message body)");
}
}
public bool Sendmail()
{
bool status = false;
try
{
{
string messageBody = txtbody.Text.Length > 1 ? txtbody.Text.ToString() : "";
string From = txtfrom.Text.Length > 1 ? txtfrom.Text.ToString() : "";
string To = txtTO.Text.Length > 1 ? txtTO.Text.ToString() : "";
string cc = txtCC.Text.Length > 1 ? txtCC.Text.ToString() : "";
string bcc = txtbcc.Text.Length > 1 ? txtbcc.Text.ToString() : "";
string subject = txtsubject.Text.Length > 1 ? txtsubject.Text.ToString() : "";
if (messageBody.Length > 1 && From.Length > 1 && To.Length > 1)
{
MailHelper.SendMail(From, To, bcc, cc, subject, messageBody, "");
//MailHelper.SendMail(DefaultEmail, DT.Rows[0]["CanEmail"].ToString(), EmailCC1, DT.Rows[0]["LogUserEmail"].ToString(), "Offer Letter", messageBody, filename);
// MailHelper.SendMail(, txt, EmailCC1, DT.Rows[0]["LogUserEmail"].ToString(), "Offer Letter", messageBody);
status = true;
}
else
{
status = false;
}
}
}
catch (Exception ex)
{
status = false;
}
finally
{
}
return status;
}
}
}
--------------------------------------------------------------------------------------------------------------
app config
--------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="goku@gmail.com">
<network host="smtpout.secureserver.net" port="45" userName="goku@gmail.com" password="dqdwdqwd"/>
</smtp>
</mailSettings>
</system.net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>