Guest User

Untitled

a guest
Mar 14th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Mail;
  6. using System.Net;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var client = new SmtpClient("smtp.gmail.com", 587)
  15. {
  16. Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
  17. EnableSsl = true
  18. };
  19. client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
  20. Console.WriteLine("Sent");
  21. Console.ReadLine();
  22. }
  23. }
  24. }
  25.  
  26. Dim SMTPClientObj As New Net.Mail.SmtpClient
  27. SMTPClientObj.UseDefaultCredentials = False
  28. SMTPClientObj.Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "mypwd")
  29. SMTPClientObj.Host = "smtp.gmail.com"
  30. SMTPClientObj.Port = 587
  31. SMTPClientObj.EnableSsl = True
  32. SMTPClientObj.Send("myusername@gmail.com","yourusername@gmail.com","test","testbody")
  33.  
  34. <configuration>
  35.  
  36. <appSettings>
  37. <add key="EnableSSLOnMail" value="True"/>
  38. </appSettings>
  39.  
  40. <!-- other settings -->
  41.  
  42. ...
  43.  
  44. <!-- system.net settings -->
  45. <system.net>
  46. <mailSettings>
  47. <smtp from="yourusername@gmail.com" deliveryMethod="Network">
  48. <network
  49. defaultCredentials="false"
  50. host="smtp.gmail.com"
  51. port="587"
  52. password="stR0ngPassW0rd"
  53. userName="yourusername@gmail.com"
  54. />
  55. <!-- When using .Net 4.0 (or later) add attribute: EnableSSL="True" and you're all set-->
  56. </smtp>
  57. </mailSettings>
  58. </system.net>
  59. </configuration>
  60.  
  61. Partial Class RecoverPassword
  62. Inherits System.Web.UI.Page
  63.  
  64. Protected Sub RecoverPwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles RecoverPwd.SendingMail
  65. e.Message.Bcc.Add("webmaster@example.com")
  66. SSLMail.SendMail(e)
  67. End Sub
  68.  
  69. End Class
  70.  
  71. Partial Class RecoverPassword
  72. Inherits System.Web.UI.Page
  73.  
  74. Protected Sub RecoverPwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles RecoverPwd.SendingMail
  75. e.Message.Bcc.Add("webmaster@example.com")
  76. SSLMail.SendMail(e)
  77. End Sub
  78.  
  79. End Class
  80.  
  81. //Satrt Send Email Function
  82. public string SendMail(string toList, string from, string ccList, string subject, string body)
  83. {
  84.  
  85. MailMessage message = new MailMessage();
  86. SmtpClient smtpClient = new SmtpClient();
  87. string msg = string.Empty;
  88. try
  89. {
  90. MailAddress fromAddress = new MailAddress(from);
  91. message.From = fromAddress;
  92. message.To.Add(toList);
  93. if (ccList != null && ccList != string.Empty)
  94. message.CC.Add(ccList);
  95. message.Subject = subject;
  96. message.IsBodyHtml = true;
  97. message.Body = body;
  98. smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
  99. smtpClient.Port = 587;
  100. smtpClient.EnableSsl = true;
  101. smtpClient.UseDefaultCredentials = true;
  102. smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");
  103.  
  104. smtpClient.Send(message);
  105. msg = "Successful<BR>";
  106. }
  107. catch (Exception ex)
  108. {
  109. msg = ex.Message;
  110. }
  111. return msg;
  112. }
  113. //End Send Email Function
  114.  
  115. //Satrt Send Email Function
  116. public string SendMail(string toList, string from, string ccList, string subject, string body)
  117. {
  118.  
  119. MailMessage message = new MailMessage();
  120. SmtpClient smtpClient = new SmtpClient();
  121. string msg = string.Empty;
  122. try
  123. {
  124. MailAddress fromAddress = new MailAddress(from);
  125. message.From = fromAddress;
  126. message.To.Add(toList);
  127. if (ccList != null && ccList != string.Empty)
  128. message.CC.Add(ccList);
  129. message.Subject = subject;
  130. message.IsBodyHtml = true;
  131. message.Body = body;
  132. smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
  133. smtpClient.Port = 587;
  134. smtpClient.EnableSsl = true;
  135. smtpClient.UseDefaultCredentials = true;
  136. smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");
  137.  
  138. smtpClient.Send(message);
  139. msg = "Successful<BR>";
  140. }
  141. catch (Exception ex)
  142. {
  143. msg = ex.Message;
  144. }
  145. return msg;
  146. }
  147. //End Send Email Function
  148.  
  149. Response.Write(SendMail(recipient Address, "UserName@gmail.com", "ccList if any", "subject", "body"))
  150.  
  151. <system.net>
  152. <mailSettings>
  153. <smtp from="myusername@gmail.com" deliveryMethod="Network">
  154. <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" password="password" userName="myusername@gmail.com"/>
  155. </smtp>
  156. </mailSettings>
  157. </system.net>
Add Comment
Please, Sign In to add comment