2016-08-05 33 views
0

我使用AWS SES Api通過將郵件消息轉換爲流發送電子郵件,但我無法發送密件抄送。AWS SES SendRawEmailAsync不具有娛樂性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; 
    } 

BCC已經進入mailMessage對象,所以當它將它轉換爲raw時,它應該在頭中也包含BCC。我試過包括/排除目的地,它給出了相同的結果。

任何幫助非常感謝。

回答

0

所以終於成功了做以下2次修改代碼

1的工作 - 在SendMessageUsingAWSProfileAsync(,取出desintation,因爲它不招待BCC

   var sendRequest = new SendRawEmailRequest(rawMessage) 
       { 
        Source = mailMessage.From.Address, // Destionation is removed from here intenationally 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); 
     }