Class EmailHandler
- Namespace
 - Dynamicweb.Mailing
 
- Assembly
 - Dynamicweb.Mailing.dll
 
Provides static members for sending e-mails with logging and Dynamicweb system settings for mail servers.
  
  public sealed class EmailHandler
  - Inheritance
 - 
      
      EmailHandler
 
- Inherited Members
 
Examples
using Dynamicweb.Logging;
using System.Net.Mail;
using System.Text;
namespace Dynamicweb.Mailing.Examples
{
    class EmailHandlerSample
    {
        public void SendMail()
        {
            bool sendSucceded;
            using (var mailMessage = new MailMessage())
            {
                mailMessage.Subject = "This is a test mail";
                mailMessage.From = new MailAddress(EmailHandler.SystemMailFromAddress(), "John Doe");
                mailMessage.To.Add("someone@gmail.com");
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = "<h1>Hi John</h1>Here is your message.";
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.HeadersEncoding = Encoding.UTF8;
                sendSucceded = EmailHandler.Send(mailMessage);
            }
            if (sendSucceded)
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails").Info("Mail successfully sent");
            }
            else
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails/Error").Error(("ERROR: Mail not sent"));
            }
        }
    }
}
  Remarks
Uses dropfolder if possible - otherwise fall back to direct SMTP protocol. Log can be found in /Files/Filer/MailLog/.NET/
  Properties
DefaultSmtpServer
Returns the first SMTP server from the control panel if more then one.
  
  public static string DefaultSmtpServer { get; }
  Property Value
Methods
GetEncodingByName(string)
Returns an encoding associated with the specified code page name.
  
  public static Encoding GetEncodingByName(string encoding)
  Parameters
encodingstring- The code page name of the preferred encoding, i.e. "utf-8".
 
Returns
SaveMail(string, MailMessage)
Saves the MailMessage to the specified folder and returns the name of the file.
  
  public static string SaveMail(string folder, MailMessage mailObj)
  Parameters
folderstring- The folder to save the MailMessage in.
 mailObjMailMessage- The MailMessage to save.
 
Returns
- string
 - The name of the saved file.
 
SaveMail(string, MailMessage, bool)
Saves the MailMessage to the specified folder and returns the name of the file.
  
  public static string SaveMail(string saveFolder, MailMessage mailObj, bool returnFileName)
  Parameters
saveFolderstring- The folder to save the MailMessage in.
 mailObjMailMessage- The MailMessage to save.
 returnFileNamebool- Indicates whether to return the name of the saved file. Not returning the name will increase performance inside loops considerably.
 
Returns
- string
 - The name of the saved file or String.Empty depending on the 
returnFileNameparameter. 
Send(MailMessage)
Sends the specified mail message.
  
  public static bool Send(MailMessage mailMessage)
  Parameters
mailMessageMailMessage- The mail message.
 
Returns
- bool
 trueif send was succesful; otherwisefalse.
Examples
using Dynamicweb.Logging;
using System.Net.Mail;
using System.Text;
namespace Dynamicweb.Mailing.Examples
{
    class EmailHandlerSample
    {
        public void SendMail()
        {
            bool sendSucceded;
            using (var mailMessage = new MailMessage())
            {
                mailMessage.Subject = "This is a test mail";
                mailMessage.From = new MailAddress(EmailHandler.SystemMailFromAddress(), "John Doe");
                mailMessage.To.Add("someone@gmail.com");
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = "<h1>Hi John</h1>Here is your message.";
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.HeadersEncoding = Encoding.UTF8;
                sendSucceded = EmailHandler.Send(mailMessage);
            }
            if (sendSucceded)
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails").Info("Mail successfully sent");
            }
            else
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails/Error").Error(("ERROR: Mail not sent"));
            }
        }
    }
}
  
  Send(MailMessage, bool)
Sends the specified mail message.
  
  public static bool Send(MailMessage mailMessage, bool logging)
  Parameters
mailMessageMailMessage- The mail message.
 loggingbool- Specifies whether logging should take place. Default is true.
 
Returns
- bool
 trueif send was succesful; otherwisefalse.
