Friday, August 2, 2013

send mail from gmail in an ASP.NET project

From C# 3.5 with System.Net.Mail:

private void SendEmailOldSchoolStyle(string report)
{
   var fromAddress = new MailAddress("fevercheese@gmail.com", "Fever Cheese");
   var toAddress = new MailAddress("tomjaeschke@tomjaeschke.com", "Tom Jaeschke");
   var smtp = new SmtpClient
   {
      Host = "smtp.gmail.com",
      Port = 587,
      EnableSsl = true,
      UseDefaultCredentials = false,
      Credentials = new System.Net.NetworkCredential("fevercheese@gmail.com",
            "dumdeedum")
   };
   using (var message = new MailMessage(fromAddress, toAddress)
   {
      Subject = "made it",
      Body = report
   })
   {
      smtp.Send(message);
   }
}

 
 

From log4net:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
   <authentication value="Basic" />
   <to value="tomjaeschke@tomjaeschke.com" />
   <from value="fevercheese@gmail.com" />
   <username value="fevercheese@gmail.com" />
   <password value="dumdeedum" />
   <subject value="AtHand.ErrorEmailAlert" />
   <smtpHost value="smtp.gmail.com" />
   <port value="587" />
   <EnableSsl value="true"/>
   <bufferSize value="1" />
   <lossy value="false" />
   <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="FATAL"/>
   </evaluator>
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
   </layout>
</appender>

No comments:

Post a Comment