亚马逊网络服务-AWS SES SendRawEmailAsync不娱乐BCC



我使用AWS SES Api发送电子邮件,将邮件消息转换为流,但我无法发送BCC。

private async Task<bool> SendMessageUsingAWSProfileAsync(EmailMessage message, CancellationToken token)
    {
        MailMessage mailMessage = GetMailMessage(message);
        if (mailMessage == null)
        {
            _logger.DebugFormat("Unable to create MailMessage from message: {0}.", message);
            return false;
        }
        SendRawEmailResponse response;
        var credentials = new BasicAWSCredentials(_settings.GetString("AWS.Key"), _settings.GetString("AWS.Secret"));
        using (var amazonClient = new AmazonSimpleEmailServiceClient(credentials, _awsRegion))
        {
            using (MemoryStream stream = ConvertMailMessageToMemoryStream(mailMessage))
            {
                // Send Email using AWS API
                var rawMessage = new RawMessage(stream);
                List<String> destinations = new List<string>();

                var sendRequest = new SendRawEmailRequest(rawMessage)
                {
                    Source = mailMessage.From.Address,
                };
                response = await amazonClient.SendRawEmailAsync(sendRequest, token).ConfigureAwait(false);
            }
        }
        message.MessageId = response.MessageId;
        message.FromServiceId = _settings.GetString("AWS.Key");
        message.CommunicationDirectionEnum = CommunicationDirectionEnum.Out;
        await LogMessageAsync(message, token).ConfigureAwait(false);
        await AuditMessageAsync(message, token).ConfigureAwait(false);
        return true;
    }

 private MailMessage GetMailMessage(EmailMessage message)
    {
        if (message == null) throw new ArgumentNullException(nameof(message));
        if (string.IsNullOrEmpty(message?.Body))
        {
            _logger.DebugFormat("Empty email body.");
            return null;
        }
        if (string.IsNullOrEmpty(message.ToEmailID))
        {
            _logger.DebugFormat("To EmailID is empty.");
            return null;
        }
        List<string> mailAddresses = GetToEmailList(message.ToEmailID);
        List<string> bccMailAddresses = GetToEmailList(message.BccEmailId);
        if (mailAddresses == null || mailAddresses.Count == 0)
        {
            _logger.DebugFormat("No valid To EmailID.");
            return null;
        }
        var result = new MailMessage();
        foreach (var toAddress in mailAddresses)
        {
            result.To.Add(new MailAddress(toAddress));
        }
        foreach (var bccAddress in bccMailAddresses)
        {
            result.Bcc.Add(new MailAddress(bccAddress));
        }

        return result;
    }
public static MemoryStream ConvertMailMessageToMemoryStream(MailMessage message)
    {
        Assembly assembly = typeof(SmtpClient).Assembly;
        Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
        var stream = new MemoryStream()
            ConstructorInfo mailWriterConstructor =
                mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
                    new[] { typeof(Stream) }, null);
        object mailWriter = mailWriterConstructor.Invoke(new object[] { stream });
        MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send",
            BindingFlags.Instance | BindingFlags.NonPublic);
        sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null,
            new[] { mailWriter, true, true }, null);
        MethodInfo closeMethod = mailWriter.GetType()
            .GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
        closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { },
            null);
        return stream;
    }

密件抄送已经包含在mailMessage对象中,因此当它将其转换为原始邮件时,也应该将密件抄送包含在邮件头中。我尝试过包括/排除目的地,结果相同。

非常感谢您的帮助。

因此,通过对代码进行以下两项更改,最终使其发挥作用

1-在SendMessageUsingAWSProfileAsync(,删除目标,因为它不接受BCC

var sendRequest = new SendRawEmailRequest(rawMessage)
{
   Source = mailMessage.From.Address, // destination is removed from here intentionally as it stop doing BCC
}

2-在ConvertMailMessageToMemoryStream中,从邮件消息创建流时包括BCC标头

// BCC is not included by default, so need to include it.
if (message.Bcc.Count > 0)
 {
    MethodInfo writeHeadersMethod = mailWriter.GetType().GetMethod("WriteHeaders", BindingFlags.Instance | BindingFlags.NonPublic);
    System.Collections.Specialized.NameValueCollection bccHeaders = new System.Collections.Specialized.NameValueCollection();
    bccHeaders.Add("BCC", string.Join(",", message.Bcc.Select(b => b.Address)));
    writeHeadersMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { bccHeaders, false }, null);
 }

相关内容

  • 没有找到相关文章

最新更新