Monday, May 07, 2007

Sending Mails using System.Net.Mail

XML Documentation helps in displaying the metadata for the dll users, so ne info thats required to be shared with DLL users can be commented here. Check "intMailFormat" parameter, users using this dll shud know wat exactly to be passed.
//----------------Use Full piece of code--------------------------------


/// <_summary>
/// Function overloaded without Attachments
///
/// <_param name="strTo">Receiver Mail ID
/// <_param name="strCC">Carbon Copy Receivers>
/// <_param name="strBCC">Blind Carbon Copy Receivers

/// <_param name="strFrom">Sender Mail ID
/// <_param name="strSubject">Mail Subject
/// <_param name="strBody">Mail Body
/// <_param name="intMailFormat">Mail Format expects 0 for Plain Text and 1 for HTML(HTML is the default)

#region Sending Mail without attachments
public static void sendMail(string strTo, string strCC, string strBCC, string strFrom, string strSubject, string strBody, int intMailFormat)
{
try
{
MailMessage MailMsg = new MailMessage();

MailMsg.From = new MailAddress(strFrom);
MailMsg.To.Add(strTo);
if (strCC.Trim() != "")
{
MailMsg.CC.Add(strCC);
}
if (strBCC.Trim() != "")
{
MailMsg.Bcc.Add(strBCC);
}
MailMsg.Subject = strSubject;
MailMsg.Body = strBody;
if (intMailFormat == 0)
{
MailMsg.IsBodyHtml = false;
}
else
{
MailMsg.IsBodyHtml = true;
}
SmtpClient client = new SmtpClient("localhost");
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
client.UseDefaultCredentials = true;
client.Send(MailMsg);
}
catch (Exception ex)
{
throw ex;
}
}

#endregion

No comments: