コード例 #1
0
        public async Task <bool> SendAsync(SenderParams args)
        {
            var emailMessage = new MimeMessage();

            // these should prevent auto-replies being sent back to the sender address
            // (e.g. out of office replies)
            emailMessage.Headers.Add(AutoSubmittedHeader);
            emailMessage.Headers.Add(PrecedenceHeader);

            // the address that the email is actually from (usually configured against
            // the application)
            emailMessage.From.Add(_sender);

            // if the client has supplied a sender address, use this as the reply-to
            //if (!string.IsNullOrEmpty(args.SenderAddress))
            //{
            //    emailMessage.ReplyTo.Add(new MailboxAddress(args.SenderName, args.SenderAddress));
            //}

            CopyAddresses(args.To, emailMessage.To);
            CopyAddresses(args.CC, emailMessage.Cc);
            CopyAddresses(args.Bcc, emailMessage.Bcc);

            // MimeKit doesn't like `null` subjects, but is fine with an empty string
            // SMTP RFC can accept emails with no subject, this is good otherwise we'd
            // have to have a resource file with appropriate translations for "no
            // subject"!
            emailMessage.Subject = args.Subject ?? string.Empty;

            var builder = new BodyBuilder();

            builder.HtmlBody  = args.Body;
            emailMessage.Body = builder.ToMessageBody();

            try
            {
                using (var client = new SmtpClient())
                {
                    client.LocalDomain = _localDomain;
                    await client.ConnectAsync(_options.Host, _options.Port, _socketOptions).ConfigureAwait(false);

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

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

                    await client.DisconnectAsync(true).ConfigureAwait(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
コード例 #2
0
        public async Task <bool> SendAsync(SenderParams args)
        {
            const string Bearer          = nameof(Bearer);
            const string TextHtml        = "text/html";
            const string ApplicationJson = "application/json";

            // ensure that everything is correctly configured
            CheckConfig();

            var json = JsonConvert.SerializeObject(new
            {
                personalizations = new object[]
                {
                    new
                    {
                        to      = args.To?.Select(e => new { email = e }),
                        cc      = args.CC?.Select(e => new { email = e }),
                        bcc     = args.Bcc?.Select(e => new { email = e }),
                        subject = args.Subject
                    }
                },
                from = new
                {
                    email = args.SenderAddress ?? _options.SenderAddress,
                    name  = args.SenderName ?? _options.SenderName
                },
                content = new object[]
                {
                    new
                    {
                        type  = TextHtml,
                        value = args.Body
                    }
                }
            }, Formatting.Indented);

            // build the HTTP request
            var request = new HttpRequestMessage(HttpMethod.Post, SendApi);

            request.Headers.Authorization = new AuthenticationHeaderValue(Bearer, _options.ApiKey);
            request.Content = new StringContent(json, Encoding.UTF8, ApplicationJson);

            // get the response
            var response = await _httpClient.SendAsync(request);

            // check the response
            if (!response.IsSuccessStatusCode)
            {
                var error = await response.Content.ReadAsStringAsync();
            }

            return(response.IsSuccessStatusCode);
        }