public static void Email(string to,
string body,
string subject,
string fromAddress,
string fromDisplay,
string credentialUser,
string credentialPassword,
params MailAttachment[] attachments)
{
string host = ConfigurationManager.AppSettings["SMTPHost"];
body = UpgradeEmailFormat(body);
try
{
MailMessage mail = new MailMessage();
mail.Body = body;
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.Priority = MailPriority.Normal;
foreach (MailAttachment ma in attachments)
{
mail.Attachments.Add(ma.File);
}
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
smtp.Host = host;
smtp.Send(mail);
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder(1024);
sb.Append("\nTo:" + to);
sb.Append("\nbody:" + body);
sb.Append("\nsubject:" + subject);
sb.Append("\nfromAddress:" + fromAddress);
sb.Append("\nfromDisplay:" + fromDisplay);
sb.Append("\ncredentialUser:" + credentialUser);
sb.Append("\ncredentialPasswordto:" + credentialPassword);
sb.Append("\nHosting:" + host);
ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
}
}
"
UpgradeEmailFormat
" and "
ErrorLog
" are generic routines; you don't need the former, and can write the later yourself!
"
MailAttachment
" is a simple class you only need if you want to send attachments:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Mime;
using System.Net.Mail;
public class MailAttachment
{
#region Fields
private MemoryStream stream;
private string filename;
private string mediaType;
#endregion
#region Properties
public Stream Data { get { return stream; } }
public string Filename { get { return filename; } }
public string MediaType { get { return mediaType; } }
public Attachment File{ get {return new Attachment(Data, Filename, MediaType); } }
#endregion
#region Constructors
public MailAttachment(byte[] data, string filename)
{
this.stream = new MemoryStream(data);
this.filename = filename;
this.mediaType = MediaTypeNames.Application.Octet;
}
public MailAttachment(string data, string filename)
{
this.stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(data));
this.filename = filename;
this.mediaType = MediaTypeNames.Text.Html;
}
#endregion
}