我正在尝试使用 MailKit(由jstedfast创建)访问 IMAP 帐户
我设法下载了该消息(作为 MimeMessage),并且在某些时候我需要将其“转发”到另一个帐户。
为了保留原始电子邮件的所有信息(地址、标题、正文内容等),最好的方法是如何做到这一点。
谢谢!
不同的人说“前进”的意思不同,所以我想我会针对我能想到的不同含义提供答案。
1. 转发(重新发送)消息,不做任何更改。
“没有变化”,我的字面意思是原始消息数据完全没有变化。这样做的方法是:
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
您帐户的名称/电子邮件地址替换标题,因此请注意此潜在问题。
但是,通常人们并不是指这种类型的“转发”。如果他们想重新发送,通常他们会使用下一种重新发送的方法。
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,\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. 以许多其他电子邮件客户端的方式“内联”转发消息。
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);
}