Advertisement
Guest User

Untitled

a guest
Sep 25th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. MailAddress to = new MailAddress("abc@gmail.com,xyz@gmail.com");
  2.  
  3. MailMessage msg = new MailMessage();
  4. msg.Body = ....;
  5. msg.To.Add(...);
  6. msg.To.Add(...);
  7.  
  8. SmtpClient smtp = new SmtpClient();
  9. smtp.Send(msg);
  10.  
  11. MailAddress to = new MailAddress(
  12. String.Format("{0} <{1}>",display_name, address));
  13.  
  14. using System;
  15. using System.Net.Mail;
  16.  
  17. public class Test
  18. {
  19. public static void Main()
  20. {
  21. SmtpClient client = new SmtpClient("smtphost", 25);
  22. MailMessage msg = new MailMessage("x@y.com", "a@b.com,c@d.com");
  23. msg.Subject = "sdfdsf";
  24. msg.Body = "sdfsdfdsfd";
  25. client.UseDefaultCredentials = true;
  26. client.Send(msg);
  27.  
  28. }
  29. }
  30.  
  31. msg.To.Add(new MailAddress("your@email1.com","Your name 1"));
  32. msg.To.Add(new MailAddress("your@email2.com","Your name 2"));
  33.  
  34. //1.The ACCOUNT
  35. MailAddress fromAddress = new MailAddress("myaccount@myaccount.com", "my display name");
  36. String fromPassword = "password";
  37.  
  38. //2.The Destination email Addresses
  39. MailAddressCollection TO_addressList = new MailAddressCollection();
  40.  
  41. //3.Prepare the Destination email Addresses list
  42. foreach (var curr_address in mailto.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
  43. {
  44. MailAddress mytoAddress = new MailAddress(curr_address, "Custom display name");
  45. TO_addressList.Add(mytoAddress);
  46. }
  47.  
  48. //4.The Email Body Message
  49. String body = bodymsg;
  50.  
  51. //5.Prepare GMAIL SMTP: with SSL on port 587
  52. var smtp = new SmtpClient
  53. {
  54. Host = "smtp.gmail.com",
  55. Port = 587,
  56. EnableSsl = true,
  57. DeliveryMethod = SmtpDeliveryMethod.Network,
  58. Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
  59. Timeout = 30000
  60. };
  61.  
  62.  
  63. //6.Complete the message and SEND the email:
  64. using (var message = new MailMessage()
  65. {
  66. From = fromAddress,
  67. Subject = subject,
  68. Body = body,
  69. })
  70. {
  71. message.To.Add(TO_addressList.ToString());
  72. smtp.Send(message);
  73. }
  74.  
  75. namespace WebForms.Code.Logging {
  76.  
  77. public class ObserverLogToEmail: ILog {
  78. private string from;
  79. private string to;
  80. private string subject;
  81. private string body;
  82. private SmtpClient smtpClient;
  83. private MailMessage mailMessage;
  84. private MailPriority mailPriority;
  85. private MailAddressCollection mailAddressCollection;
  86. private MailAddress fromMailAddress, toMailAddress;
  87. public MailAddressCollection toMailAddressCollection {
  88. get;
  89. set;
  90. }
  91. public MailAddressCollection ccMailAddressCollection {
  92. get;
  93. set;
  94. }
  95. public MailAddressCollection bccMailAddressCollection {
  96. get;
  97. set;
  98. }
  99.  
  100. public ObserverLogToEmail(string from, string to, string subject, string body, SmtpClient smtpClient) {
  101. this.from = from;
  102. this.to = to;
  103. this.subject = subject;
  104. this.body = body;
  105.  
  106. this.smtpClient = smtpClient;
  107. }
  108.  
  109. public ObserverLogToEmail(MailAddress fromMailAddress, MailAddress toMailAddress,
  110. string subject, string content, SmtpClient smtpClient) {
  111. try {
  112. this.fromMailAddress = fromMailAddress;
  113. this.toMailAddress = toMailAddress;
  114. this.subject = subject;
  115. this.body = content;
  116.  
  117. this.smtpClient = smtpClient;
  118.  
  119. mailAddressCollection = new MailAddressCollection();
  120.  
  121. } catch {
  122. throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
  123. }
  124. }
  125.  
  126. public ObserverLogToEmail(MailAddressCollection fromMailAddressCollection,
  127. MailAddressCollection toMailAddressCollection,
  128. string subject, string content, SmtpClient smtpClient) {
  129. try {
  130. this.toMailAddressCollection = toMailAddressCollection;
  131. this.ccMailAddressCollection = ccMailAddressCollection;
  132. this.subject = subject;
  133. this.body = content;
  134.  
  135. this.smtpClient = smtpClient;
  136.  
  137. } catch {
  138. throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
  139. }
  140. }
  141.  
  142. public ObserverLogToEmail(MailAddressCollection toMailAddressCollection,
  143. MailAddressCollection ccMailAddressCollection,
  144. MailAddressCollection bccMailAddressCollection,
  145. string subject, string content, SmtpClient smtpClient) {
  146. try {
  147. this.toMailAddressCollection = toMailAddressCollection;
  148. this.ccMailAddressCollection = ccMailAddressCollection;
  149. this.bccMailAddressCollection = bccMailAddressCollection;
  150.  
  151. this.subject = subject;
  152. this.body = content;
  153.  
  154. this.smtpClient = smtpClient;
  155.  
  156. } catch {
  157. throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
  158. }
  159. }#region ILog Members
  160.  
  161. // sends a log request via email.
  162. // actual email 'Send' calls are commented out.
  163. // uncomment if you have the proper email privileges.
  164. public void Log(object sender, LogEventArgs e) {
  165. string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message;
  166. fromMailAddress = new MailAddress("", "HaNN", System.Text.Encoding.UTF8);
  167. toMailAddress = new MailAddress("", "XXX", System.Text.Encoding.UTF8);
  168.  
  169. mailMessage = new MailMessage(fromMailAddress, toMailAddress);
  170. mailMessage.Subject = subject;
  171. mailMessage.Body = body;
  172.  
  173. // commented out for now. you need privileges to send email.
  174. // _smtpClient.Send(from, to, subject, body);
  175. smtpClient.Send(mailMessage);
  176. }
  177.  
  178. public void LogAllEmails(object sender, LogEventArgs e) {
  179. try {
  180. string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message;
  181.  
  182. mailMessage = new MailMessage();
  183. mailMessage.Subject = subject;
  184. mailMessage.Body = body;
  185.  
  186. foreach(MailAddress toMailAddress in toMailAddressCollection) {
  187. mailMessage.To.Add(toMailAddress);
  188.  
  189. }
  190. foreach(MailAddress ccMailAddress in ccMailAddressCollection) {
  191. mailMessage.CC.Add(ccMailAddress);
  192. }
  193. foreach(MailAddress bccMailAddress in bccMailAddressCollection) {
  194. mailMessage.Bcc.Add(bccMailAddress);
  195. }
  196. if (smtpClient == null) {
  197. var smtp = new SmtpClient {
  198. Host = "smtp.gmail.com",
  199. Port = 587,
  200. EnableSsl = true,
  201. DeliveryMethod = SmtpDeliveryMethod.Network,
  202. Credentials = new NetworkCredential("yourEmailAddress", "yourPassword"),
  203. Timeout = 30000
  204. };
  205. } else smtpClient.SendAsync(mailMessage, null);
  206. } catch (Exception) {
  207.  
  208. throw;
  209. }
  210. }
  211. }
  212.  
  213. try
  214. {
  215. string s = textBox2.Text;
  216. string[] f = s.Split(',');
  217.  
  218. for (int i = 0; i < f.Length; i++)
  219. {
  220. MailMessage message = new MailMessage(); // Create instance of message
  221. message.To.Add(f[i]); // Add receiver
  222. message.From = new System.Net.Mail.MailAddress(c);// Set sender .In this case the same as the username
  223. message.Subject = label3.Text; // Set subject
  224. message.Body = richTextBox1.Text; // Set body of message
  225. client.Send(message); // Send the message
  226. message = null; // Clean up
  227. }
  228.  
  229. }
  230.  
  231. catch (Exception ex)
  232. {
  233.  
  234. MessageBox.Show(ex.Message);
  235. }
  236.  
  237. MailAddress fromAddress = new MailAddress (fromMail,fromName);
  238. MailAddress toAddress = new MailAddress(toMail,toName);
  239. MailMessage message = new MailMessage(fromAddress,toAddress);
  240. message.Subject = subject;
  241. message.Body = body;
  242. SmtpClient smtp = new SmtpClient()
  243. {
  244. Host = host, Port = port,
  245. enabHost = "smtp.gmail.com",
  246. Port = 25,
  247. EnableSsl = true,
  248. UseDefaultCredentials = true,
  249. Credentials = new NetworkCredentials (fromMail, password)
  250. };
  251. smtp.send(message);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement