gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:58:28 26 4
gpt4 key购买 nike

我正在尝试使用 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 header 替换为您帐户的名称/电子邮件地址,因此请注意这一潜在问题。

不过,通常人们并不是指这种类型的“转发”。如果他们想重新发送,通常他们会使用下一个重新发送的方法。

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

关于c# - 使用 MailKit (C#) 转发电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29414995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com