Forward Email Using MailKit (C #)

I am trying to access an IMAP account using MailKit (created by jstedfast )

I managed to download the message (like MimeMessage), and at some point I need to "forward" it to another account.

What would be the best way to do this in order to save all the information of the original letter (addresses, headers, body content, etc.).

Thanks!

+5
source share
1 answer

Different people mean different things when they say โ€œforward,โ€ so I think I will give answers to different meanings that I can think of.

1. Forward (Resend) the message without any changes.

In the "No Change" section, I literally do not mean any changes to the data of the raw messages. The way to do this is:

var message = FetchMessageFromImapServer (); using (var client = new SmtpClient ()) { client.Connect ("smtp.example.com", 465, true); client.Authenticate ("username", "password"); var sender = new MailboxAddress ("My Name", " username@example.com "); var recipients = new [] { new MailboxAddress ("John Smith", " john@smith.com ") }; // This version of the Send() method uses the supplied sender and // recipients rather than getting them from the message headers. client.Send (message, sender, recipients); client.Disconnect (true); } 

Usually people do not mean this type of โ€œforwardingโ€. If they want to resend, usually they will use the following resend method.

2. Forward (resend) the message in the standard way.

 var message = FetchMessageFromImapServer (); // clear the Resent-* headers in case this message has already been Resent... message.ResentSender = null; message.ResentFrom.Clear (); message.ResentReplyTo.Clear (); message.ResentTo.Clear (); message.ResentCc.Clear (); message.ResentBcc.Clear (); // now add our own Resent-* headers... message.ResentFrom.Add (new MailboxAddress ("MyName", " username@example.com ")); message.ResentReplyTo.Add (new MailboxAddress ("MyName", " username@example.com ")); message.ResentTo.Add (new MailboxAddress ("John Smith", " john@smith.com ")); message.ResentMessageId = MimeUtils.GenerateMessageId (); message.ResentDate = DateTimeOffset.Now; using (var client = new SmtpClient ()) { client.Connect ("smtp.example.com", 465, true); client.Authenticate ("username", "password"); // The Send() method will use the Resent-From/To/Cc/Bcc headers if // they are present. client.Send (message); client.Disconnect (true); } 

3. Forward the message, linking it (in general) with the new message, as some mail clients can do.

 var messageToForward = FetchMessageFromImapServer (); // construct a new message var message = new MimeMessage (); message.From.Add (new MailboxAddress ("MyName", " username@example.com ")); message.ReplyTo.Add (new MailboxAddress ("MyName", " username@example.com ")); message.To.Add (new MailboxAddress ("John Smith", " john@smith.com ")); // now to create our body... var builder = new BodyBuilder (); builder.TextBody = "Hey John,\r\n\r\nHere that message I was telling you about...\r\n"; builder.Attachments.Add (new MessagePart { Message = messageToForward }); message.Body = builder.ToMessageBody (); using (var client = new SmtpClient ()) { client.Connect ("smtp.example.com", 465, true); client.Authenticate ("username", "password"); client.Send (message); client.Disconnect (true); } 
+10
source

All Articles