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
encoding
string- 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
folder
string- The folder to save the MailMessage in.
mailObj
MailMessage- 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
saveFolder
string- The folder to save the MailMessage in.
mailObj
MailMessage- The MailMessage to save.
returnFileName
bool- 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
returnFileName
parameter.
Send(MailMessage)
Sends the specified mail message.
public static bool Send(MailMessage mailMessage)
Parameters
mailMessage
MailMessage- The mail message.
Returns
- bool
true
if 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
mailMessage
MailMessage- The mail message.
logging
bool- Specifies whether logging should take place. Default is true.
Returns
- bool
true
if 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
mailMessage
MailMessage- The mail message.
logging
bool- Specifies whether logging should take place. Default is true.
throwException
bool- Specifies whether any potential exceptions should be thrown. Default is false.
Returns
- bool
true
if 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
mailMessage
MailMessage- The mail message.
logging
bool- Specifies whether logging should take place. Default is true.
throwException
bool- Specifies whether any potential exceptions should be thrown. Default is false.
username
string- Specifies a username for the smpt server. Password must also be specified, otherwise ignored.
password
string- Password for the username specified.
port
int- The SMTP server port. Default is 25.
useSsl
bool- Specifies whether the connection should use SSL or not.
host
string- The list of smtp host to try - seperate by ;
Returns
- bool
true
if 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
username
string- User name
password
string- Password
hostname
string- Host name
port
int- Port
useSsl
bool- 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