Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1.     public static bool sendMail(string[] to, string[] cc, string subject, string body, string attachFile, HttpContext context)
  2.     {
  3.         try
  4.         {
  5.             string filepath = WebConfigurationManager.AppSettings["folder_config"] + "Config.ini";
  6.             if (File.Exists(context.Server.MapPath(filepath)))
  7.             {
  8.                 IniParser parser = new IniParser(context.Server.MapPath(filepath));
  9.  
  10.                 string host = "";
  11.                 string port = "";
  12.                 string ssl = "";
  13.                 string username = "";
  14.                 string password = "";
  15.                 string bccEmail = "";
  16.                 string senderName = "";
  17.  
  18.                 host = parser.GetSetting("EmailServer", "Host").Trim();
  19.                 port = parser.GetSetting("EmailServer", "Port").Trim();
  20.                 ssl = parser.GetSetting("EmailServer", "EnableSsl").Trim();
  21.                 username = parser.GetSetting("EmailServer", "Username").Trim();
  22.                 password = parser.GetSetting("EmailServer", "Password").Trim();
  23.                 bccEmail = parser.GetSetting("EmailServer", "BCCEmail").Trim();
  24.                 senderName = parser.GetSetting("EmailServer", "SenderName").Trim();
  25.  
  26.                 if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(ssl) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
  27.                 {
  28.                     return false;
  29.                 }
  30.                 else
  31.                 {
  32.                     SmtpClient SmtpServer = new SmtpClient();
  33.                     SmtpServer.Credentials = new System.Net.NetworkCredential(username, password);
  34.                     SmtpServer.Port = Convert.ToInt32(port);
  35.                     SmtpServer.Host = host;
  36.                     SmtpServer.EnableSsl = ssl == "1" ? true : false;
  37.                     MailMessage message = new MailMessage();
  38.                     message.From = new MailAddress(username, senderName);
  39.                     foreach (string item in to)
  40.                     {
  41.                         if (checkEmail(item.Trim()))
  42.                         {
  43.                             message.To.Add(new MailAddress(item.Trim()));
  44.                         }
  45.                         else
  46.                         {
  47.                             return false;
  48.                         }
  49.                     }
  50.                     foreach (string item in cc)
  51.                     {
  52.                         if (checkEmail(item.Trim()))
  53.                         {
  54.                             message.CC.Add(new MailAddress(item.Trim()));
  55.                         }
  56.                     }
  57.                     if (!string.IsNullOrEmpty(attachFile))
  58.                     {
  59.                         message.Attachments.Add(new Attachment(attachFile));
  60.                     }
  61.                     message.Subject = subject;
  62.                     message.Body = body;
  63.                     message.IsBodyHtml = true;
  64.                     SmtpClient client = new SmtpClient();
  65.                     message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  66.                     try
  67.                     {
  68.                         SmtpServer.Send(message);
  69.                         return true;
  70.                     }
  71.                     catch (Exception ex)
  72.                     {
  73.                         IBE_Business.ClassStatic.clsBSSystemLog.insert(ex);
  74.                         return false;
  75.                     }
  76.                 }
  77.             }
  78.             else
  79.             {
  80.                 return false;
  81.             }
  82.         }
  83.         catch (Exception ex)
  84.         {
  85.             IBE_Business.ClassStatic.clsBSSystemLog.insert(ex);
  86.             return false;
  87.         }
  88.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement