Posted by : Jebastin Saturday 14 December 2013

C#:

  1. public bool Send_Email(string Subject, string Body, string ToEmail, string AttachmentUrl)
  2. {
  3. bool Sent = false;
  4. try
  5. {
  6. if (!string.IsNullOrEmpty(ToEmail))
  7. {
  8. string FromEmail = "", Username = "", Password = "", Host = "", Port = "";
  9. FromEmail = Convert.ToString(ConfigurationManager.AppSettings["FromEmail"]);
  10. Username = Convert.ToString(ConfigurationManager.AppSettings["Username"]);
  11. Password = Convert.ToString(ConfigurationManager.AppSettings["Password"]);
  12. Host = Convert.ToString(ConfigurationManager.AppSettings["Host"]);
  13. Port = Convert.ToString(ConfigurationManager.AppSettings["Port"]);
  14. bool UseSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["UseSsl"]);
  15. System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
  16. System.Net.NetworkCredential cred = new System.Net.NetworkCredential(Username, Password);
  17. if (ToEmail.Contains(','))
  18. {
  19. string[] ToEmailArray = ToEmail.Split(',');
  20. foreach (string si in ToEmailArray)
  21. {
  22.     mail.To.Add(si);
  23. }
  24. }
  25. else
  26. {
  27. mail.To.Add(ToEmail);
  28. }
  29. mail.Subject = Subject;
  30. mail.From = new System.Net.Mail.MailAddress(FromEmail, Convert.ToString(ConfigurationManager.AppSettings["FromName"]));
  31. mail.IsBodyHtml = true;
  32. mail.Body = Body;
  33. if (!string.IsNullOrEmpty(AttachmentUrl))
  34. {
  35. System.Net.Mail.Attachment Attachment;
  36. if (AttachmentUrl.Contains(','))
  37. {
  38.     string[] AttachmentUrlArray = ToEmail.Split(',');
  39.     foreach (string url in AttachmentUrlArray)
  40.     {
  41.         string strUrl = Uri.EscapeUriString(url);
  42.         if (File.Exists(strUrl))
  43.         {
  44.             Attachment = new System.Net.Mail.Attachment(strUrl);
  45.             mail.Attachments.Add(Attachment);
  46.         }
  47.     }
  48. }
  49. else
  50. {
  51.     string strUrl = Uri.EscapeUriString(AttachmentUrl);
  52.     if (File.Exists(strUrl))
  53.     {
  54.         Attachment = new System.Net.Mail.Attachment(strUrl);
  55.         mail.Attachments.Add(Attachment);
  56.     }
  57. }
  58. }
  59. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
  60. smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  61. smtp.UseDefaultCredentials = false;
  62. smtp.EnableSsl = UseSsl;
  63. smtp.Credentials = cred;
  64. smtp.Host = Host;
  65. smtp.Port = Convert.ToInt32(Port);
  66. smtp.Send(mail);
  67. Sent = true;
  68. }
  69. }
  70. catch
  71. {
  72. Sent = false;
  73. }
  74. return Sent;
  75. }

Configuration in appSettings.config:

  1.  <!-- Email Data Configuration Start -->
  2.   <add key="FromName" value="Company Name or Any Text"/>
  3.   <add key="FromEmail" value="example@gmail.com"/>
  4.   <add key="Username" value="example@gmail.com"/>
  5.   <add key="Password" value="password"/>
  6.   <add key="Host" value="smtp.gmail.com"/>
  7.   <add key="Port" value="587"/>
  8.   <add key="UseSsl" value="true"/>
  9.   <add key="ToEmail" value="example@gmail.com"/>
  10.   <!-- Email Data Configuration End -->

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -