コード例 #1
0
        /// <summary>
        ///     <see cref="Send" /> the email message to Amazon's Simple Email Service.
        /// </summary>
        /// <returns>true if it succeeds, false if it fails.</returns>
        internal bool Send()
        {
            _email.MessageId = string.Empty;

            try
            {
                var message = new Message
                {
                    Body =
                        _email.HTML
                            ? new Body().WithHtml(new Content(_email.MessageBody)) : new Body().WithText(new Content(_email.MessageBody)),
                    Subject = new Content(_email.MessageSubject)
                };
                var request = new SendEmailRequest(_email.FromAddress, _email.Destination, message);
                using (var client = new Client(_email.Credentials))
                {
                    _email.MessageId = client.SendFormattedEmail(request);
                }

                return !_email.ErrorExists;
            }
            catch (AmazonSimpleEmailServiceException ex)
            {
                return _email.SetErrorMessage(
                    string.Format(
                        "AWS Simple Email Service Exception\n\nError Type: {0}\n" +
                        "Error Code: {1}\nRequest Id: {2}\nStatus Code: {3}\n\n{4}",
                        ex.ErrorType, ex.ErrorCode, ex.RequestId, ex.StatusCode, ex));
            }
            catch (AmazonClientException ex)
            {
                return _email.SetErrorMessage(ex.ToString());
            }
            catch (Exception ex)
            {
                return _email.SetErrorMessage(ex.ToString());
            }
        }
コード例 #2
0
        /// <summary>
        ///     <see cref="Send" /> this message.
        /// </summary>
        /// <returns>
        ///     <see langword="true" /> if it succeeds, <see langword="false" /> if it fails.
        /// </returns>
        internal bool Send()
        {
            _email.MessageId = string.Empty;

            try
            {
                var mailMessage = new MailMessage { From = new MailAddress(_email.FromAddress) };

                foreach (string toAddress in _email.ToAddressList)
                    mailMessage.To.Add(new MailAddress(toAddress));

                foreach (string ccAddress in _email.CcAddressList)
                    mailMessage.CC.Add(new MailAddress(ccAddress));

                foreach (string bccAddress in _email.BccAddressList)
                    mailMessage.Bcc.Add(new MailAddress(bccAddress));

                mailMessage.Subject = _email.MessageSubject;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.AlternateViews.Add(
                    _email.HTML
                        ? AlternateView.CreateAlternateViewFromString(_email.MessageBody, Encoding.UTF8, "text/html")
                        : AlternateView.CreateAlternateViewFromString(_email.MessageBody, Encoding.UTF8, "text/plain"));

                var attachment = new Attachment(_email.AttachmentFilePath)
                {
                    ContentType = new ContentType("application/octet-stream")
                };

                ContentDisposition disposition = attachment.ContentDisposition;
                disposition.DispositionType = "attachment";
                disposition.CreationDate = File.GetCreationTime(_email.AttachmentFilePath);
                disposition.ModificationDate = File.GetLastWriteTime(_email.AttachmentFilePath);
                disposition.ReadDate = File.GetLastAccessTime(_email.AttachmentFilePath);
                mailMessage.Attachments.Add(attachment);

                var rawMessage = new RawMessage();

                using (MemoryStream memoryStream = ConvertMailMessageToMemoryStream(mailMessage))
                    rawMessage.WithData(memoryStream);

                var request = new SendRawEmailRequest
                {
                    RawMessage = rawMessage,
                    Destinations = _email.Destination.ToAddresses,
                    Source = _email.FromAddress
                };

                using (var client = new Client(_email.Credentials))
                {
                    _email.MessageId = client.SendRawEmail(request);
                }

                return !_email.ErrorExists;
            }
            catch (AmazonSimpleEmailServiceException ex)
            {
                return _email.SetErrorMessage(
                    string.Format(
                        "AWS Simple Email Service Exception\n\nError Type: {0}\n" +
                        "Error Code: {1}\nRequest Id: {2}\nStatus Code: {3}\n\n{4}",
                        ex.ErrorType, ex.ErrorCode, ex.RequestId, ex.StatusCode, ex));
            }
            catch (AmazonClientException ex)
            {
                return _email.SetErrorMessage(ex.ToString());
            }
            catch (Exception ex)
            {
                return _email.SetErrorMessage(ex.ToString());
            }
        }