public class EmailUtility
{
public string FromEmailID { get; set; }
public string FromName { get; set; }
public string ToEmailID { get; set; }
public string CcEmailID { get; set; }
public string BccEmailID { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public bool IsBodyHtml { get; set; }
public List<String> AttachmentFiles { get; set; }
public string Error { get; set; }
public bool SendMail()
{
MailMessage mail = new MailMessage();
try
{
if (!String.IsNullOrEmpty(FromEmailID))
mail.From = new MailAddress(FromEmailID, FromName);
else
{
Error = "The Sender Address can't be empty.";
return false;
}
if (!String.IsNullOrEmpty(ToEmailID))
if (ToEmailID.Contains(";"))
{
string[] str = ToEmailID.Split(';');
for (int i = 0; i < str.Length; i++)
if (str[i] != String.Empty)
mail.To.Add(str[i]);
}
else
mail.To.Add(ToEmailID);
else
{
Error = "Recepient address can't be empty.";
return false;
}
if (!String.IsNullOrEmpty(BccEmailID))
if (BccEmailID.Contains(";"))
{
string[] str = BccEmailID.Split(';');
for (int i = 0; i < str.Length; i++)
if (str[i].Trim() != String.Empty)
mail.Bcc.Add(str[i]);
}
else
mail.Bcc.Add(BccEmailID);
if (!String.IsNullOrEmpty(CcEmailID))
if (CcEmailID.Contains(";"))
{
string[] str = CcEmailID.Split(';');
for (int i = 0; i < str.Length; i++)
if (str[i].Trim() != String.Empty)
mail.CC.Add(str[i]);
}
else
mail.CC.Add(CcEmailID);
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = IsBodyHtml;
mail.Priority = MailPriority.Normal;
if (AttachmentFiles != null)
foreach (string attachment in AttachmentFiles)
mail.Attachments.Add(new Attachment(attachment, MediaTypeNames.Application.Octet));
SmtpClient client = new SmtpClient();
client.Send(mail);
mail.Dispose();
if (AttachmentFiles != null && AttachmentFiles.Count > 0)
AttachmentFiles.Clear();
return true;
}
catch (Exception ex)
{
Error = ex.ToString();
mail.Dispose();
return false;
}
}
_______________________________________________________________________
Use this Function also
_______________________________________________________________________
public static EmailUtility SendActivationEmail(User user)
{
EmailUtility utility = new EmailUtility();
utility.FromEmailID = Utils.EmailNotification;
utility.FromName = Utils.EmailFromName;
utility.ToEmailID = user.Email;
utility.Subject = "Shades n Style - New Account Activation";
StringBuilder mailBody = new StringBuilder();
mailBody.Append("Thank you for registering with <br/>");
mailBody.AppendLine("Username: " + user .Email+ "<br/>");
mailBody.AppendLine("Password: " + user.Password + "<br/><br/>");
mailBody.AppendLine("This message was sent from an unmonitored email address. To contact us you can use the following page: <br/>");
mailBody.AppendLine(string.Format("<a href='{0}/contactus.aspx'>{0}/contactus.aspx</a> <br/><br/>", Utils.DomainName));
mailBody.AppendLine("The Shades n Style Team <br/>");
mailBody.AppendLine(string.Format("<a href='{0}'>{0}</a>", Utils.DomainName));
utility.Body = mailBody.ToString();
utility.IsBodyHtml = true;
utility.SendMail();
return utility;
}