Skip to content

Commit

Permalink
Fix Mail Recovery System
Browse files Browse the repository at this point in the history
Updated Code with Adjusted Timeout and Trials with Different Security Options
  • Loading branch information
dhmello authored Aug 7, 2024
1 parent 447809f commit 94ef770
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions Intersect.Server/Notifications/Notification.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Net;
using Intersect.Logging;
using Intersect.Server.Localization;
using MailKit.Net.Smtp;
Expand All @@ -9,10 +8,8 @@

namespace Intersect.Server.Notifications
{

public partial class Notification
{

public Notification(string to, string subject = "", bool html = false)
{
ToAddress = to;
Expand All @@ -30,22 +27,29 @@ public Notification(string to, string subject = "", bool html = false)

public bool Send()
{
//Check and see if smtp is even setup
// Check if SMTP is set up
if (Options.Smtp.IsValid())
{
//Make sure we have a body
// Ensure we have a body
if (!string.IsNullOrEmpty(Body))
{
try
{
//Send the email
var fromAddress = new MailboxAddress(Options.Smtp.FromName, Options.Smtp.FromAddress);
var toAddress = new MailboxAddress(ToAddress, ToAddress);
var toAddress = new MailboxAddress("Recipient", ToAddress);

using (var client = new SmtpClient())
{
client.Connect(Options.Smtp.Host, Options.Smtp.Port, Options.Smtp.UseSsl ? SecureSocketOptions.StartTls : SecureSocketOptions.Auto);
client.Authenticate(Options.Smtp.Username, Options.Smtp.Password);
client.Timeout = 20000; // 20 seconds timeout

// Attempt to connect to the SMTP server
client.Connect(Options.Smtp.Host, Options.Smtp.Port, SecureSocketOptions.SslOnConnect);

// Authenticate if necessary
if (!string.IsNullOrEmpty(Options.Smtp.Username) && !string.IsNullOrEmpty(Options.Smtp.Password))
{
client.Authenticate(Options.Smtp.Username, Options.Smtp.Password);
}

var message = new MimeMessage();
message.To.Add(toAddress);
Expand All @@ -63,6 +67,7 @@ public bool Send()
}
message.Body = bodyBuilder.ToMessageBody();

// Send the message
client.Send(message);
client.Disconnect(true);
}
Expand Down Expand Up @@ -90,7 +95,7 @@ public bool Send()
Subject +
") to " +
ToAddress +
". Reason: SMTP not configured!"
". Reason: Body is empty!"
);
return false;
}
Expand Down Expand Up @@ -138,7 +143,5 @@ protected bool LoadFromTemplate(string templatename, string username)

return false;
}

}

}

0 comments on commit 94ef770

Please sign in to comment.