Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Minor changes - there is not need to run another thread
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Jan 11, 2019
1 parent 1ae29fc commit 7f53c57
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions Grand.Services/Messages/EmailSender.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Grand.Core.Domain.Messages;
using Grand.Services.Media;
using Grand.Services.Logging;
using Grand.Services.Media;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;


namespace Grand.Services.Messages
Expand Down Expand Up @@ -46,7 +45,7 @@ public EmailSender(ILogger logger, IDownloadService downloadService, IMimeMappin
/// <param name="attachmentFilePath">Attachment file path</param>
/// <param name="attachmentFileName">Attachment file name. If specified, then this file name will be sent to a recipient. Otherwise, "AttachmentFilePath" name will be used.</param>
/// <param name="attachedDownloadId">Attachment download ID (another attachedment)</param>
public virtual async void SendEmail(EmailAccount emailAccount, string subject, string body,
public virtual void SendEmail(EmailAccount emailAccount, string subject, string body,
string fromAddress, string fromName, string toAddress, string toName,
string replyToAddress = null, string replyToName = null,
IEnumerable<string> bccAddresses = null, IEnumerable<string> ccAddresses = null,
Expand Down Expand Up @@ -98,7 +97,7 @@ public virtual async void SendEmail(EmailAccount emailAccount, string subject, s
{
Content = new MimeContent(File.OpenRead(attachmentFilePath), ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment)
{
{
CreationDate = File.GetCreationTime(attachmentFilePath),
ModificationDate = File.GetLastWriteTime(attachmentFilePath),
ReadDate = File.GetLastAccessTime(attachmentFilePath)
Expand Down Expand Up @@ -140,29 +139,15 @@ public virtual async void SendEmail(EmailAccount emailAccount, string subject, s
message.Body = builder.ToMessageBody();

//send email
try
using (var smtpClient = new SmtpClient())
{
using (var smtpClient = new SmtpClient())
{
smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => emailAccount.UseServerCertificateValidation;
await smtpClient.ConnectAsync(
emailAccount.Host,
emailAccount.Port,
(SecureSocketOptions)emailAccount.SecureSocketOptionsId
).ConfigureAwait(false);

await smtpClient.AuthenticateAsync(
emailAccount.Username,
emailAccount.Password
).ConfigureAwait(false);

await smtpClient.SendAsync(message).ConfigureAwait(false);
await smtpClient.DisconnectAsync(true).ConfigureAwait(false);
}
} catch (Exception exc)
{
_logger.Error(string.Format("Error sending e-mail. {0}", exc.Message), exc);
smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => emailAccount.UseServerCertificateValidation;
smtpClient.Connect(emailAccount.Host, emailAccount.Port, (SecureSocketOptions)emailAccount.SecureSocketOptionsId);
smtpClient.Authenticate(emailAccount.Username, emailAccount.Password);
smtpClient.Send(message);
smtpClient.Disconnect(true);
}

}
}
}

0 comments on commit 7f53c57

Please sign in to comment.