Examples
using Dynamicweb.Logging;
using System.Net.Mail;
using System.Text;
namespace Dynamicweb.Mailing.Examples
{
    class EmailHandlerSample
    {
        public void SendMail()
        {
            bool sendSucceded;
            using (var mailMessage = new MailMessage())
            {
                mailMessage.Subject = "This is a test mail";
                mailMessage.From = new MailAddress(EmailHandler.SystemMailFromAddress(), "John Doe");
                mailMessage.To.Add("someone@gmail.com");
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = "<h1>Hi John</h1>Here is your message.";
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.HeadersEncoding = Encoding.UTF8;
                sendSucceded = EmailHandler.Send(mailMessage);
            }
            if (sendSucceded)
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails").Info("Mail successfully sent");
            }
            else
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails/Error").Error(("ERROR: Mail not sent"));
            }
        }
    }
}
  
  Send(MailMessage, bool, bool)
Sends the specified mail message.
  
  public static bool Send(MailMessage mailMessage, bool logging, bool throwException)
  Parameters
mailMessageMailMessage- The mail message.
 loggingbool- Specifies whether logging should take place. Default is true.
 throwExceptionbool- Specifies whether any potential exceptions should be thrown. Default is false.
 
Returns
- bool
 trueif send was succesful; otherwisefalse.
Examples
using Dynamicweb.Logging;
using System.Net.Mail;
using System.Text;
namespace Dynamicweb.Mailing.Examples
{
    class EmailHandlerSample
    {
        public void SendMail()
        {
            bool sendSucceded;
            using (var mailMessage = new MailMessage())
            {
                mailMessage.Subject = "This is a test mail";
                mailMessage.From = new MailAddress(EmailHandler.SystemMailFromAddress(), "John Doe");
                mailMessage.To.Add("someone@gmail.com");
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = "<h1>Hi John</h1>Here is your message.";
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.HeadersEncoding = Encoding.UTF8;
                sendSucceded = EmailHandler.Send(mailMessage);
            }
            if (sendSucceded)
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails").Info("Mail successfully sent");
            }
            else
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails/Error").Error(("ERROR: Mail not sent"));
            }
        }
    }
}
  
  Send(MailMessage, bool, bool, string, string, int, bool, string)
Sends the specified mail message.
  
  public static bool Send(MailMessage mailMessage, bool logging, bool throwException, string username, string password, int port, bool useSsl, string host)
  Parameters
mailMessageMailMessage- The mail message.
 loggingbool- Specifies whether logging should take place. Default is true.
 throwExceptionbool- Specifies whether any potential exceptions should be thrown. Default is false.
 usernamestring- Specifies a username for the smpt server. Password must also be specified, otherwise ignored.
 passwordstring- Password for the username specified.
 portint- The SMTP server port. Default is 25.
 useSslbool- Specifies whether the connection should use SSL or not.
 hoststring- The list of smtp host to try - seperate by ;
 
Returns
- bool
 trueif send was succesful; otherwisefalse.
Examples
using Dynamicweb.Logging;
using System.Net.Mail;
using System.Text;
namespace Dynamicweb.Mailing.Examples
{
    class EmailHandlerSample
    {
        public void SendMail()
        {
            bool sendSucceded;
            using (var mailMessage = new MailMessage())
            {
                mailMessage.Subject = "This is a test mail";
                mailMessage.From = new MailAddress(EmailHandler.SystemMailFromAddress(), "John Doe");
                mailMessage.To.Add("someone@gmail.com");
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = "<h1>Hi John</h1>Here is your message.";
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.HeadersEncoding = Encoding.UTF8;
                sendSucceded = EmailHandler.Send(mailMessage);
            }
            if (sendSucceded)
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails").Info("Mail successfully sent");
            }
            else
            {
                //Log to /Files/System/Log/MyMails
                LogManager.Current.GetLogger("/MyMails/Error").Error(("ERROR: Mail not sent"));
            }
        }
    }
}
  
  SendTestMail(string, string, string, int, bool)
Send email to test smtp mail settings
  
  public static ApplicationResponse<string> SendTestMail(string username, string password, string hostname, int port, bool useSsl)
  Parameters
usernamestring- User name
 passwordstring- Password
 hostnamestring- Host name
 portint- Port
 useSslbool- Use SSL or not
 
Returns
SystemMailFrom()
public static MailAddress SystemMailFrom()
  Returns
SystemMailFromAddress()
Gets the system email address from settings for use as mail from for system emails
  
  public static string SystemMailFromAddress()
  Returns
- string
 - The email address found in /Globalsettings/Settings/CommonInformation/Email if it is valide, otherwise noreply@dynamicweb.dk