AWS SES SendRawEmailAsync not entertaining BCC

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I am sending the email using AWS SES Api, by converting the mail message into the stream, but i am not able to send the 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;     } 

The BCC is already into the mailMessage object, so when its converting it to raw, it should be include BCC into the headers too. I have tried including/ excluding destinations, it gives the same result.

Any help much appreciated.

回答1:

So finally made it work by doing following 2 changes to code

1 - In SendMessageUsingAWSProfileAsync(, Remove the desintation as it does not entertain BCC

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

}

2 - In ConvertMailMessageToMemoryStream, Include BCC headers while creating the stream from mail message

// 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);         } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!