简体   繁体   English

使用 MailKit (C#) 转发电子邮件

[英]Forward email using MailKit (C#)

I'm trying to access to an IMAP account using MailKit (created by jstedfast )我正在尝试使用 MailKit(由jstedfast创建)访问 IMAP 帐户

I manage to download the message (as a MimeMessage), and at some point I need to "forward" it to another account.我设法下载了消息(作为 MimeMessage),并且在某些时候我需要将它“转发”到另一个帐户。

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).如何最好地做到这一点,以保留原始电子邮件的所有信息(地址、标题、正文内容等)。

Thanks!谢谢!

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. Forward (Resend) the message without any changes. 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);
}

Note: Some webmail providers such as GMail, Hotmail/Office365, etc. might not allow you to use this approach as they might consider it to be email spoofing.注意:某些网络邮件提供商(例如 GMail、Hotmail/Office365 等)可能不允许您使用这种方法,因为他们可能认为这是电子邮件欺骗。 These mail hosts might replace the From header with the name/email address of your account, so be aware of this potential issue.这些邮件主机可能会用您帐户的名称/电子邮件地址替换From标头,因此请注意这个潜在问题。

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. Forward (Resend) the message the way that an automated filter might send it. 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. Forward the message by attaching it (in whole) to a new message, the way some email clients might do it. 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"));
message.Subject = "FWD: " + messageToForward.Subject;

// 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);
}

4. Forward the message "inline" the way many other email clients do it. 4. 以许多其他电子邮件客户端的方式“内联”转发邮件。

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"));
message.Subject = "FWD: " + messageToForward.Subject;

// now to create our body...
var builder = new BodyBuilder ();

using (var writer = new StringWriter ()) {
    var sender = messageToForward.From.Mailboxes.FirstOrDefault () ?? messageToForward.Sender;
    var senderName = sender != null ? (!string.IsNullOrEmpty (sender.Name) ? sender.Name : sender.Address) : "someone";
    var text = messageToForward.TextBody ?? string.Empty;

    writer.WriteLine ("On {0}, {1} wrote:", messageToForward.Date, senderName);

    using (var reader = new StringReader (text)) {
        string line;

        while ((line = reader.ReadLine ()) != null) {
            writer.Write ("> ");
            writer.WriteLine (line);
        }
    }

    builder.TextBody = writer.ToString ();
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM