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 System.Net.Mail;
using System.Text;
using Dynamicweb.Logging;
namespace Dynamicweb.Mailing.Examples;
public static class EmailHandlerSample
{
public static 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
encodingstringThe 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
folderstringThe folder to save the MailMessage in.
mailObjMailMessageThe 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
saveFolderstringThe folder to save the MailMessage in.
mailObjMailMessageThe MailMessage to save.
returnFileNameboolIndicates 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
mailMessageMailMessageThe mail message.
Returns
- bool
trueif send was succesful; otherwisefalse.
Examples
using System.Net.Mail;
using System.Text;
using Dynamicweb.Logging;
namespace Dynamicweb.Mailing.Examples;
public static class EmailHandlerSample
{
public static 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"));
}
}
}
Exceptions
- EmailException
Thrown when email sending fails.
Send(MailMessage, bool)
Sends the specified mail message.
public static bool Send(MailMessage mailMessage, bool logging)
Parameters
mailMessageMailMessageThe mail message.
loggingboolSpecifies whether logging should take place. Default is true.
Returns
- bool
trueif send was succesful; otherwisefalse.
Examples
using System.Net.Mail;
using System.Text;
using Dynamicweb.Logging;
namespace Dynamicweb.Mailing.Examples;
public static class EmailHandlerSample
{
public static 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"));
}
}
}
Exceptions
- EmailException
Thrown when email sending fails.
- ArgumentNullException
Thrown when
mailMessageisnull.
Send(MailMessage, bool, string, string, int, bool, string)
Sends the specified mail message.
public static bool Send(MailMessage mailMessage, bool logging, string username, string password, int port, bool useSsl, string host)
Parameters
mailMessageMailMessageThe mail message.
loggingboolSpecifies whether logging should take place. Default is true.
usernamestringSpecifies a username for the smpt server. Password must also be specified, otherwise ignored.
passwordstringPassword for the username specified.
portintThe SMTP server port. Default is 25.
useSslboolSpecifies whether the connection should use SSL or not.
hoststringThe list of smtp host to try - seperate by ;
Returns
- bool
trueif send was succesful; otherwisefalse.
Send(MailMessage, string)
Sends the specified mail message, if the given key has not already been used.
public static bool Send(MailMessage mailMessage, string key)
Parameters
mailMessageMailMessageThe mail message.
keystringThe key identifying previously sent emails.
Returns
- bool
trueif email was send succesfully; otherwisefalse.
SendTestMail(SmtpOptions)
Send email to test smtp mail settings
public static ApplicationResponse<string> SendTestMail(SmtpOptions options)
Parameters
optionsSmtpOptionsConfiguration options for SMTP
Returns
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
usernamestringUser name
passwordstringPassword
hostnamestringHost name
portintPort
useSslboolUse 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
TrySend(MailMessage, bool)
Attempts to send an email message without throwing exceptions on failure.
public static bool TrySend(MailMessage mailMessage, bool logging = true)
Parameters
mailMessageMailMessageThe mail message.
loggingboolSpecifies whether logging should take place. Default is true.
Returns
- bool
trueif send was succesful; otherwisefalse.