使用此SO帖子(Convert MailMessage to Raw text),我們能夠將MailMessage作爲流獲取。但是,Bcc被刪除。 To和CC仍然工作,但不是密件抄送。將MailMessage轉換爲Stream刪除密件抄送
爲什麼會被刪除,以及如何保存?
使用此SO帖子(Convert MailMessage to Raw text),我們能夠將MailMessage作爲流獲取。但是,Bcc被刪除。 To和CC仍然工作,但不是密件抄送。將MailMessage轉換爲Stream刪除密件抄送
爲什麼會被刪除,以及如何保存?
很確定BCC字段永遠不會與消息一起發送。
BCC字段用於向收件人發送「盲碳副本」。從WikiPedia:
在對應的情況下,盲複寫本(縮寫密件:)允許消息隱藏在密件輸入的人的發送者:從所述其它接收者字段。
如果密件抄送字段中的收件人實際上包含在電子郵件的原始SMTP文本中,則該保證無法執行。
您在問題中鏈接到的方法很可能被SmtpClient
用於準備傳輸的消息。因此,爲了保持預期的行爲,它將不得不忽略BCC字段。
要真正保持它,你將不得不手動將其插入到輸出文本:
var email = new MailMessage();
using (var reader = new StreamReader(email.RawMessage()))
using (var writer = new StringWriter()) {
while(true) {
var line = reader.ReadLine();
if (line == null) break; // EOF
if (line != "") {
// Header line
writer.WriteLine(line);
continue;
}
// End of headers, insert bcc, read body, then bail
writer.WriteLine("Bcc: " + email.Bcc.ToString()); // or however you want to format it
writer.WriteLine("");
writer.Write(reader.ReadToEnd());
break;
}
var messageText = writer.ToString();
// Do something with message text which now has Bcc: header
}
現在你提到它,這確實有道理。 SmtpClient.Send()方法將去除BCC。我們通過SendRawEmail()方法將此發送給Amazon SES。相反,我想我會手動創建MIME消息字符串,創建流並完成。只是爲了澄清,如果您將BCC發送到SES,它會正確執行。 – scojomodena
可能是因爲它在實際的SMTP信封中使用BCC(它不是郵件標題/正文的一部分),在RCPT TO:<[email protected]>交換期間。 – rossipedia
這裏是RawMessage我的VB.NET翻版(),其中包括BCC的頭。根據別人對原SO鏈接的重新編排:http://colinmackay.co.uk/2011/11/10/sending-more-than-a-basic-email-with-amazon-ses
Public Class BuildRawMailHelper
Private Const nonPublicInstance As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
Private Shared ReadOnly _mailWriterContructor As ConstructorInfo
Private Shared ReadOnly _sendMethod As MethodInfo
Private Shared ReadOnly _closeMethod As MethodInfo
Private Shared ReadOnly _writeHeadersMethod As MethodInfo
Shared Sub New()
Dim systemAssembly As Assembly = GetType(SmtpClient).Assembly
Dim mailWriterType As Type = systemAssembly.[GetType]("System.Net.Mail.MailWriter")
_mailWriterContructor = mailWriterType.GetConstructor(nonPublicInstance, Nothing, {GetType(Stream)}, Nothing)
_sendMethod = GetType(MailMessage).GetMethod("Send", nonPublicInstance)
_closeMethod = mailWriterType.GetMethod("Close", nonPublicInstance)
_writeHeadersMethod = mailWriterType.GetMethod("WriteHeaders", nonPublicInstance)
End Sub
Public Shared Function ConvertMailMessageToMemoryStream(message As MailMessage) As MemoryStream
Using memoryStream As New MemoryStream()
Dim mailWriter As Object = _mailWriterContructor.Invoke(New Object() {memoryStream})
' BCC is not output in headers by Send method, so we add it in manually
If message.Bcc.Any Then
Dim bccHeaders As New NameValueCollection()
bccHeaders.Add("BCC", String.Join(", ", message.Bcc.Select(Function(x) x.ToString())))
_writeHeadersMethod.Invoke(mailWriter, nonPublicInstance, Nothing, {bccHeaders, False}, Nothing)
End If
' See http://stackoverflow.com/questions/9595440/getting-system-net-mail-mailmessage-as-a-memorystream-in-net-4-5-beta
_sendMethod.Invoke(message, nonPublicInstance, Nothing, {mailWriter, True, True}, Nothing)
_closeMethod.Invoke(mailWriter, nonPublicInstance, Nothing, {}, Nothing)
Return memoryStream
End Using
End Function
End Class
我在C#中做了上面的代碼。請參閱我的帖子http://stackoverflow.com/questions/38794401/aws-ses-sendrawemailasync-not-entertaining-bcc –