public static string SendMail(string fromAddress, string fromDisplayName, string[] toAddresses, string[] ccAddresses, string[] bccAddresses, string subject, string body, string SmtpHost, int port, string[] attachments, Encoding contentEncoding)
{
string result = "";
try
{
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateCertificate);
int i = 0;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromAddress, fromDisplayName);
for (i = 0; i < toAddresses.Length; i++)
{
mailMessage.To.Add(toAddresses[i]);
}
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.Priority = MailPriority.High;
mailMessage.BodyEncoding = contentEncoding;
mailMessage.IsBodyHtml = true;
if (ccAddresses != null)
{
for (i = 0; i < ccAddresses.Length; i++)
{
mailMessage.CC.Add(ccAddresses[i]);
}
}
if (bccAddresses != null)
{
for (i = 0; i < bccAddresses.Length; i++)
{
mailMessage.Bcc.Add(bccAddresses[i]);
}
}
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
// SmtpClient smtpClient = null;
//if (!string.IsNullOrEmpty(SmtpHost) && port != 0)
//smtpClient = new SmtpClient(SmtpHost, port);
// else
// smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("
[email protected]", "sha2sha2sha");
smtpClient.EnableSsl = true;
if (attachments != null)
{
for (i = 0; i < attachments.Length; i++)
{
mailMessage.Attachments.Add(new Attachment(attachments[i]));
}
}
smtpClient.Send(mailMessage);
result = "Success";
}
catch (Exception ex)
{
result = "Failure due to " + ex.Message;
}
return result;
}