public static void SendMessage(string server,
string from, string to, string cc, string bcc,
string subject, string body, List<string> attachments,
string[] embeddedFiles)
{
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
string[] ccArray = cc.Split(';');
if (ccArray.Length > 0)
{
foreach (string address in ccArray)
{
if (address.Length > 0)
{
message.CC.Add(new MailAddress(address));
}
}
}
string[] bccArray = bcc.Split(';');
if (bccArray.Length > 0)
{
foreach (string address in bccArray)
{
if (address.Length > 0)
{
message.Bcc.Add(new MailAddress(address));
}
}
}
message.IsBodyHtml = true;
if (null != embeddedFiles && embeddedFiles.Length > 0)
{
foreach (string filePath in embeddedFiles)
{
string fileName = Path.GetFileName(filePath);
body = body.Replace(fileName, "cid:" + fileName);
}
AlternateView view = AlternateView.CreateAlternateViewFromString(body,
null,MediaTypeNames.Text.Html);
foreach (string filePath in embeddedFiles) {
LinkedResource res = new LinkedResource(filePath);
res.ContentId = Path.GetFileName(filePath);
view.LinkedResources.Add(res);
}
message.AlternateViews.Add(view);
} else {
message.Body = body;
}
foreach (string file in attachments) {
Attachment data = new Attachment(file);
message.Attachments.Add(data);
}
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}