Guest User

Untitled

a guest
Jan 3rd, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. Create MailHelper.cs into an App_Code
  2. ---------------------------------------
  3. using System.Net.Mail;
  4.  
  5. public class MailHelper
  6. {
  7. /// <summary>
  8. /// Sends an mail message
  9. /// </summary>
  10. /// <param name="from">Sender address</param>
  11. /// <param name="to">Recepient address</param>
  12. /// <param name="bcc">Bcc recepient</param>
  13. /// <param name="cc">Cc recepient</param>
  14. /// <param name="subject">Subject of mail message</param>
  15. /// <param name="body">Body of mail message</param>
  16. public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
  17. {
  18. // Instantiate a new instance of MailMessage
  19. MailMessage mMailMessage = new MailMessage();
  20.  
  21. // Set the sender address of the mail message
  22. mMailMessage.From = new MailAddress(from);
  23. // Set the recepient address of the mail message
  24. mMailMessage.To.Add(new MailAddress(to));
  25.  
  26. // Check if the bcc value is null or an empty string
  27. if ((bcc != null) && (bcc != string.Empty))
  28. {
  29. // Set the Bcc address of the mail message
  30. mMailMessage.Bcc.Add(new MailAddress(bcc));
  31. } // Check if the cc value is null or an empty value
  32. if ((cc != null) && (cc != string.Empty))
  33. {
  34. // Set the CC address of the mail message
  35. mMailMessage.CC.Add(new MailAddress(cc));
  36. } // Set the subject of the mail message
  37. mMailMessage.Subject = subject;
  38. // Set the body of the mail message
  39. mMailMessage.Body = body;
  40.  
  41. // Set the format of the mail message body as HTML
  42. mMailMessage.IsBodyHtml = true;
  43. // Set the priority of the mail message to normal
  44. mMailMessage.Priority = MailPriority.Normal;
  45.  
  46. // Instantiate a new instance of SmtpClient
  47. SmtpClient mSmtpClient = new SmtpClient();
  48. mSmtpClient.EnableSsl = true;
  49. // Send the mail message
  50. mSmtpClient.Send(mMailMessage);
  51. }
  52. }
  53. ----------------------------------------------
  54. Web.Config
  55. add the following line
  56.  
  57.  
  58. <system.net>
  59. <mailSettings>
  60. <smtp from="youremail@gmail.com">
  61. <network host="smtp.gmail.com" port="587" userName="youremail" password="yoourpassword"/>
  62. </smtp>
  63. </mailSettings>
  64. </system.net>
  65. -----------------------------------
  66. Send email code
  67.  
  68. string body="At " + DateTime.Now + " feedback was sent from an ASP.NET " +
  69. "Web page. Below you will find the feedback message " +
  70. "send by " + txtName.Text + "." +
  71. "---------------------------------------" +txtMessage.Text;
  72. MailHelper.SendMailMessage("yourmail@gmail.com", txtEmail.Text, "", "", "Testing", body);
  73. panelSendEmail.Visible = false;
  74. panelMailSent.Visible = true;
Add Comment
Please, Sign In to add comment