|
@@ -1,4 +1,5 @@
|
|
-using MailKit.Net.Smtp;
|
|
|
|
|
|
+using MailKit;
|
|
|
|
+using MailKit.Net.Smtp;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Extensions.Options;
|
|
using MimeKit;
|
|
using MimeKit;
|
|
using MTWorkHR.Core.Email;
|
|
using MTWorkHR.Core.Email;
|
|
@@ -52,24 +53,42 @@ namespace MTWorkHR.Infrastructure.EmailService
|
|
//MailText = MailText.Replace("[NotificationType]", email.Subject).Replace("[Info]", email.Body).Replace("[Link]", email.url );
|
|
//MailText = MailText.Replace("[NotificationType]", email.Subject).Replace("[Info]", email.Body).Replace("[Link]", email.url );
|
|
|
|
|
|
var emailObj = new MimeMessage();
|
|
var emailObj = new MimeMessage();
|
|
|
|
+ emailObj.From.Add(new MailboxAddress("Zinab Elgendy", _configuration.MailSettings.FromAddress)); // Ensure From matches authenticated email
|
|
emailObj.Sender = MailboxAddress.Parse(_configuration.MailSettings.FromAddress);
|
|
emailObj.Sender = MailboxAddress.Parse(_configuration.MailSettings.FromAddress);
|
|
emailObj.To.Add(MailboxAddress.Parse(mailTo));
|
|
emailObj.To.Add(MailboxAddress.Parse(mailTo));
|
|
emailObj.Subject = email.Subject;
|
|
emailObj.Subject = email.Subject;
|
|
|
|
|
|
var builder = new BodyBuilder();
|
|
var builder = new BodyBuilder();
|
|
email.Body = email.Body +
|
|
email.Body = email.Body +
|
|
- ( string.IsNullOrEmpty( email.url ) ?"": "Please click on <a href =\"" + email.url + "\">this link</a> to confirm your email address is correct. ");
|
|
|
|
|
|
+ ( string.IsNullOrEmpty( email.url ) ?"": "Please click on <a href =\"" + email.url + "\">this link</a> or copy the following url and open it : \' " + email.url+"\'");
|
|
|
|
|
|
builder.HtmlBody = email.Body;
|
|
builder.HtmlBody = email.Body;
|
|
emailObj.Body = builder.ToMessageBody();
|
|
emailObj.Body = builder.ToMessageBody();
|
|
|
|
+ using var logger = new ProtocolLogger("smtp.log", append: false);
|
|
|
|
+ using var smtp = new SmtpClient(logger);
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- using var smtp = new SmtpClient();
|
|
|
|
|
|
+
|
|
|
|
+
|
|
smtp.Connect(_configuration.MailSettings.Host, _configuration.MailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
|
|
smtp.Connect(_configuration.MailSettings.Host, _configuration.MailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
|
|
smtp.Authenticate(_configuration.MailSettings.FromAddress, _configuration.MailSettings.Password);
|
|
smtp.Authenticate(_configuration.MailSettings.FromAddress, _configuration.MailSettings.Password);
|
|
await smtp.SendAsync(emailObj);
|
|
await smtp.SendAsync(emailObj);
|
|
- smtp.Disconnect(true);
|
|
|
|
- }catch (Exception ex) { var ms = ex.Message; }
|
|
|
|
|
|
+ //smtp.Disconnect(true);
|
|
|
|
+ }
|
|
|
|
+ catch (MailKit.Security.AuthenticationException ex)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine($"Authentication failed: {ex.Message}");
|
|
|
|
+ throw;
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine($"Error sending email: {ex.Message}");
|
|
|
|
+ throw;
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ {
|
|
|
|
+ await smtp.DisconnectAsync(true);
|
|
|
|
+ }
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|