【问题标题】:Can I send files via email using MailKit?我可以使用 MailKit 通过电子邮件发送文件吗?
【发布时间】:2016-10-17 15:48:33
【问题描述】:

如题,MailKit是否支持发送文件?
如果是,我该怎么做?

【问题讨论】:

    标签: c# email-attachments mailkit mimekit


    【解决方案1】:

    是的。这在文档以及FAQ 中进行了解释。

    来自常见问题解答:

    如何创建带有附件的消息?

    要构造带有附件的消息,您需要做的第一件事是创建一个multipart/mixed 容器,然后您需要首先将消息正文添加到该容器中。添加正文后,您可以向其中添加包含您要附加​​的文件内容的 MIME 部分,确保将 Content-Disposition 标头值设置为附件。您可能还想在Content-Disposition 标头上设置filename 参数以及在Content-Type 标头上设置name 参数。最方便的方法是简单地使用 MimePart.FileName 属性 将为您设置这两个参数,并将 Content-Disposition 标头值设置为 attachment(如果尚未设置为其他值)。

    var message = new MimeMessage ();
    message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
    message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
    message.Subject = "How you doin?";
    
    // create our message text, just like before (except don't set it as the message.Body)
    var body = new TextPart ("plain") {
        Text = @"Hey Alice,
    
    What are you up to this weekend? Monica is throwing one of her parties on
    Saturday. I was hoping you could make it.
    
    Will you be my +1?
    
    -- Joey
    "
    };
    
    // create an image attachment for the file located at path
    var attachment = new MimePart ("image", "gif") {
        Content = new MimeContent (File.OpenRead (path)),
        ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
        ContentTransferEncoding = ContentEncoding.Base64,
        FileName = Path.GetFileName (path)
    };
    
    // now create the multipart/mixed container to hold the message text and the
    // image attachment
    var multipart = new Multipart ("mixed");
    multipart.Add (body);
    multipart.Add (attachment);
    
    // now set the multipart/mixed as the message body
    message.Body = multipart;
    

    构造带有附件的消息的一种更简单的方法是利用 BodyBuilder类。

    var message = new MimeMessage ();
    message.From.Add (new MailboxAddress ("Joey", "joey@friends.com"));
    message.To.Add (new MailboxAddress ("Alice", "alice@wonderland.com"));
    message.Subject = "How you doin?";
    
    var builder = new BodyBuilder ();
    
    // Set the plain-text version of the message text
    builder.TextBody = @"Hey Alice,
    
    What are you up to this weekend? Monica is throwing one of her parties on
    Saturday. I was hoping you could make it.
    
    Will you be my +1?
    
    -- Joey
    ";
    
    // We may also want to attach a calendar event for Monica's party...
    builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics");
    
    // Now we just need to set the message body and we're done
    message.Body = builder.ToMessageBody ();
    

    有关详细信息,请参阅Creating Messages

    【讨论】:

    • 谢谢,File 运行良好。但是第二个示例的图像不如预期
    • 我不明白这是什么意思。
    • 我已经发送了文件,但是没有发送类型为图片的文件
    • 如果您像示例中一样添加它,我保证它正在发送。
    • 是的 - 只需将内容配置设置为内联
    【解决方案2】:

    @jstedfast 带来了非常酷的解决方案,这里有几个简单的方法示例,可以将文件作为附件发送(在这种情况下是 pdf 文档,但可以应用于任何文件类型)。

    var message = new MimeMessage();
    // add from, to, subject and other needed properties to your message
    
    var builder = new BodyBuilder();
    builder.HtmlBody = htmlContent;
    builder.TextBody = textContent;
    
    // you can either create MimeEntity object(s)
    // this might get handy in case you want to pass multiple attachments from somewhere else
    byte[] myFileAsByteArray = LoadMyFileAsByteArray();
    var attachments = new List<MimeEntity>
    {
        // from file
        MimeEntity.Load("myFile.pdf"),
        // file from stream
        MimeEntity.Load(new MemoryStream(myFileAsByteArray)),
        // from stream with a content type defined
        MimeEntity.Load(new ContentType("application", "pdf"), new MemoryStream(myFileAsByteArray))
    }
    
    // or add file directly - there are a few more overloads to this
    builder.Attachments.Add("myFile.pdf");
    builder.Attachments.Add("myFile.pdf", myFileAsByteArray);
    builder.Attachments.Add("myFile.pdf", myFileAsByteArray , new ContentType("application", "pdf"));
    
    // append previously created attachments
    foreach (var attachment in attachments)
    {
        builder.Attachments.Add(attachment);
    }
    
    message.Body = builder.ToMessageBody();
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      您也可以直接使用这种方法发送多个文件。 **注意:这里使用的文件是 IEnumerable 文件**

              try
              {
                  var message = new MimeMessage();
                  message.From.Add(new MailboxAddress(emailService.FromFullName, emailService.FromEmail));
                  message.To.AddRange(emailsToSend.Select(x => new MailboxAddress(x)));
                  message.Subject = subject;
      
                  var builder = new BodyBuilder();
                  builder.HtmlBody = body;
      
                  foreach (var attachment in files)
                  {
                      if (attachment.Length > 0)
                      {
                          string fileName = Path.GetFileName(attachment.FileName);
                          builder.Attachments.Add(fileName, attachment.OpenReadStream());
                      }
                  }
                  message.Body = builder.ToMessageBody();
                 
                  }
           
      

      【讨论】:

        猜你喜欢
        • 2023-03-18
        • 2020-11-05
        • 2011-05-26
        • 2020-05-01
        • 2023-03-18
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 2020-07-16
        相关资源
        最近更新 更多