使用MailKit转发电子邮件(C#) [英] Forward email using MailKit (C#)

查看:2883
本文介绍了使用MailKit转发电子邮件(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问使用MailKit IMAP帐户

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

我设法下载邮件(作为的MimeMessage),并在某些时候我需要前进到另一个帐户。

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

如何将做到这一点,为了保持原始邮件的所有信息(不会忽略,标题,正文内容等)的最佳方式。

How would be the best way to do it, in order to preserve all the information of the original email (adresses, headers, body content, etc).

谢谢!

推荐答案

不同的人意味着不同的事情,当他们说前进,所以我想我会提供答案的不同含义的我能想到的。

Different people mean different things when they say "forward", so I guess I'll provide answers to the different meanings that I can think of.

1。转发(重发)无任何变化的消息。

这是没有改变,我硬是指在所有的原始信息数据没有变化。要做到这一点的方法是要做到:

By "no changes", I literally mean no changes at all to the raw message data. The way to do this is to do:

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's headers.
    client.Send (message, sender, recipients);

    client.Disconnect (true);
}



通常人们并不意味着这种类型的转发,虽然。如果他们想重新发送,通常他们会用重发的一个方法。

Usually people don't mean this type of "forwarding", though. If they want to resend, usually they'll use the next method of resending.

2。转发(重发)消息的标准方法

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。通过将它(整体),以一个新的消息,有些邮件客户端可能会做的方式转发邮件。

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's 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);
}

这篇关于使用MailKit转发电子邮件(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