Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.77 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.Mail;
  3.  
  4. var fromAddress = new MailAddress("from@gmail.com", "From Name");
  5. var toAddress = new MailAddress("to@example.com", "To Name");
  6. const string fromPassword = "fromPassword";
  7. const string subject = "Subject";
  8. const string body = "Body";
  9.  
  10. var smtp = new SmtpClient
  11. {
  12. Host = "smtp.gmail.com",
  13. Port = 587,
  14. EnableSsl = true,
  15. DeliveryMethod = SmtpDeliveryMethod.Network,
  16. UseDefaultCredentials = false,
  17. Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  18. };
  19. using (var message = new MailMessage(fromAddress, toAddress)
  20. {
  21. Subject = subject,
  22. Body = body
  23. })
  24. {
  25. smtp.Send(message);
  26. }
  27.  
  28. using System.Net.Mail;
  29. using System.Net;
  30.  
  31. var fromAddress = new MailAddress("from@gmail.com", "From Name");
  32. var toAddress = new MailAddress("to@yahoo.com", "To Name");
  33. const string fromPassword = "password";
  34. const string subject = "test";
  35. const string body = "Hey now!!";
  36.  
  37. var smtp = new SmtpClient
  38. {
  39. Host = "smtp.gmail.com",
  40. Port = 587,
  41. EnableSsl = true,
  42. DeliveryMethod = SmtpDeliveryMethod.Network,
  43. Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
  44. Timeout = 20000
  45. };
  46. using (var message = new MailMessage(fromAddress, toAddress)
  47. {
  48. Subject = subject,
  49. Body = body
  50. })
  51. {
  52. smtp.Send(message);
  53. }
  54.  
  55. using System.Net;
  56. using System.Net.Mail;
  57.  
  58. public void email_send()
  59. {
  60. MailMessage mail = new MailMessage();
  61. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  62. mail.From = new MailAddress("your mail@gmail.com");
  63. mail.To.Add("to_mail@gmail.com");
  64. mail.Subject = "Test Mail - 1";
  65. mail.Body = "mail with attachment";
  66.  
  67. System.Net.Mail.Attachment attachment;
  68. attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
  69. mail.Attachments.Add(attachment);
  70.  
  71. SmtpServer.Port = 587;
  72. SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
  73. SmtpServer.EnableSsl = true;
  74.  
  75. SmtpServer.Send(mail);
  76.  
  77. }
  78.  
  79. using (MailMessage mail = new MailMessage())
  80. {
  81. mail.From = new MailAddress("email@gmail.com");
  82. mail.To.Add("somebody@domain.com");
  83. mail.Subject = "Hello World";
  84. mail.Body = "<h1>Hello</h1>";
  85. mail.IsBodyHtml = true;
  86. mail.Attachments.Add(new Attachment("C:\file.zip"));
  87.  
  88. using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
  89. {
  90. smtp.Credentials = new NetworkCredential("email@gmail.com", "password");
  91. smtp.EnableSsl = true;
  92. smtp.Send(mail);
  93. }
  94. }
  95.  
  96. // Include this.
  97. using System.Net.Mail;
  98.  
  99. string fromAddress = "xyz@gmail.com";
  100. string mailPassword = "*****"; // Mail id password from where mail will be sent.
  101. string messageBody = "Write the body of the message here.";
  102.  
  103.  
  104. // Create smtp connection.
  105. SmtpClient client = new SmtpClient();
  106. client.Port = 587;//outgoing port for the mail.
  107. client.Host = "smtp.gmail.com";
  108. client.EnableSsl = true;
  109. client.Timeout = 10000;
  110. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  111. client.UseDefaultCredentials = false;
  112. client.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);
  113.  
  114.  
  115. // Fill the mail form.
  116. var send_mail = new MailMessage();
  117.  
  118. send_mail.IsBodyHtml = true;
  119. //address from where mail will be sent.
  120. send_mail.From = new MailAddress("from@gmail.com");
  121. //address to which mail will be sent.
  122. send_mail.To.Add(new MailAddress("to@example.com");
  123. //subject of the mail.
  124. send_mail.Subject = "put any subject here";
  125.  
  126. send_mail.Body = messageBody;
  127. client.Send(send_mail);
  128.  
  129. using System;
  130. using System.Net;
  131. using System.Net.Mail;
  132.  
  133. namespace SendMailViaGmail
  134. {
  135. class Program
  136. {
  137. static void Main(string[] args)
  138. {
  139.  
  140. //Specify senders gmail address
  141. string SendersAddress = "Sendersaddress@gmail.com";
  142. //Specify The Address You want to sent Email To(can be any valid email address)
  143. string ReceiversAddress = "ReceiversAddress@yahoo.com";
  144. //Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
  145. const string SendersPassword = "Password";
  146. //Write the subject of ur mail
  147. const string subject = "Testing";
  148. //Write the contents of your mail
  149. const string body = "Hi This Is my Mail From Gmail";
  150.  
  151. try
  152. {
  153. //we will use Smtp client which allows us to send email using SMTP Protocol
  154. //i have specified the properties of SmtpClient smtp within{}
  155. //gmails smtp server name is smtp.gmail.com and port number is 587
  156. SmtpClient smtp = new SmtpClient
  157. {
  158. Host = "smtp.gmail.com",
  159. Port = 587,
  160. EnableSsl = true,
  161. DeliveryMethod = SmtpDeliveryMethod.Network,
  162. Credentials = new NetworkCredential(SendersAddress, SendersPassword),
  163. Timeout = 3000
  164. };
  165.  
  166. //MailMessage represents a mail message
  167. //it is 4 parameters(From,TO,subject,body)
  168.  
  169. MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
  170. /*WE use smtp sever we specified above to send the message(MailMessage message)*/
  171.  
  172. smtp.Send(message);
  173. Console.WriteLine("Message Sent Successfully");
  174. Console.ReadKey();
  175. }
  176.  
  177. catch (Exception ex)
  178. {
  179. Console.WriteLine(ex.Message);
  180. Console.ReadKey();
  181. }
  182. }
  183. }
  184. }
  185.  
  186. using System.Net.Mail;
  187.  
  188. MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
  189. SmtpClient client = new SmtpClient("smtp.gmail.com");
  190.  
  191. client.Port = Convert.ToInt16("587");
  192. client.Credentials = new System.Net.NetworkCredential("mail-id@gmail.com","password");
  193. client.EnableSsl = true;
  194.  
  195. client.Send(sendmsg);
  196.  
  197. public void SendEmail(string address, string subject, string message)
  198. {
  199. string email = "yrshaikh.mail@gmail.com";
  200. string password = "put-your-GMAIL-password-here";
  201.  
  202. var loginInfo = new NetworkCredential(email, password);
  203. var msg = new MailMessage();
  204. var smtpClient = new SmtpClient("smtp.gmail.com", 587);
  205.  
  206. msg.From = new MailAddress(email);
  207. msg.To.Add(new MailAddress(address));
  208. msg.Subject = subject;
  209. msg.Body = message;
  210. msg.IsBodyHtml = true;
  211.  
  212. smtpClient.EnableSsl = true;
  213. smtpClient.UseDefaultCredentials = false;
  214. smtpClient.Credentials = loginInfo;
  215. smtpClient.Send(msg);
  216. }
  217.  
  218. public void SendEmail(string address, string subject, string message)
  219. {
  220. Thread threadSendMails;
  221. threadSendMails = new Thread(delegate()
  222. {
  223.  
  224. //Place your Code here
  225.  
  226. });
  227. threadSendMails.IsBackground = true;
  228. threadSendMails.Start();
  229. }
  230.  
  231. using System.Threading;
  232.  
  233. MailMessage sendmsg = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
  234. SmtpClient client = new SmtpClient("smtp.gmail.com");
  235.  
  236. client.Port = Convert.ToInt32("587");
  237. client.EnableSsl = true;
  238. client.Credentials = new System.Net.NetworkCredential("mail-id@gmail.com","MyPassWord");
  239. client.Send(sendmsg);
  240.  
  241. using System.Net;
  242. using System.Net.Mail;
  243.  
  244. private void button1_Click(object sender, EventArgs e)
  245. {
  246. try
  247. {
  248. MailMessage mail = new MailMessage();
  249. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  250.  
  251. mail.From = new MailAddress("your_email_address@gmail.com");
  252. mail.To.Add("to_address");
  253. mail.Subject = "Test Mail";
  254. mail.Body = "This is for testing SMTP mail from GMAIL";
  255.  
  256. SmtpServer.Port = 587;
  257. SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
  258. SmtpServer.EnableSsl = true;
  259.  
  260. SmtpServer.Send(mail);
  261. MessageBox.Show("mail Send");
  262. }
  263. catch (Exception ex)
  264. {
  265. MessageBox.Show(ex.ToString());
  266. }
  267. }
  268.  
  269. msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));
  270.  
  271. using System;
  272. using System.Net;
  273. using System.Net.Mail;
  274.  
  275. namespace SendMailViaGmail
  276. {
  277. class Program
  278. {
  279. static void Main(string[] args)
  280. {
  281.  
  282. //Specify senders gmail address
  283. string SendersAddress = "Sendersaddress@gmail.com";
  284. //Specify The Address You want to sent Email To(can be any valid email address)
  285. string ReceiversAddress = "ReceiversAddress@yahoo.com";
  286. //Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
  287. const string SendersPassword = "Password";
  288. //Write the subject of ur mail
  289. const string subject = "Testing";
  290. //Write the contents of your mail
  291. const string body = "Hi This Is my Mail From Gmail";
  292.  
  293. try
  294. {
  295. //we will use Smtp client which allows us to send email using SMTP Protocol
  296. //i have specified the properties of SmtpClient smtp within{}
  297. //gmails smtp server name is smtp.gmail.com and port number is 587
  298. SmtpClient smtp = new SmtpClient
  299. {
  300. Host = "smtp.gmail.com",
  301. Port = 587,
  302. EnableSsl = true,
  303. DeliveryMethod = SmtpDeliveryMethod.Network,
  304. Credentials = new NetworkCredential(SendersAddress, SendersPassword),
  305. Timeout = 3000
  306. };
  307.  
  308. //MailMessage represents a mail message
  309. //it is 4 parameters(From,TO,subject,body)
  310.  
  311. MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
  312. /*WE use smtp sever we specified above to send the message(MailMessage message)*/
  313.  
  314. smtp.Send(message);
  315. Console.WriteLine("Message Sent Successfully");
  316. Console.ReadKey();
  317. }
  318. catch (Exception ex)
  319. {
  320. Console.WriteLine(ex.Message);
  321. Console.ReadKey();
  322. }
  323. }
  324. }
  325. }
  326.  
  327. public static string SendEmail(string To, string Subject, string Msg, bool bodyHtml = false, bool test = false, Stream AttachmentStream = null, string AttachmentType = null, string AttachmentFileName = null)
  328. {
  329. try
  330. {
  331. System.Net.Mail.MailMessage newMsg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailCfg"], To, Subject, Msg);
  332. newMsg.BodyEncoding = System.Text.Encoding.UTF8;
  333. newMsg.HeadersEncoding = System.Text.Encoding.UTF8;
  334. newMsg.SubjectEncoding = System.Text.Encoding.UTF8;
  335.  
  336. System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
  337. if (AttachmentStream != null && AttachmentType != null && AttachmentFileName != null)
  338. {
  339. System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(AttachmentStream, AttachmentFileName);
  340. System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
  341. disposition.FileName = AttachmentFileName;
  342. disposition.DispositionType = System.Net.Mime.DispositionTypeNames.Attachment;
  343.  
  344. newMsg.Attachments.Add(attachment);
  345. }
  346. if (test)
  347. {
  348. smtpClient.PickupDirectoryLocation = "C:\TestEmail";
  349. smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
  350. }
  351. else
  352. {
  353. //smtpClient.EnableSsl = true;
  354. }
  355.  
  356. newMsg.IsBodyHtml = bodyHtml;
  357. smtpClient.Send(newMsg);
  358. return SENT_OK;
  359. }
  360. catch (Exception ex)
  361. {
  362.  
  363. return "Error: " + ex.Message
  364. + "<br/><br/>Inner Exception: "
  365. + ex.InnerException;
  366. }
  367.  
  368. }
  369.  
  370. <appSettings>
  371. <add key="mailCfg" value="yourmail@example.com"/>
  372. </appSettings>
  373. <system.net>
  374. <mailSettings>
  375. <smtp deliveryMethod="Network" from="yourmail@example.com">
  376. <network defaultCredentials="false" host="mail.exapmple.com" userName="yourmail@example.com" password="your_password" port="25"/>
  377. </smtp>
  378. </mailSettings>
  379. </system.net>
  380.  
  381. public static bool Send(string receiverEmail, string ReceiverName, string subject, string body)
  382. {
  383. MailMessage mailMessage = new MailMessage();
  384. MailAddress mailAddress = new MailAddress("abc@gmail.com", "Sender Name"); // abc@gmail.com = input Sender Email Address
  385. mailMessage.From = mailAddress;
  386. mailAddress = new MailAddress(receiverEmail, ReceiverName);
  387. mailMessage.To.Add(mailAddress);
  388. mailMessage.Subject = subject;
  389. mailMessage.Body = body;
  390. mailMessage.IsBodyHtml = true;
  391.  
  392. SmtpClient mailSender = new SmtpClient("smtp.gmail.com", 587)
  393. {
  394. EnableSsl = true,
  395. UseDefaultCredentials = false,
  396. DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
  397. Credentials = new NetworkCredential("abc@gmail.com", "pass") // abc@gmail.com = input sender email address
  398. //pass = sender email password
  399. };
  400.  
  401. try
  402. {
  403. mailSender.Send(mailMessage);
  404. return true;
  405. }
  406. catch (SmtpFailedRecipientException ex)
  407. { }
  408. catch (SmtpException ex)
  409. { }
  410. finally
  411. {
  412. mailSender = null;
  413. mailMessage.Dispose();
  414. }
  415. return false;
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement