Full sample code to send generic email from asp.net and c# code. This code is fully tested on local environment.
Code Snippet
- using System;
- using System.Web;
- using System.Web.Security;
- using System.Text;
- using System.Net.Mail;
- using System.Collections;
- using System.Collections.Generic;
- using AdsDataComponentTableAdapters;
- /// <summary>
- /// Summary description for Util
- /// </summary>
- namespace AspNet.Class.BusinessLogicLayer
- {
- public class Utilities
- {
- public Utilities()
- {
//
// 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
- string SitesenderAddress = Constants.EmailFromAddress;
- bool sent = false;
- SiteSettings s = SiteSettings.GetSharedSettings();
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("From Screen:" + FromScreen);
- sb.AppendLine("The contact information of the user is below:");
- sb.AppendLine();
- sb.AppendLine("Name: " + senderName);
- sb.AppendLine("Email: " + senderAddress);
- sb.AppendLine();
- sb.AppendLine("User comments/questions:");
- sb.AppendLine(comments);
- sb.AppendLine();
- sb.AppendLine("Extra information: " + extrabody);
- sb.AppendLine();
- sb.AppendLine("Mobile Number: " + number);
- sb.AppendLine();
- sb.AppendLine(s.SiteName);
- sb.AppendLine(ClassifiedsHttpApplication.SiteUrl);
- string from = String.Format("{0} <{1}>", senderName, SitesenderAddress);
- try
- {
- MailMessage m = new MailMessage(from, s.SiteEmailAddress);
- m.Subject = subject;
- m.Body = sb.ToString();
- SmtpClient client = new SmtpClient();
- client.Send(m);
- sent = true;
- }
- catch
- {
- // Consider customizing the message for the EmailNotSentPanel in the ShowAds page.
- sent = false;
- }
- return sent;
- }
- }
- }
0 comments:
Post a Comment