• MailKit系列之转发电子邮件


    原文:http://www.it1352.com/429181.html

    问题

    我尝试通过MailKit访问一个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);
    }

    通常人们并不意味着这种类型的“转发”,虽然。如果他们想重新发送,通常他们会用重发的一个方法。

    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"));
    
    // 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);
    }
  • 相关阅读:
    让photoshop cc 支持 webp格式
    DedeCMS文章页去img图片width和height属性
    DedeCMS提交自定义表单加入验证码功能
    php session的操作
    使用jQuery,实现完美的表单异步提交
    Windows Server 2003 IIS6.0+PHP5(FastCGI)+MySQL5环境搭建教程
    BT之下拉菜单
    慕课网上的Bootstrap学习(二)
    表单控件状态(禁用状态)
    在慕课学习Bootstrap
  • 原文地址:https://www.cnblogs.com/xcsn/p/10695145.html
Copyright © 2020-2023  润新知