Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static bool sendMail(string[] to, string[] cc, string subject, string body, string attachFile, HttpContext context)
- {
- try
- {
- string filepath = WebConfigurationManager.AppSettings["folder_config"] + "Config.ini";
- if (File.Exists(context.Server.MapPath(filepath)))
- {
- IniParser parser = new IniParser(context.Server.MapPath(filepath));
- string host = "";
- string port = "";
- string ssl = "";
- string username = "";
- string password = "";
- string bccEmail = "";
- string senderName = "";
- host = parser.GetSetting("EmailServer", "Host").Trim();
- port = parser.GetSetting("EmailServer", "Port").Trim();
- ssl = parser.GetSetting("EmailServer", "EnableSsl").Trim();
- username = parser.GetSetting("EmailServer", "Username").Trim();
- password = parser.GetSetting("EmailServer", "Password").Trim();
- bccEmail = parser.GetSetting("EmailServer", "BCCEmail").Trim();
- senderName = parser.GetSetting("EmailServer", "SenderName").Trim();
- if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(ssl) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
- {
- return false;
- }
- else
- {
- SmtpClient SmtpServer = new SmtpClient();
- SmtpServer.Credentials = new System.Net.NetworkCredential(username, password);
- SmtpServer.Port = Convert.ToInt32(port);
- SmtpServer.Host = host;
- SmtpServer.EnableSsl = ssl == "1" ? true : false;
- MailMessage message = new MailMessage();
- message.From = new MailAddress(username, senderName);
- foreach (string item in to)
- {
- if (checkEmail(item.Trim()))
- {
- message.To.Add(new MailAddress(item.Trim()));
- }
- else
- {
- return false;
- }
- }
- foreach (string item in cc)
- {
- if (checkEmail(item.Trim()))
- {
- message.CC.Add(new MailAddress(item.Trim()));
- }
- }
- if (!string.IsNullOrEmpty(attachFile))
- {
- message.Attachments.Add(new Attachment(attachFile));
- }
- message.Subject = subject;
- message.Body = body;
- message.IsBodyHtml = true;
- SmtpClient client = new SmtpClient();
- message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- try
- {
- SmtpServer.Send(message);
- return true;
- }
- catch (Exception ex)
- {
- IBE_Business.ClassStatic.clsBSSystemLog.insert(ex);
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- }
- catch (Exception ex)
- {
- IBE_Business.ClassStatic.clsBSSystemLog.insert(ex);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement