Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

smtp - Forward email using MailKit (C#)

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

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).

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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. 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. Forward (Resend) the message the way that an automated filter might send it.

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.

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. Forward the message "inline" the way many other email clients do it.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...