Thursday, August 12, 2010

MailManager

#region Mail Manager
public class MailManager
{
#region Properties

public string SenderEmail { get; set; }
public string RecipientEmail { get; set; }
public string CcToEmail { get; set; }
public string Subject { get; set; }
public string MailBody { get; set; }
public bool IsBodyHTML { get; set; }
public bool IsCCToSelf { get; set; }

#endregion

#region Send Mail
public void sendMail()
{
try
{
MailMessage mail = new MailMessage();
//if (string.IsNullOrEmpty(recipientEmail))
//{
// recipientEmail = ConfigurationManager.AppSettings.Get("AdminEmail");
//}

mail.To.Add(RecipientEmail);
mail.Subject = Subject;
mail.IsBodyHtml = IsBodyHTML;
if (!string.IsNullOrEmpty(CcToEmail))
{
mail.CC.Add(CcToEmail);
}
mail.Body = MailBody.ToString();
if (IsCCToSelf)
{
mail.Bcc.Add(new MailAddress(SenderEmail));
}

Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
SmtpClient smtp = new SmtpClient();
int port = mailSettings.Smtp.Network.Port;
string host = mailSettings.Smtp.Network.Host;
if (string.IsNullOrEmpty(SenderEmail))
{
mail.From = new MailAddress(mailSettings.Smtp.From);
}
else
{
mail.From = new MailAddress(SenderEmail);
}
string password = mailSettings.Smtp.Network.Password;
string username = mailSettings.Smtp.Network.UserName;
smtp.Host = host;
smtp.Send(mail);
}
}
catch (Exception)
{
throw;
}
finally
{
}
}
#endregion
}
#endregion


var smtp1 = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("fd@gmail.com", "s")
};

No comments:

Post a Comment