Sponsored Ad

Sunday, November 15, 2009

C#.NET Send Email Sample code

Full sample code to send generic email from asp.net and c# code. This code is fully tested on local environment.

Code Snippet
  1.     using System;
  2.  
  3. using System.Web;
  4.  
  5. using System.Web.Security;
  6.  
  7. using System.Text;
  8.  
  9. using System.Net.Mail;
  10.  
  11. using System.Collections;
  12.  
  13. using System.Collections.Generic;
  14.  
  15. using AdsDataComponentTableAdapters;
  16.  
  17. /// <summary>
  18.  
  19. /// Summary description for Util
  20.  
  21. /// </summary>
  22.  
  23. namespace AspNet.Class.BusinessLogicLayer
  24.  
  25. {
  26.  
  27. public class Utilities
  28.  
  29. {
  30.  
  31. public Utilities()
  32.  
  33. {

//

// TODO: Add constructor logic here

//

}

public static bool SendEmail(string senderName, string senderAddress, string comments, string subject, string extrabody, string number, string FromScreen )

{

//change to work email

Code Snippet
  1. string SitesenderAddress = Constants.EmailFromAddress;
  2.  
  3. bool sent = false;
  4.  
  5. SiteSettings s = SiteSettings.GetSharedSettings();
  6.  
  7. StringBuilder sb = new StringBuilder();
  8.  
  9. sb.AppendLine("From Screen:" + FromScreen);
  10.  
  11. sb.AppendLine("The contact information of the user is below:");
  12.  
  13. sb.AppendLine();
  14.  
  15. sb.AppendLine("Name: " + senderName);
  16.  
  17. sb.AppendLine("Email: " + senderAddress);
  18.  
  19. sb.AppendLine();
  20.  
  21. sb.AppendLine("User comments/questions:");
  22.  
  23. sb.AppendLine(comments);
  24.  
  25. sb.AppendLine();
  26.  
  27. sb.AppendLine("Extra information: " + extrabody);
  28.  
  29. sb.AppendLine();
  30.  
  31. sb.AppendLine("Mobile Number: " + number);
  32.  
  33. sb.AppendLine();
  34.  
  35. sb.AppendLine(s.SiteName);
  36.  
  37. sb.AppendLine(ClassifiedsHttpApplication.SiteUrl);
  38.  
  39. string from = String.Format("{0} <{1}>", senderName, SitesenderAddress);
  40.  
  41. try
  42.  
  43. {
  44.  
  45. MailMessage m = new MailMessage(from, s.SiteEmailAddress);
  46.  
  47. m.Subject = subject;
  48.  
  49. m.Body = sb.ToString();
  50.  
  51. SmtpClient client = new SmtpClient();
  52.  
  53. client.Send(m);
  54.  
  55. sent = true;
  56.  
  57. }
  58.  
  59. catch
  60.  
  61. {
  62.  
  63. // Consider customizing the message for the EmailNotSentPanel in the ShowAds page.
  64.  
  65. sent = false;
  66.  
  67. }
  68.  
  69. return sent;
  70.  
  71. }
  72.  
  73. }
  74.  
  75. }

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers