Replies: 1 comment 2 replies
|
The easiest solution is to do this: MailboxAddress sender = null;
List<MailboxAddress>recipients = new List<MailboxAddress> ();
foreach (var header in message.Headers) {
if (header.Field.Equals ("X-Sender", StringComparison.OrdinalIgnoreCase)) {
if (sender != null)
throw new Exception ("Too many X-Sender headers!");
sender = MailboxAddress.Parse (header.RawValue);
} else if (header.Field.Equals ("X-Receiver", StringComparison.OrdinalIgnoreCase)) {
recipients.Add (MailboxAddress.Parse (header.RawValue));
}
}
if (sender == null)
throw new Exception ("No X-Sender header!");
if (recipients.Count == 0)
throw new Exception ("No X-Receiver headers!");
var options = FormatOptions.Default.Clone ();
options.HiddenHeaders.Add ("X-Sender");
options.HiddenHeaders.Add ("X-Receiver");
smtpClient.Send (options, message, sender, recipients);I recommend using Header.RawValue instead of Value if you intend to parse it using any of the address parsing routines because, internally, they will just convert the string back into a byte[] before parsing anyway. This saves unnecessary decoding/re-encoding and will result in better performance overall. I also recommend, in this case, to use the SmtpClient.Send (or SendAsync) method that takes a sender and list of recipients rather than modifying the message to rewrite the address headers and risk losing information. Instead of removing the headers from the message, you can also use the FormatOptions.HiddenHeaders property to tell the SmtpClient to drop those headers when it writes the message out to the SMTP server (effectively removing them). |
Uh oh!
There was an error while loading. Please reload this page.
With the imminent demise of the IIS SMTP Server component, I am writing a simple console application to pick up emails stored in a "pickup" folder, and send them on to our SMTP server. That way, I don't need to reconfigure our applications, or have them wait on a network request to send the email.
Using
MimeMessage.Loadto load the generatedemlfile gets us 99% of the way there. But there is a problem: neither theSendernor theBccaddresses are populated.Looking at a raw message file, it seems that these properties are written as custom headers:
Passing that file to
MimeMessage.Loadreturns a message with theSenderset tonull, and an emptyBcccollection.So far, the workaround I've come up with involves a fair amount of code:
Is there some magic built-in way to load these values into a
MimeMessagewithout manually parsing the headers?All reactions