Send Gmail email using C#


/ Published in: C#
Save to your folder(s)

Notes:\r\n\r\n(1) your namespace\'s using section should include the following:\r\nusing System.Net.Mail;\r\n\r\n(2) my example code places the email-sending code in a Button\'s Click() event-handler


Copy this code and paste it in your HTML
  1. 1.
  2. private void button1_Click(object sender, EventArgs e) {
  3. 2.
  4. DialogResult dr = MessageBox.Show("Send email?", "Get Permission", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
  5. 3.
  6. MessageBoxDefaultButton.Button2);
  7. 4.
  8.  
  9. 5.
  10. if (dr == DialogResult.Yes) {
  11. 6.
  12.  
  13. 7.
  14. SmtpClient smtpClnt = new SmtpClient("smtp.gmail.com", 587);
  15. 8.
  16.  
  17. 9.
  18. try {
  19. 10.
  20.  
  21. 11.
  22. MailMessage oMail = new MailMessage();
  23. 12.
  24. oMail.To.Add(new MailAddress("[email protected]"));
  25. 13.
  26. oMail.From = new MailAddress("[email protected]");
  27. 14.
  28. oMail.IsBodyHtml = true;
  29. 15.
  30.  
  31. 16.
  32. oMail.Body = "your message body goes here";
  33. 17.
  34. oMail.Subject = "your subject goes here";
  35. 18.
  36.  
  37. 19.
  38. smtpClnt.Timeout = 100000;
  39. 20.
  40.  
  41. 21.
  42. smtpClnt.EnableSsl = true;
  43. 22.
  44. System.Net.NetworkCredential nc = new System.Net.NetworkCredential("[email protected]", "your_password");
  45. 23.
  46. smtpClnt.Credentials = nc;
  47. 24.
  48.  
  49. 25.
  50. Attachment oAttached = new Attachment("C:\\yourimage.jpg");
  51. 26.
  52. oMail.Attachments.Add(oAttached);
  53. 27.
  54.  
  55. 28.
  56. smtpClnt.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
  57. 29.
  58.  
  59. 30.
  60. smtpClnt.SendAsync(oMail, "sending");
  61. 31.
  62.  
  63. 32.
  64. }
  65. 33.
  66. catch (Exception mailExc) {
  67. 34.
  68. if (mailExc.ToString().IndexOf("could not be resolved") >= 0) {
  69. 35.
  70. int line = mailExc.ToString().IndexOf("line");
  71. 36.
  72. int leng = mailExc.ToString().Length;
  73. 37.
  74. string sLin = mailExc.ToString().Substring(line, (leng - line));
  75. 38.
  76. string s = "Either the developer's Gmail SMTP was unavailable, or else your computer is not currently ";
  77. 39.
  78. s += "connected to the internet. Error generated at source code " + sLin;
  79. 40.
  80. MessageBox.Show(s, "Unable to send character file via email");
  81. 41.
  82. }
  83. 42.
  84. else {
  85. 43.
  86. MessageBox.Show("Error:" + Environment.NewLine + Environment.NewLine +
  87. 44.
  88. mailExc.Message + Environment.NewLine + Environment.NewLine + mailExc.ToString());
  89. 45.
  90. }
  91. 46.
  92. }
  93. 47.
  94. finally {
  95. 48.
  96.  
  97. 49.
  98. //don't invoke smtpClnt.Dispose() here; if you do, the mail never sends
  99. 50.
  100. }
  101. 51.
  102.  
  103. 52.
  104.  
  105. 53.
  106. }
  107. 54.
  108. }
  109. 55.
  110.  
  111. 56.
  112.  
  113. 57.
  114.  
  115. 58.
  116. private void MailDeliveryComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e) {
  117. 59.
  118.  
  119. 60.
  120. string myMessage = e.ToString();
  121. 61.
  122. string myCaption = string.Empty;
  123. 62.
  124.  
  125. 63.
  126. if (e.Error != null) {
  127. 64.
  128. myCaption = "Error sending email";
  129. 65.
  130. }
  131. 66.
  132. else if (e.Cancelled) {
  133. 67.
  134. myCaption = "Sending of email cancelled.";
  135. 68.
  136. }
  137. 69.
  138. else {
  139. 70.
  140. myCaption = "Your email message was sent successfully to the Game Master.";
  141. 71.
  142. }
  143. 72.
  144.  
  145. 73.
  146. MessageBox.Show(myMessage, myCaption);
  147. 74.
  148.  
  149. 75.
  150. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.