public async Task <ICommandResult <MailMessage> > SendAsync(MailMessage message) { var result = new SmtpResult(); // Ensure we've configured required email settings if (_smtpSettings?.DefaultFrom == null) { return(result.Failed("Email settings must be configured before an email can be sent.")); } // Use application email if no from is specified if (message.From == null) { message.From = new MailAddress(_smtpSettings.DefaultFrom); } // Invoke EmailSending subscriptions foreach (var handler in _broker.Pub <MailMessage>(this, "EmailSending")) { message = await handler.Invoke(new Message <MailMessage>(message, this)); } // Attempt to send the email var sendResult = await _smtpService.SendAsync(message); if (sendResult.Succeeded) { // Invoke EmailSent subscriptions foreach (var handler in _broker.Pub <MailMessage>(this, "EmailSent")) { message = await handler.Invoke(new Message <MailMessage>(message, this)); } return(result.Success(message)); } return(result.Failed(sendResult.Errors.ToArray())); }
/// <inheritdoc/> public async Task <SmtpResult> SendAsync(MailMessage message) { if (_gmailSetting?.DefaultSender == null) { return(SmtpResult.Failed(S["Gmail settings must be configured before an email can be sent."])); } try { message.From = string.IsNullOrWhiteSpace(message.From) ? _gmailSetting.DefaultSender : message.From; var mimeMessage = FromMailMessage(message); await SendMessage(mimeMessage); return(SmtpResult.Success); } catch (Exception ex) { return(SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message])); } }
/// <summary> /// [deprecated] Sends the specified email message. Use <see cref="SendMailAsync(EmailMessage)"/> instead. /// </summary> /// <param name="message">The email message.</param> /// <returns>True if the email was sent successfully. False otherwise.</returns> public async Task <bool> SendMail(EmailMessage message) { SmtpResult result = await SendMailAsync(message); return(result == SmtpResult.OK); }