Tuesday, 7 July 2009

C# How to send email

using System.Net.Mail;

public class Email
{
public static void Send(string smtpHost, string from, string to, string cc,
string subject, string body, MailPriority priority, bool isBodyHTML)
{
SmtpClient client = new SmtpClient(smtpHost);
client.UseDefaultCredentials = true;

MailMessage message = new MailMessage();
message.From = new MailAddress(from);
message.Subject = subject;
message.Body = body;
message.Priority = priority;
message.IsBodyHtml = isBodyHTML;

string[] addressesTo = to.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string address in addressesTo)
{
message.To.Add(new MailAddress(address));
}

if (cc != null)
{
string[] addressesCc = cc.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string address in addressesCc)
{
message.CC.Add(new MailAddress(address));
}
}

client.Send(message);
}
}

No comments:

Post a Comment