Create blank project first.
Create Global class.
namespace SendMail.BusinessEntity { public class Global {
#region +++ Global variables and constants declaration +++
//Constants for the configuration public const string CONFIGURATION_FILE_NAME = "SendMail.config"; #endregion
} }
Create EmailInfo class.
using System; using System.Collections; using System.Net.Mail;
namespace SendMail.BusinessEntity {
public class EmailInfo {
#region +++ Local variables and constants declaration +++
private Guid guidEmailID; private MailAddress maFrom; private MailAddressCollection macTo; private MailAddressCollection macCarbonCopy; private MailAddressCollection macBlindCarbonCopy; private MailAddress maSender; private MailAddress maReplyTo; private string strSubject; private string strMessageBody; private char cPriority; private bool bolIsBodyHtml = false; private DateTime dtEmailDate; private string[] strAttachmentFileName; private Attachment[] objAttachmentData;
#endregion
#region +++ Properties assigning and retrieving codes +++
public Guid EmailID { get { return guidEmailID; } set { guidEmailID = value; } }
public MailAddress From { get { return maFrom; } set { maFrom = value; } }
public MailAddressCollection To { get { return macTo; } set { macTo = value; } }
public MailAddressCollection Cc { get { return macCarbonCopy; } set { macCarbonCopy = value; } }
public MailAddressCollection Bcc { get { return macBlindCarbonCopy; } set { macBlindCarbonCopy = value; } }
public MailAddress Sender { get { return maSender; } set { maSender = value; } }
public MailAddress ReplyTo { get { return maReplyTo; } set { maReplyTo = value; } }
public string Subject { get { return strSubject; } set { strSubject = value; } }
public string MessageBody { get { return strMessageBody; } set { strMessageBody = value; } }
public char Priority { get { return cPriority; } set { cPriority = value; } }
public bool IsBodyHtml { get { return bolIsBodyHtml; } set { bolIsBodyHtml = value; } }
public DateTime EmailDate { get { return dtEmailDate; } set { dtEmailDate = value; } }
public string[] AttachmentFileName { get { return strAttachmentFileName; } set { strAttachmentFileName = value; } }
public Attachment[] AttachmentData { get { return objAttachmentData; } set { objAttachmentData = value; } }
#endregion
} }
Create SendMail class.
using System; using System.Configuration; using System.Net; using System.Net.Mail; using System.Net.Mime; using SendMail.BusinessEntity;
namespace SendMail {
public class SendEmail { public static bool bSend = false;
#region +++ Codes of private access functions +++
private SmtpServerInfo LoadSmtpConfigSetting() { string strConfigFileName; SmtpServerInfo objTemp; Configuration objConfigData; ExeConfigurationFileMap objConfigFile; KeyValueConfigurationCollection objAppSetting = null;
objTemp = new SmtpServerInfo();
try { // Get the application configuration file path. strConfigFileName = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + Global.CONFIGURATION_FILE_NAME;
//strConfigFileName = System.IO.Path.Combine(Environment.CurrentDirectory, Global.CONFIGURATION_FILE_NAME);
// Map to the application configuration file. objConfigFile = new ExeConfigurationFileMap(); objConfigFile.ExeConfigFilename = strConfigFileName; objConfigData = ConfigurationManager.OpenMappedExeConfiguration(objConfigFile, ConfigurationUserLevel.None);
// Get the AppSettings collection. objAppSetting = objConfigData.AppSettings.Settings; foreach (KeyValueConfigurationElement objKeyValueElement in objAppSetting) { switch (objKeyValueElement.Key) { case "SmtpHostName": objTemp.SmtpHost = objKeyValueElement.Value; break; case "SmtpPortNo": objTemp.SmtpPort = objKeyValueElement.Value; break; case "UserName": objTemp.UserName = objKeyValueElement.Value; break; case "Password": objTemp.Password = objKeyValueElement.Value; break; case "UseDefaultCredential": if (objKeyValueElement.Value == "false") objTemp.UseDefaultCredential = false; else objTemp.UseDefaultCredential = true; break; case "Timeout": objTemp.Timeout = Convert.ToInt32(objKeyValueElement.Value); break; } } } finally { objAppSetting.Clear(); objConfigData = null; objConfigFile = null; }
return objTemp; }
#endregion
#region +++ Codes of public access functions +++
public bool Send(EmailInfo EmailData) { // Checking the parameters if (EmailData == null) { throw new ArgumentNullException(); }
SmtpServerInfo objSmtpInfo = LoadSmtpConfigSetting(); return this.Send(EmailData, objSmtpInfo); }
public bool Send(EmailInfo[] EmailData) { // Checking the parameters if (EmailData == null) { throw new ArgumentNullException(); }
SmtpServerInfo objSmtpInfo = LoadSmtpConfigSetting(); return this.Send(EmailData, objSmtpInfo); }
public bool Send(EmailInfo EmailData, SmtpServerInfo SmtpData) { int iCount;
// Checking the parameters if (EmailData == null) { throw new ArgumentNullException(); } if (SmtpData == null) { throw new ArgumentNullException(); }
try { // MailMessage is used to represent the e-mail being sent using (MailMessage objMailMessage = new MailMessage()) { // Assign FROM property to the MailMessage if (EmailData.From == null) { throw new ArgumentNullException("Email data must have FROM property."); } else { objMailMessage.From = EmailData.From; } // Assign TO property to the MailMessage if (EmailData.To == null) { throw new ArgumentNullException("Email data must have TO property."); } else { for (iCount = 0; iCount <> { objMailMessage.To.Add(EmailData.To[iCount]); } } // Assign CC property to the MailMessage if (EmailData.Cc != null) { for (iCount = 0; iCount <> { objMailMessage.CC.Add(EmailData.Cc[iCount]); } } // Assign BCC property to the MailMessage if (EmailData.Bcc != null) { for (iCount = 0; iCount <> { objMailMessage.Bcc.Add(EmailData.Bcc[iCount]); } } // Assign Sender property to the MailMessage objMailMessage.Sender = EmailData.Sender; // Assign ReplyTo property to the MailMessage //objMailMessage.ReplyTo = EmailData.ReplyTo; // Assign Subject to the MailMessage if (EmailData.EmailID == Guid.Empty) { objMailMessage.Subject = EmailData.Subject; } else { objMailMessage.Subject = EmailData.EmailID.ToString() + "##" + EmailData.Subject; } // Assign email body to the MailMessage objMailMessage.Body = EmailData.MessageBody; objMailMessage.IsBodyHtml = EmailData.IsBodyHtml; switch (EmailData.Priority) { case '1': objMailMessage.Priority = MailPriority.High; break; case '2': objMailMessage.Priority = MailPriority.Normal; break; case '3': objMailMessage.Priority = MailPriority.Low; break; default: objMailMessage.Priority = MailPriority.Normal; break; }
// Create the file name attachment for this e-mail message if (EmailData.AttachmentFileName != null) { for (iCount = 0; iCount <> { string strFileName = EmailData.AttachmentFileName[iCount]; Attachment objAttachment = new Attachment(strFileName, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition objContentDisposition = objAttachment.ContentDisposition; objContentDisposition.CreationDate = System.IO.File.GetCreationTime(strFileName); objContentDisposition.ModificationDate = System.IO.File.GetLastWriteTime(strFileName); objContentDisposition.ReadDate = System.IO.File.GetLastAccessTime(strFileName); // Add the file attachment to this e-mail message. objMailMessage.Attachments.Add(objAttachment); } }
// Add binary attachment for this e-mail message if (EmailData.AttachmentData != null) { if (EmailData.AttachmentData.Length > 0) { for (iCount = 0; iCount <> { // Add the attachment to this e-mail message. string strFileName = EmailData.AttachmentData[iCount].Name.ToString(); EmailData.AttachmentData[iCount].ContentDisposition.CreationDate = System.IO.File.GetCreationTime(strFileName); EmailData.AttachmentData[iCount].ContentDisposition.ModificationDate = System.IO.File.GetLastWriteTime(strFileName); EmailData.AttachmentData[iCount].ContentDisposition.ReadDate = System.IO.File.GetLastAccessTime(strFileName); objMailMessage.Attachments.Add(EmailData.AttachmentData[iCount]); } } }
// SmtpClient is used to send the e-mail SmtpClient objSmtpClient = new SmtpClient(); objSmtpClient.Host = SmtpData.SmtpHost; objSmtpClient.Port = Convert.ToInt32(SmtpData.SmtpPort); if (SmtpData.UseDefaultCredential) { objSmtpClient.UseDefaultCredentials = true; } else { objSmtpClient.UseDefaultCredentials = false; objSmtpClient.Credentials = new NetworkCredential(SmtpData.UserName, SmtpData.Password); } objSmtpClient.Timeout = Convert.ToInt32(SmtpData.Timeout); // Send delivers the message to the mail server objSmtpClient.Send(objMailMessage); bSend = true; } } catch (FormatException ex) { Console.WriteLine(ex.Message); } catch (SmtpException ex) { Console.WriteLine(ex.Message); }
return bSend ; }
public bool Send(EmailInfo[] EmailData, SmtpServerInfo SmtpData) { // Checking the parameters if (EmailData == null) { throw new ArgumentNullException(); } if (SmtpData == null) { throw new ArgumentNullException(); }
bool bolFlag = false; try { foreach (EmailInfo objEmailInfo in EmailData) { this.Send(objEmailInfo, SmtpData); bolFlag = true; } } catch (Exception objException) { bolFlag = false; throw objException; }
return bolFlag; }
#endregion
}
}
Create SendMail class.
namespace SendMail.BusinessEntity { public class SmtpServerInfo {
#region +++ Local variables and constants declaration +++
private string strSmtpHost; private string strSmtpPort; private string strUserName; private string strPassword; private bool bolUseDefaultCredential; private int iTimeout;
#endregion
#region +++ Properties assigning and retrieving codes +++
public string SmtpHost { get { return strSmtpHost; } set { strSmtpHost = value; } }
public string SmtpPort { get { return strSmtpPort; } set { strSmtpPort = value; } }
public string UserName { get { return strUserName; } set { strUserName = value; } }
public string Password { get { return strPassword; } set { strPassword = value; } }
public bool UseDefaultCredential { get { return bolUseDefaultCredential; } set { bolUseDefaultCredential = value; } }
public int Timeout { get { return iTimeout; } set { iTimeout = value; } } #endregion
}
}
Create SendMail.config file. Put it under /bin folder. Need to add SmtpHostName value .
Add Form1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Mail; namespace SendMail { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { SendMail.BusinessEntity.EmailInfo objInfo = new SendMail.BusinessEntity.EmailInfo(); MailAddress maFrom = new MailAddress("sendmailaddress@xxxxx"); MailAddressCollection maTo = new MailAddressCollection(); maTo.Add("receivemailaddress@xxxxx"); //Attachment[] objAttachmentList;
objInfo.EmailID = Guid.NewGuid(); objInfo.From = maFrom; objInfo.To = maTo; objInfo.Subject = "SMTP Test Email"; objInfo.MessageBody = "This testing email is sent by program automatically.\r\nThis is next line.";
//objAttachmentList = new Attachment[1]; //objAttachmentList[0] = new Attachment("C:\\Attachment.txt");
SendMail.SendEmail clsSendEmail = new SendMail.SendEmail(); bool bSend = clsSendEmail.Send(objInfo);
if (bSend) MessageBox.Show("Mail Sent"); } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|