问题描述
我正在尝试使用 MailKit 访问 IMAP 帐户(由 jstedfast 创建)
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.
p>
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);
}
注意:GMail、Hotmail/Office365 等一些网络邮件提供商可能不允许您使用这种方法,因为他们可能认为这是电子邮件欺骗.这些邮件主机可能会将 From
标头替换为您帐户的名称/电子邮件地址,因此请注意此潜在问题.
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. These mail hosts might replace the From
header with the name/email address of your account, so be aware of this potential issue.
不过,通常人们并不是指这种类型的转发".如果他们想重新发送,通常他们会使用下一种重新发送的方法.
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"));
message.Subject = "FWD: " + messageToForward.Subject;
// now to create our body...
var builder = new BodyBuilder ();
builder.TextBody = "Hey John,
Here's that message I was telling you about...
";
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.转发消息inline";许多其他电子邮件客户端的做法.
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);
}
这篇关于使用 MailKit (C#) 转发电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!