public static String Send(String senderAddress, String senderName, String recipient, String ccList, String bccList, String subject, String body, MailFormat bodyFormat, ArrayList attachments, ArrayList attachmentNames, Boolean useSsl)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
if (String.IsNullOrEmpty(senderName))
{
mail.From = new MailAddress(senderAddress);
}
else
{
mail.From = new MailAddress(senderAddress, senderName);
}
mail.Subject = subject;
mail.Body = body;
foreach (String address in recipient.Split(',')) mail.To.Add(address);
if (!String.IsNullOrEmpty(ccList))
{
foreach (String address in ccList.Split(',')) mail.CC.Add(address);
}
if (!String.IsNullOrEmpty(bccList))
{
foreach (String address in bccList.Split(',')) mail.Bcc.Add(address);
}
String result = "SUCCESS";
AlternateView newBody = AlternateView.CreateAlternateViewFromString(
body,
Encoding.ASCII,
"text/plain");
try
{
mail.IsBodyHtml = (bodyFormat == MailFormat.Html ? true : false);
if (bodyFormat == MailFormat.PlainText7Bit)
{
mail.Body = null;
newBody.TransferEncoding = TransferEncoding.SevenBit;
mail.AlternateViews.Add(newBody);
}
if (attachments != null)
{
if (attachments.Count > 0)
{
for(int x = 0; x < attachments.Count; x++)
{
System.Net.Mail.Attachment data = null;
if (attachments[x].GetType() == typeof(string) || attachments[x].GetType() == typeof(String))
{
data = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath((string)attachments[x]), MediaTypeNames.Application.Octet);
data.ContentId = attachments[x].ToString().Substring((attachments[x].ToString().LastIndexOf("/") == 0 ? 0 : attachments[x].ToString().LastIndexOf("/") + 1));
}
else if (attachments[x].GetType() == typeof(MemoryStream) && attachmentNames.Count == attachments.Count && attachmentNames[x] != null)
{
data = new System.Net.Mail.Attachment((MemoryStream)attachments[x], (string)attachmentNames[x]);
}
if (data != null)
{
mail.Attachments.Add(data);
}
}
}
}
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = useSsl;
smtp.Send( mail );
if (newBody != null)
{
newBody.Dispose();
}
mail.Dispose();
}
catch (Exception err)
{
result = err.ToString();
}
return result;
}