예제 #1
0
        public static async Task <(bool success, string errorMsg)> SendEmailAsync(string senderName, string senderEmail,
                                                                                  string recepientName, string recepientEmail,
                                                                                  string subject, string body, SmtpConfig config = null, bool isHtml = true)
        {
            var from = new MailboxAddress(senderName, senderEmail);
            var to   = new MailboxAddress(recepientName, recepientEmail);

            return(await EmailSender.SendEmailAsync(from, new MailboxAddress[] { to }, subject, body, config, isHtml));
        }
예제 #2
0
        public static async Task <(bool success, string errorMsg)> SendEmailAsync(MailboxAddress sender, MailboxAddress[] recepients, string subject, string body, SmtpConfig config = null, bool isHtml = true)
        {
            MimeMessage message = new MimeMessage();

            message.From.Add(sender);
            message.To.AddRange(recepients);
            message.Subject = subject;
            message.Body    = isHtml ? new BodyBuilder {
                HtmlBody = body
            }.ToMessageBody() : new TextPart("plain")
            {
                Text = body
            };

            try
            {
                if (config == null)
                {
                    config = Configuration;
                }

                using (var client = new SmtpClient())
                {
                    if (!config.UseSSL)
                    {
                        client.ServerCertificateValidationCallback = (object sender2, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true;
                    }

                    await client.ConnectAsync(config.Host, config.Port, config.UseSSL).ConfigureAwait(false);

                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    if (!string.IsNullOrWhiteSpace(config.Username))
                    {
                        await client.AuthenticateAsync(config.Username, config.Password).ConfigureAwait(false);
                    }

                    await client.SendAsync(message).ConfigureAwait(false);

                    await client.DisconnectAsync(true).ConfigureAwait(false);
                }

                return(true, null);
            }
            catch (Exception ex)
            {
                Utilities.CreateLogger <EmailSender>().LogError(LoggingEvents.SEND_EMAIL, ex, "An error occurred whilst sending email");
                return(false, ex.Message);
            }
        }
예제 #3
0
        public static async Task <(bool success, string errorMsg)> SendEmailAsync(MailboxAddress[] recepients,
                                                                                  string subject, string body, SmtpConfig config = null, bool isHtml = true)
        {
            var from = new MailboxAddress(Configuration.Name, Configuration.EmailAddress);

            return(await EmailSender.SendEmailAsync(from, recepients, subject, body, config, isHtml));
        }