Guest User

Untitled

a guest
Jun 28th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.Mail;
  3. using System.Net.Mime;
  4.  
  5. ...
  6. try
  7. {
  8.  
  9. SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");
  10.  
  11. // set smtp-client with basicAuthentication
  12. mySmtpClient.UseDefaultCredentials = false;
  13. System.Net.NetworkCredential basicAuthenticationInfo = new
  14. System.Net.NetworkCredential("username", "password");
  15. mySmtpClient.Credentials = basicAuthenticationInfo;
  16.  
  17. // add from,to mailaddresses
  18. MailAddress from = new MailAddress("test@example.com", "TestFromName");
  19. MailAddress to = new MailAddress("test2@example.com", "TestToName");
  20. MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
  21.  
  22. // add ReplyTo
  23. MailAddress replyto = new MailAddress("reply@example.com");
  24. myMail.ReplyTo = replyto;
  25.  
  26. // set subject and encoding
  27. myMail.Subject = "Test message";
  28. myMail.SubjectEncoding = System.Text.Encoding.UTF8;
  29.  
  30. // set body-message and encoding
  31. myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
  32. myMail.BodyEncoding = System.Text.Encoding.UTF8;
  33. // text or html
  34. myMail.IsBodyHtml = true;
  35.  
  36. mySmtpClient.Send(myMail);
  37. }
  38.  
  39. catch (SmtpException ex)
  40. {
  41. throw new ApplicationException
  42. ("SmtpException has occured: " + ex.Message);
  43. }
  44. catch (Exception ex)
  45. {
  46. throw ex;
  47. }
  48.  
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Text;
  52. using System.Threading;
  53. using System.Net.Mail;
  54.  
  55. namespace ConsoleApplication1
  56. {
  57. public class SendMail
  58. {
  59. string[] NameArray = new string[10] { "Recipient 1",
  60. "Recipient 2",
  61. "Recipient 3",
  62. "Recipient 4",
  63. "Recipient 5",
  64. "Recipient 6",
  65. "Recipient 7",
  66. "Recipient 8",
  67. "Recipient 9",
  68. "Recipient 10"
  69. };
  70.  
  71. public SendMail(int i, ManualResetEvent doneEvent)
  72. {
  73. Console.WriteLine("Started sending mail process for {0} - ", NameArray[i].ToString() + " at " + System.DateTime.Now.ToString());
  74. Console.WriteLine("");
  75. SmtpClient mailClient = new SmtpClient();
  76. mailClient.Host = Your host name;
  77. mailClient.UseDefaultCredentials = true;
  78. mailClient.Port = Your mail server port number; // try with default port no.25
  79.  
  80. MailMessage mailMessage = new MailMessage(FromAddress,ToAddress);//replace the address value
  81. mailMessage.Subject = "Testing Bulk mail application";
  82. mailMessage.Body = NameArray[i].ToString();
  83. mailMessage.IsBodyHtml = true;
  84. mailClient.Send(mailMessage);
  85. Console.WriteLine("Mail Sent succesfully for {0} - ",NameArray[i].ToString() + " at " + System.DateTime.Now.ToString());
  86. Console.WriteLine("");
  87.  
  88. _doneEvent = doneEvent;
  89. }
  90.  
  91. public void ThreadPoolCallback(Object threadContext)
  92. {
  93. int threadIndex = (int)threadContext;
  94. Console.WriteLine("Thread process completed for {0} ...",threadIndex.ToString() + "at" + System.DateTime.Now.ToString());
  95. _doneEvent.Set();
  96. }
  97.  
  98. private ManualResetEvent _doneEvent;
  99. }
  100.  
  101.  
  102. public class Program
  103. {
  104. static int TotalMailCount, Mailcount, AddCount, Counter, i, AssignI;
  105. static void Main(string[] args)
  106. {
  107. TotalMailCount = 10;
  108. Mailcount = TotalMailCount / 2;
  109. AddCount = Mailcount;
  110. InitiateThreads();
  111.  
  112. Thread.Sleep(100000);
  113. }
  114.  
  115. static void InitiateThreads()
  116. {
  117. //One event is used for sending mails for each person email id as batch
  118. ManualResetEvent[] doneEvents = new ManualResetEvent[Mailcount];
  119.  
  120. // Configure and launch threads using ThreadPool:
  121. Console.WriteLine("Launching thread Pool tasks...");
  122.  
  123. for (i = AssignI; i < Mailcount; i++)
  124. {
  125. doneEvents[i] = new ManualResetEvent(false);
  126. SendMail SRM_mail = new SendMail(i, doneEvents[i]);
  127. ThreadPool.QueueUserWorkItem(SRM_mail.ThreadPoolCallback, i);
  128. }
  129.  
  130. Thread.Sleep(10000);
  131.  
  132. // Wait for all threads in pool to calculation...
  133. //try
  134. //{
  135. // // WaitHandle.WaitAll(doneEvents);
  136. //}
  137. //catch(Exception e)
  138. //{
  139. // Console.WriteLine(e.ToString());
  140. //}
  141.  
  142. Console.WriteLine("All mails are sent in this thread pool.");
  143. Counter = Counter+1;
  144. Console.WriteLine("Please wait while we check for the next thread pool queue");
  145. Thread.Sleep(5000);
  146. CheckBatchMailProcess();
  147. }
  148.  
  149. static void CheckBatchMailProcess()
  150. {
  151.  
  152. if (Counter < 2)
  153. {
  154. Mailcount = Mailcount + AddCount;
  155. AssignI = Mailcount - AddCount;
  156. Console.WriteLine("Starting the Next thread Pool");
  157.  
  158. Thread.Sleep(5000);
  159. InitiateThreads();
  160. }
  161.  
  162. else
  163. {
  164. Console.WriteLine("No thread pools to start - exiting the batch mail application");
  165. Thread.Sleep(1000);
  166. Environment.Exit(0);
  167. }
  168. }
  169. }
  170.  
  171. using System.Net.Mail
  172.  
  173. new SmtpClient("smtp.server.com", 25).send("from@email.com",
  174. "to@email.com",
  175. "subject",
  176. "body");
  177.  
  178. MailMessage message = new MailMessage(
  179. "fromemail@contoso.com",
  180. "toemail@contoso.com",
  181. "Subject goes here",
  182. "Body goes here");
  183.  
  184. SmtpClient client = new SmtpClient(server);
  185. client.Send(message);
Add Comment
Please, Sign In to add comment