Advertisement
rstx2

sendMail

Nov 4th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Mail;
  7. using System.IO;
  8. using System.Net;
  9. using System.Security.Cryptography.X509Certificates;
  10. using System.Net.Security;
  11.  
  12. namespace ms_web.Library
  13. {
  14. class MailGenerator
  15. {
  16. public string host;
  17. public int port;
  18. public string sender;
  19.  
  20. public MailGenerator()
  21. {
  22. host = "10.1.0.9";
  23. port = 25;
  24. sender = "noreply.hrp@tsh.ssbshoes.com";
  25.  
  26. ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  27. { return true; };
  28. }
  29.  
  30.  
  31. public void send(string subject, string body, string[] receivers, string[] cc, string[] file)
  32. {
  33. MailMessage message = new MailMessage();
  34.  
  35. foreach (string email in receivers)
  36. {
  37. message.To.Add(email);
  38. }
  39.  
  40. foreach (string email in cc)
  41. {
  42. message.CC.Add(email);
  43. }
  44.  
  45. message.From = new MailAddress(sender);
  46. message.Subject = subject;
  47. message.Body = body;
  48. message.IsBodyHtml = true;
  49.  
  50. if (file.Length > 0)
  51. {
  52. foreach (var item in file)
  53. {
  54. Attachment attachment = new Attachment(item);
  55. message.Attachments.Add(attachment);
  56. }
  57. }
  58. executeMail(message);
  59. }
  60.  
  61. private void executeMail(MailMessage message)
  62. {
  63. SmtpClient client = new SmtpClient(host, port);
  64.  
  65. try
  66. {
  67. client.UseDefaultCredentials = false;
  68. client.Credentials = new NetworkCredential("tsh\'restu.adi", "p@55w0rd");
  69. //client.EnableSsl = true;
  70. client.Send(message);
  71. }
  72. catch (SmtpException ex)
  73. {
  74. string msg = ex.Message;
  75.  
  76. if (ex.InnerException != null)
  77. {
  78. msg += "\n" + ex.InnerException.Message;
  79. }
  80. }
  81. }
  82. }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement