Send Email Class - TechNet Articles - United States (English) - TechNet Wiki
Description:
Generic class to send emails
Code:
public class EmailManager
{
private string server = string.Empty;
private string username = string.Empty;
private string password = string.Empty;
#region Properties
public string Server
{
get
{
return this.server;
}
set
{
this.server = value;
}
}
/// <summary>
/// Gets and Sets the username.
/// </summary>
public string Username
{
get
{
return this.username;
}
set
{
this.username = value;
}
}
/// <summary>
/// Gets and Set the password.
/// </summary>
publiiv class="full-post">
Send Email Classc string Password
{
get
{
return this.password;
}
set
{
this.password = value;
}
}
#endregion
#region Constructor
public EmailManager(string server, string username, string password)
{
this.Server = server;
this.Username = username;
this.Password = password;
}
#endregion
#region Public
public void Send(List<string> sendTo, List<string> sendCC, string sendFrom, string subject, string body)
{
MailMessage message = new MailMessage();
try
{
// Create a SMTP client object and set up credential properties
SmtpClient smtpClient = new SmtpClient(Server);
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
smtpClient.UseDefaultCredentials = true;
else
{
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(username, password);
smtpClient.Credentials = credentials;
}
foreach (string to in sendTo)
{
message.To.Add(to);
}
foreach (string cc in sendCC)
{
message.CC.Add(cc);
}
message.Subject = subject;
message.From = new System.Net.Mail.MailAddress(sendFrom);
message.Body = body;
message.IsBodyHtml = true;
smtpClient.Send(message);
message.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
message.Dispose();
}
}
#endregion
}