Class EmailHandler
- Namespace
- Dynamicweb.Mailing
- Assembly
- Dynamicweb.Core.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
stringThe 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
stringThe folder to save the MailMessage in.
mailObj
MailMessageThe 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
stringThe folder to save the MailMessage in.
mailObj
MailMessageThe MailMessage to save.
returnFileName
boolIndicates 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
MailMessageThe 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
MailMessageThe mail message.
logging
boolSpecifies 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
MailMessageThe mail message.
logging
boolSpecifies whether logging should take place. Default is true.
throwException
boolSpecifies 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
MailMessageThe mail message.
logging
boolSpecifies whether logging should take place. Default is true.
throwException
boolSpecifies whether any potential exceptions should be thrown. Default is false.
username
stringSpecifies a username for the smpt server. Password must also be specified, otherwise ignored.
password
stringPassword for the username specified.
port
intThe SMTP server port. Default is 25.
useSsl
boolSpecifies whether the connection should use SSL or not.
host
stringThe 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
stringUser name
password
stringPassword
hostname
stringHost name
port
intPort
useSsl
boolUse SSL or not
Returns
SystemMailFrom()
Gets the system email address from settings for use as mail from for system emails
public static MailAddress SystemMailFrom()
Returns
- MailAddress
The email address found in /Globalsettings/Settings/CommonInformation/Email if it is valide, otherwise noreply@dynamicweb.dk
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