From 51812af2d19739404b3dbeb246f118e420951419 Mon Sep 17 00:00:00 2001
From: "deepsource-autofix[bot]"
<62050782+deepsource-autofix[bot]@users.noreply.github.com>
Date: Thu, 28 Aug 2025 02:18:41 +0000
Subject: [PATCH 1/2] refactor: enable ssl for smtp connections
This PR updates the email sending implementation to ensure that all SmtpClient instances use SSL by default, improving the security of SMTP communications.
- Consider using SSL when dealing with SMTP: DeepSource identified that SmtpClient objects were created without SSL enabled, leading to potential plaintext transmission of emails. We modified both the primary Send and the RetrySend backup logic to instantiate SmtpClient with EnableSsl = true, ensuring encrypted connections for all SMTP operations.
> This Autofix was generated by AI. Please review the change before merging.
---
mojoPortal.Net/Email.cs | 287 ++++++++++++++++++++--------------------
1 file changed, 146 insertions(+), 141 deletions(-)
diff --git a/mojoPortal.Net/Email.cs b/mojoPortal.Net/Email.cs
index de861fdc1..5eb4c8f8d 100644
--- a/mojoPortal.Net/Email.cs
+++ b/mojoPortal.Net/Email.cs
@@ -760,106 +760,108 @@ public static bool Send(SmtpSettings smtpSettings, MailMessage message)
{
return Send(smtpSettings, message, out _);
}
- public static bool Send(SmtpSettings smtpSettings, MailMessage message, out string result)
- {
- if (message.To.ToString() == "admin@admin.com")
- {
- //demo site
- result = "can't use admin@admin.com email address";
- return false;
- }
-
- string globalBcc = GetGlobalBccAddress();
- if (globalBcc.Length > 0)
- {
- MailAddress bcc = new MailAddress(globalBcc);
- message.Bcc.Add(bcc);
- }
-
- int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
- SmtpClient smtpClient = new SmtpClient(smtpSettings.Server, smtpSettings.Port);
- smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtpClient.EnableSsl = smtpSettings.UseSsl;
- smtpClient.Timeout = timeoutMilliseconds;
-
- if (smtpSettings.RequiresAuthentication)
- {
-
- NetworkCredential smtpCredential
- = new NetworkCredential(
- smtpSettings.User,
- smtpSettings.Password);
-
- CredentialCache myCache = new CredentialCache();
- myCache.Add(smtpSettings.Server, smtpSettings.Port, "LOGIN", smtpCredential);
-
- smtpClient.UseDefaultCredentials = false;
- smtpClient.Credentials = myCache;
- }
- else
- {
- //aded 2010-01-22 JA
- smtpClient.UseDefaultCredentials = true;
- }
-
- foreach (var header in smtpSettings.AdditionalHeaders)
- {
- message.Headers.Add(header.Name, header.Value);
- }
-
- //message.Headers.Add(smtpSettings.AdditionalHeaders);
- if (!string.IsNullOrWhiteSpace(smtpSettings.SenderHeader))
- message.Headers.Add("X-mojo-Sender", smtpSettings.SenderHeader);
-
- try
- {
- smtpClient.Send(message);
- //log.Debug("Sent Message: " + subject);
- //log.Info("Sent Message: " + subject);
-
- bool logEmail = ConfigHelper.GetBoolProperty("LogAllEmailsWithSubject", false);
-
- if (logEmail)
- {
- log.Info("Sent message " + message.Subject + " to " + message.To[0].Address);
- }
- result = "sent";
- return true;
- }
- catch (System.Net.Mail.SmtpException ex)
- {
- //log.Error("error sending email to " + to + " from " + from, ex);
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", will retry", ex);
- return RetrySend(message, smtpClient, ex);
-
- }
- catch (WebException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
- catch (SocketException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
- catch (InvalidOperationException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
- catch (FormatException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
-
- }
+ public static bool Send(SmtpSettings smtpSettings, MailMessage message, out string result)
+ {
+ if (message.To.ToString() == "admin@admin.com")
+ {
+ //demo site
+ result = "can't use admin@admin.com email address";
+ return false;
+ }
+
+ string globalBcc = GetGlobalBccAddress();
+ if (globalBcc.Length > 0)
+ {
+ MailAddress bcc = new MailAddress(globalBcc);
+ message.Bcc.Add(bcc);
+ }
+
+ int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
+ SmtpClient smtpClient = new SmtpClient(smtpSettings.Server, smtpSettings.Port)
+ {
+ EnableSsl = true
+ };
+ smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
+ smtpClient.Timeout = timeoutMilliseconds;
+
+ if (smtpSettings.RequiresAuthentication)
+ {
+
+ NetworkCredential smtpCredential
+ = new NetworkCredential(
+ smtpSettings.User,
+ smtpSettings.Password);
+
+ CredentialCache myCache = new CredentialCache();
+ myCache.Add(smtpSettings.Server, smtpSettings.Port, "LOGIN", smtpCredential);
+
+ smtpClient.UseDefaultCredentials = false;
+ smtpClient.Credentials = myCache;
+ }
+ else
+ {
+ //aded 2010-01-22 JA
+ smtpClient.UseDefaultCredentials = true;
+ }
+
+ foreach (var header in smtpSettings.AdditionalHeaders)
+ {
+ message.Headers.Add(header.Name, header.Value);
+ }
+
+ //message.Headers.Add(smtpSettings.AdditionalHeaders);
+ if (!string.IsNullOrWhiteSpace(smtpSettings.SenderHeader))
+ message.Headers.Add("X-mojo-Sender", smtpSettings.SenderHeader);
+
+ try
+ {
+ smtpClient.Send(message);
+ //log.Debug("Sent Message: " + subject);
+ //log.Info("Sent Message: " + subject);
+
+ bool logEmail = ConfigHelper.GetBoolProperty("LogAllEmailsWithSubject", false);
+
+ if (logEmail)
+ {
+ log.Info("Sent message " + message.Subject + " to " + message.To[0].Address);
+ }
+ result = "sent";
+ return true;
+ }
+ catch (System.Net.Mail.SmtpException ex)
+ {
+ //log.Error("error sending email to " + to + " from " + from, ex);
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", will retry", ex);
+ return RetrySend(message, smtpClient, ex);
+
+ }
+ catch (WebException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+ catch (SocketException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+ catch (InvalidOperationException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+ catch (FormatException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+
+ }
private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex)
{
@@ -867,47 +869,50 @@ private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex
}
- private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex, out string result)
- {
- //retry
- int timesToRetry = ConfigHelper.GetIntProperty("TimesToRetryOnSmtpError", 3);
- for (int i = 1; i <= timesToRetry; )
- {
- if (RetrySend(message, smtp, i)) { result = "sent"; return true; }
- i += 1;
- Thread.Sleep(1000); // 1 second sleep in case it is a temporary network issue
- }
-
- // allows use of localhost as backup
- if (ConfigurationManager.AppSettings["BackupSmtpServer"] != null)
- {
- string backupServer = ConfigurationManager.AppSettings["BackupSmtpServer"];
- int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
- int backupSmtpPort = ConfigHelper.GetIntProperty("BackupSmtpPort", 25);
- SmtpClient smtpClient = new SmtpClient(backupServer, backupSmtpPort);
- smtpClient.UseDefaultCredentials = true;
-
- try
- {
- smtpClient.Send(message);
- log.Info("success using backup smtp server sending email to " + message.To.ToString() + " from " + message.From);
- result = "sent";
- return true;
- }
- catch (System.Net.Mail.SmtpException) { }
- catch (WebException) { }
- catch (SocketException) { }
- catch (InvalidOperationException) { }
- catch (FormatException) { }
-
- }
-
- //log.Info("all retries failed sending email to " + message.To.ToString() + " from " + message.From);
- log.Error("all retries failed sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- result = "fail";
- return false;
-
- }
+ private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex, out string result)
+ {
+ //retry
+ int timesToRetry = ConfigHelper.GetIntProperty("TimesToRetryOnSmtpError", 3);
+ for (int i = 1; i <= timesToRetry; )
+ {
+ if (RetrySend(message, smtp, i)) { result = "sent"; return true; }
+ i += 1;
+ Thread.Sleep(1000); // 1 second sleep in case it is a temporary network issue
+ }
+
+ // allows use of localhost as backup
+ if (ConfigurationManager.AppSettings["BackupSmtpServer"] != null)
+ {
+ string backupServer = ConfigurationManager.AppSettings["BackupSmtpServer"];
+ int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
+ int backupSmtpPort = ConfigHelper.GetIntProperty("BackupSmtpPort", 25);
+ SmtpClient smtpClient = new SmtpClient(backupServer, backupSmtpPort)
+ {
+ EnableSsl = true
+ };
+ smtpClient.UseDefaultCredentials = true;
+
+ try
+ {
+ smtpClient.Send(message);
+ log.Info("success using backup smtp server sending email to " + message.To.ToString() + " from " + message.From);
+ result = "sent";
+ return true;
+ }
+ catch (System.Net.Mail.SmtpException) { }
+ catch (WebException) { }
+ catch (SocketException) { }
+ catch (InvalidOperationException) { }
+ catch (FormatException) { }
+
+ }
+
+ //log.Info("all retries failed sending email to " + message.To.ToString() + " from " + message.From);
+ log.Error("all retries failed sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ result = "fail";
+ return false;
+
+ }
private static bool RetrySend(MailMessage message, SmtpClient smtp, int tryNumber)
{
From a935c8057b7707455f4876c1d222dd794abbbbeb Mon Sep 17 00:00:00 2001
From: "deepsource-autofix[bot]"
<62050782+deepsource-autofix[bot]@users.noreply.github.com>
Date: Thu, 28 Aug 2025 02:20:05 +0000
Subject: [PATCH 2/2] style: format code with dotnet-format
This commit fixes the style issues introduced in 51812af according to the output
from dotnet-format.
Details: https://github.com/QuackatronHQ/mojoportal/pull/7
---
mojoPortal.Net/Email.cs | 596 ++++++++++++++++++++--------------------
1 file changed, 299 insertions(+), 297 deletions(-)
diff --git a/mojoPortal.Net/Email.cs b/mojoPortal.Net/Email.cs
index 5eb4c8f8d..2e30deda4 100644
--- a/mojoPortal.Net/Email.cs
+++ b/mojoPortal.Net/Email.cs
@@ -12,12 +12,12 @@
namespace mojoPortal.Net
{
- ///
- /// A class for sending email.
- ///
- public static class Email
+ ///
+ /// A class for sending email.
+ ///
+ public static class Email
{
-
+
private static readonly ILog log = LogManager.GetLogger(typeof(Email));
private static bool debugLog = log.IsDebugEnabled;
@@ -27,9 +27,9 @@ public static class Email
const int SmtpAuthenticated = 1;
-
-
+
+
public static void SendEmail(
SmtpSettings smtpSettings,
string from,
@@ -43,7 +43,7 @@ public static void SendEmail(
{
if (to == "admin@admin.com") { return; } //demo site
- if ((ConfigurationManager.AppSettings["DisableSmtp"] != null)&&(ConfigurationManager.AppSettings["DisableSmtp"] == "true"))
+ if ((ConfigurationManager.AppSettings["DisableSmtp"] != null) && (ConfigurationManager.AppSettings["DisableSmtp"] == "true"))
{
log.Info("Not Sending email because DisableSmtp is true in config.");
return;
@@ -58,10 +58,10 @@ public static void SendEmail(
SendEmailNormal(smtpSettings, from, to, cc, bcc, subject, messageBody, html, priority);
return;
-
+
}
-
+
public static void SendEmail(
SmtpSettings smtpSettings,
string from,
@@ -88,7 +88,7 @@ public static void SendEmail(
return;
}
-
+
if (replyTo.Length > 0)
{
SendEmailNormal(
@@ -215,7 +215,7 @@ public static void SendEmailNormal(
attachmentPaths,
attachmentNames);
-
+
}
@@ -366,26 +366,26 @@ public static bool Send(
priority,
attachmentPaths,
attachmentNames);
-
+
}
- public static bool Send(
- SmtpSettings smtpSettings,
- string from,
- string fromAlias,
- string replyTo,
- string to,
- string cc,
- string bcc,
- string subject,
- string messageBody,
- bool html,
- string priority)
- {
- return Send(smtpSettings, from, fromAlias, replyTo, to, cc, bcc, subject, messageBody, html, priority, out _);
- }
-
-
- public static bool Send(
+ public static bool Send(
+ SmtpSettings smtpSettings,
+ string from,
+ string fromAlias,
+ string replyTo,
+ string to,
+ string cc,
+ string bcc,
+ string subject,
+ string messageBody,
+ bool html,
+ string priority)
+ {
+ return Send(smtpSettings, from, fromAlias, replyTo, to, cc, bcc, subject, messageBody, html, priority, out _);
+ }
+
+
+ public static bool Send(
SmtpSettings smtpSettings,
string from,
string fromAlias,
@@ -397,17 +397,18 @@ public static bool Send(
string messageBody,
bool html,
string priority,
- out string result)
+ out string result)
{
- if (to == "admin@admin.com") {
- //demo site
- result = "can't use admin@admin.com email address";
- return false;
- }
+ if (to == "admin@admin.com")
+ {
+ //demo site
+ result = "can't use admin@admin.com email address";
+ return false;
+ }
string[] attachmentPaths = new string[0];
string[] attachmentNames = new string[0];
- //result = string.Empty;
+ //result = string.Empty;
return Send(
smtpSettings,
from,
@@ -422,44 +423,44 @@ public static bool Send(
priority,
attachmentPaths,
attachmentNames,
- out result);
+ out result);
}
- public static bool Send(
- SmtpSettings smtpSettings,
- string from,
- string fromAlias,
- string replyTo,
- string to,
- string cc,
- string bcc,
- string subject,
- string messageBody,
- bool html,
- string priority,
- string[] attachmentPaths,
- string[] attachmentNames)
- {
- return Send(smtpSettings,
- from,
- fromAlias,
- replyTo,
- to,
- cc,
- bcc,
- subject,
- messageBody,
- html,
- priority,
- attachmentPaths,
- attachmentNames,
- out _);
- }
-
- ///
- /// This method uses the built in .NET classes to send mail.
- ///
- public static bool Send(
+ public static bool Send(
+ SmtpSettings smtpSettings,
+ string from,
+ string fromAlias,
+ string replyTo,
+ string to,
+ string cc,
+ string bcc,
+ string subject,
+ string messageBody,
+ bool html,
+ string priority,
+ string[] attachmentPaths,
+ string[] attachmentNames)
+ {
+ return Send(smtpSettings,
+ from,
+ fromAlias,
+ replyTo,
+ to,
+ cc,
+ bcc,
+ subject,
+ messageBody,
+ html,
+ priority,
+ attachmentPaths,
+ attachmentNames,
+ out _);
+ }
+
+ ///
+ /// This method uses the built in .NET classes to send mail.
+ ///
+ public static bool Send(
SmtpSettings smtpSettings,
string from,
string fromAlias,
@@ -473,65 +474,65 @@ public static bool Send(
string priority,
string[] attachmentPaths,
string[] attachmentNames,
- out string result)
+ out string result)
{
-
- // add attachments if there are any
- List attachments = new List();
- if ((attachmentPaths.Length > 0) && (attachmentNames.Length == attachmentPaths.Length))
+
+ // add attachments if there are any
+ List attachments = new List();
+ if ((attachmentPaths.Length > 0) && (attachmentNames.Length == attachmentPaths.Length))
+ {
+ for (int i = 0; i < attachmentPaths.Length; i++)
{
- for (int i = 0; i < attachmentPaths.Length; i++)
+ if (!File.Exists(attachmentPaths[i]))
{
- if (!File.Exists(attachmentPaths[i]))
- {
- log.Error("could not find file for email attachment " + attachmentPaths[i]);
- continue;
- }
-
- Attachment a = new Attachment(attachmentPaths[i]);
- a.Name = attachmentNames[i];
- //mail.Attachments.Add(a);
- attachments.Add(a);
-
+ log.Error("could not find file for email attachment " + attachmentPaths[i]);
+ continue;
}
+ Attachment a = new Attachment(attachmentPaths[i]);
+ a.Name = attachmentNames[i];
+ //mail.Attachments.Add(a);
+ attachments.Add(a);
+
}
- return Send(
- smtpSettings,
- from,
- fromAlias,
- replyTo,
- to,
- cc,
- bcc,
- subject,
- messageBody,
- html,
- priority,
- attachments,
- out result);
+ }
+
+ return Send(
+ smtpSettings,
+ from,
+ fromAlias,
+ replyTo,
+ to,
+ cc,
+ bcc,
+ subject,
+ messageBody,
+ html,
+ priority,
+ attachments,
+ out result);
}
- public static bool Send(
- SmtpSettings smtpSettings,
- string from,
- string fromAlias,
- string replyTo,
- string to,
- string cc,
- string bcc,
- string subject,
- string messageBody,
- bool html,
- string priority,
- List attachments)
- {
- return Send(smtpSettings, from, fromAlias, replyTo, to, cc, bcc, subject, messageBody, html, priority, attachments, out _);
- }
-
- public static bool Send(
+ public static bool Send(
+ SmtpSettings smtpSettings,
+ string from,
+ string fromAlias,
+ string replyTo,
+ string to,
+ string cc,
+ string bcc,
+ string subject,
+ string messageBody,
+ bool html,
+ string priority,
+ List attachments)
+ {
+ return Send(smtpSettings, from, fromAlias, replyTo, to, cc, bcc, subject, messageBody, html, priority, attachments, out _);
+ }
+
+ public static bool Send(
SmtpSettings smtpSettings,
string from,
string fromAlias,
@@ -544,25 +545,26 @@ public static bool Send(
bool html,
string priority,
List attachments,
- out string result)
+ out string result)
{
- if (to == "admin@admin.com") {
- //demo site
- result = "can't use admin@admin.com email address";
- return false;
- }
+ if (to == "admin@admin.com")
+ {
+ //demo site
+ result = "can't use admin@admin.com email address";
+ return false;
+ }
if ((ConfigurationManager.AppSettings["DisableSmtp"] != null) && (ConfigurationManager.AppSettings["DisableSmtp"] == "true"))
{
- result = "Not Sending email because DisableSmtp is true in config.";
+ result = "Not Sending email because DisableSmtp is true in config.";
log.Info(result);
- return false;
+ return false;
}
if ((smtpSettings == null) || (!smtpSettings.IsValid))
{
- result = "Invalid smtp settings detected in Email.Send ";
- log.Error(result);
+ result = "Invalid smtp settings detected in Email.Send ";
+ log.Error(result);
return false;
}
@@ -587,16 +589,16 @@ public static bool Send(
}
catch (ArgumentException)
{
- result = $"invalid from address {from}";
+ result = $"invalid from address {from}";
log.Error(result);
log.Info("no valid from address was provided so not sending message " + messageBody);
return false;
}
catch (FormatException)
{
- result = $"invalid from address {from}";
- log.Error(result);
- log.Info("no valid from address was provided so not sending message " + messageBody);
+ result = $"invalid from address {from}";
+ log.Error(result);
+ log.Info("no valid from address was provided so not sending message " + messageBody);
return false;
}
@@ -623,8 +625,8 @@ public static bool Send(
if (mail.To.Count == 0)
{
- result = $"no valid to address was provided so not sending message {messageBody}";
- log.Error(result);
+ result = $"no valid to address was provided so not sending message {messageBody}";
+ log.Error(result);
return false;
}
@@ -748,7 +750,7 @@ private static string GetGlobalBccAddress()
// I use this for the demo site so I get copied on every message
// so that I will know if anyone is managing to send spam from the demo site
- if ((ConfigurationManager.AppSettings["GlobalBCC"] != null)&&(ConfigurationManager.AppSettings["GlobalBCC"].Length > 0))
+ if ((ConfigurationManager.AppSettings["GlobalBCC"] != null) && (ConfigurationManager.AppSettings["GlobalBCC"].Length > 0))
{
return ConfigurationManager.AppSettings["GlobalBCC"];
}
@@ -756,163 +758,163 @@ private static string GetGlobalBccAddress()
return string.Empty;
}
- public static bool Send(SmtpSettings smtpSettings, MailMessage message)
- {
- return Send(smtpSettings, message, out _);
- }
- public static bool Send(SmtpSettings smtpSettings, MailMessage message, out string result)
- {
- if (message.To.ToString() == "admin@admin.com")
- {
- //demo site
- result = "can't use admin@admin.com email address";
- return false;
- }
-
- string globalBcc = GetGlobalBccAddress();
- if (globalBcc.Length > 0)
- {
- MailAddress bcc = new MailAddress(globalBcc);
- message.Bcc.Add(bcc);
- }
-
- int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
- SmtpClient smtpClient = new SmtpClient(smtpSettings.Server, smtpSettings.Port)
- {
- EnableSsl = true
- };
- smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtpClient.Timeout = timeoutMilliseconds;
-
- if (smtpSettings.RequiresAuthentication)
- {
-
- NetworkCredential smtpCredential
- = new NetworkCredential(
- smtpSettings.User,
- smtpSettings.Password);
-
- CredentialCache myCache = new CredentialCache();
- myCache.Add(smtpSettings.Server, smtpSettings.Port, "LOGIN", smtpCredential);
-
- smtpClient.UseDefaultCredentials = false;
- smtpClient.Credentials = myCache;
- }
- else
- {
- //aded 2010-01-22 JA
- smtpClient.UseDefaultCredentials = true;
- }
-
- foreach (var header in smtpSettings.AdditionalHeaders)
- {
- message.Headers.Add(header.Name, header.Value);
- }
-
- //message.Headers.Add(smtpSettings.AdditionalHeaders);
- if (!string.IsNullOrWhiteSpace(smtpSettings.SenderHeader))
- message.Headers.Add("X-mojo-Sender", smtpSettings.SenderHeader);
-
- try
- {
- smtpClient.Send(message);
- //log.Debug("Sent Message: " + subject);
- //log.Info("Sent Message: " + subject);
-
- bool logEmail = ConfigHelper.GetBoolProperty("LogAllEmailsWithSubject", false);
-
- if (logEmail)
- {
- log.Info("Sent message " + message.Subject + " to " + message.To[0].Address);
- }
- result = "sent";
- return true;
- }
- catch (System.Net.Mail.SmtpException ex)
- {
- //log.Error("error sending email to " + to + " from " + from, ex);
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", will retry", ex);
- return RetrySend(message, smtpClient, ex);
-
- }
- catch (WebException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
- catch (SocketException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
- catch (InvalidOperationException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
- catch (FormatException ex)
- {
- result = $"error: {ex}";
- log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- return false;
- }
-
- }
-
- private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex)
- {
- return RetrySend(message, smtp, ex, out _);
- }
-
-
- private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex, out string result)
- {
- //retry
- int timesToRetry = ConfigHelper.GetIntProperty("TimesToRetryOnSmtpError", 3);
- for (int i = 1; i <= timesToRetry; )
- {
- if (RetrySend(message, smtp, i)) { result = "sent"; return true; }
- i += 1;
- Thread.Sleep(1000); // 1 second sleep in case it is a temporary network issue
- }
-
- // allows use of localhost as backup
- if (ConfigurationManager.AppSettings["BackupSmtpServer"] != null)
- {
- string backupServer = ConfigurationManager.AppSettings["BackupSmtpServer"];
- int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
- int backupSmtpPort = ConfigHelper.GetIntProperty("BackupSmtpPort", 25);
- SmtpClient smtpClient = new SmtpClient(backupServer, backupSmtpPort)
- {
- EnableSsl = true
- };
- smtpClient.UseDefaultCredentials = true;
-
- try
- {
- smtpClient.Send(message);
- log.Info("success using backup smtp server sending email to " + message.To.ToString() + " from " + message.From);
- result = "sent";
- return true;
- }
- catch (System.Net.Mail.SmtpException) { }
- catch (WebException) { }
- catch (SocketException) { }
- catch (InvalidOperationException) { }
- catch (FormatException) { }
-
- }
-
- //log.Info("all retries failed sending email to " + message.To.ToString() + " from " + message.From);
- log.Error("all retries failed sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
- result = "fail";
- return false;
-
- }
+ public static bool Send(SmtpSettings smtpSettings, MailMessage message)
+ {
+ return Send(smtpSettings, message, out _);
+ }
+ public static bool Send(SmtpSettings smtpSettings, MailMessage message, out string result)
+ {
+ if (message.To.ToString() == "admin@admin.com")
+ {
+ //demo site
+ result = "can't use admin@admin.com email address";
+ return false;
+ }
+
+ string globalBcc = GetGlobalBccAddress();
+ if (globalBcc.Length > 0)
+ {
+ MailAddress bcc = new MailAddress(globalBcc);
+ message.Bcc.Add(bcc);
+ }
+
+ int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
+ SmtpClient smtpClient = new SmtpClient(smtpSettings.Server, smtpSettings.Port)
+ {
+ EnableSsl = true
+ };
+ smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
+ smtpClient.Timeout = timeoutMilliseconds;
+
+ if (smtpSettings.RequiresAuthentication)
+ {
+
+ NetworkCredential smtpCredential
+ = new NetworkCredential(
+ smtpSettings.User,
+ smtpSettings.Password);
+
+ CredentialCache myCache = new CredentialCache();
+ myCache.Add(smtpSettings.Server, smtpSettings.Port, "LOGIN", smtpCredential);
+
+ smtpClient.UseDefaultCredentials = false;
+ smtpClient.Credentials = myCache;
+ }
+ else
+ {
+ //aded 2010-01-22 JA
+ smtpClient.UseDefaultCredentials = true;
+ }
+
+ foreach (var header in smtpSettings.AdditionalHeaders)
+ {
+ message.Headers.Add(header.Name, header.Value);
+ }
+
+ //message.Headers.Add(smtpSettings.AdditionalHeaders);
+ if (!string.IsNullOrWhiteSpace(smtpSettings.SenderHeader))
+ message.Headers.Add("X-mojo-Sender", smtpSettings.SenderHeader);
+
+ try
+ {
+ smtpClient.Send(message);
+ //log.Debug("Sent Message: " + subject);
+ //log.Info("Sent Message: " + subject);
+
+ bool logEmail = ConfigHelper.GetBoolProperty("LogAllEmailsWithSubject", false);
+
+ if (logEmail)
+ {
+ log.Info("Sent message " + message.Subject + " to " + message.To[0].Address);
+ }
+ result = "sent";
+ return true;
+ }
+ catch (System.Net.Mail.SmtpException ex)
+ {
+ //log.Error("error sending email to " + to + " from " + from, ex);
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", will retry", ex);
+ return RetrySend(message, smtpClient, ex);
+
+ }
+ catch (WebException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+ catch (SocketException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+ catch (InvalidOperationException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+ catch (FormatException ex)
+ {
+ result = $"error: {ex}";
+ log.Error("error sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ return false;
+ }
+
+ }
+
+ private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex)
+ {
+ return RetrySend(message, smtp, ex, out _);
+ }
+
+
+ private static bool RetrySend(MailMessage message, SmtpClient smtp, Exception ex, out string result)
+ {
+ //retry
+ int timesToRetry = ConfigHelper.GetIntProperty("TimesToRetryOnSmtpError", 3);
+ for (int i = 1; i <= timesToRetry;)
+ {
+ if (RetrySend(message, smtp, i)) { result = "sent"; return true; }
+ i += 1;
+ Thread.Sleep(1000); // 1 second sleep in case it is a temporary network issue
+ }
+
+ // allows use of localhost as backup
+ if (ConfigurationManager.AppSettings["BackupSmtpServer"] != null)
+ {
+ string backupServer = ConfigurationManager.AppSettings["BackupSmtpServer"];
+ int timeoutMilliseconds = ConfigHelper.GetIntProperty("SMTPTimeoutInMilliseconds", 15000);
+ int backupSmtpPort = ConfigHelper.GetIntProperty("BackupSmtpPort", 25);
+ SmtpClient smtpClient = new SmtpClient(backupServer, backupSmtpPort)
+ {
+ EnableSsl = true
+ };
+ smtpClient.UseDefaultCredentials = true;
+
+ try
+ {
+ smtpClient.Send(message);
+ log.Info("success using backup smtp server sending email to " + message.To.ToString() + " from " + message.From);
+ result = "sent";
+ return true;
+ }
+ catch (System.Net.Mail.SmtpException) { }
+ catch (WebException) { }
+ catch (SocketException) { }
+ catch (InvalidOperationException) { }
+ catch (FormatException) { }
+
+ }
+
+ //log.Info("all retries failed sending email to " + message.To.ToString() + " from " + message.From);
+ log.Error("all retries failed sending email to " + message.To.ToString() + " from " + message.From.ToString() + ", message was: " + message.Body, ex);
+ result = "fail";
+ return false;
+
+ }
private static bool RetrySend(MailMessage message, SmtpClient smtp, int tryNumber)
{