• <tfoot id='dvceP'></tfoot>
    • <bdo id='dvceP'></bdo><ul id='dvceP'></ul>
  • <small id='dvceP'></small><noframes id='dvceP'>

        <legend id='dvceP'><style id='dvceP'><dir id='dvceP'><q id='dvceP'></q></dir></style></legend>

        <i id='dvceP'><tr id='dvceP'><dt id='dvceP'><q id='dvceP'><span id='dvceP'><b id='dvceP'><form id='dvceP'><ins id='dvceP'></ins><ul id='dvceP'></ul><sub id='dvceP'></sub></form><legend id='dvceP'></legend><bdo id='dvceP'><pre id='dvceP'><center id='dvceP'></center></pre></bdo></b><th id='dvceP'></th></span></q></dt></tr></i><div id='dvceP'><tfoot id='dvceP'></tfoot><dl id='dvceP'><fieldset id='dvceP'></fieldset></dl></div>

        将文件从 MemoryStream 附加到 C# 中的 MailMessage

        时间:2023-10-06
          • <small id='VmT6s'></small><noframes id='VmT6s'>

              <i id='VmT6s'><tr id='VmT6s'><dt id='VmT6s'><q id='VmT6s'><span id='VmT6s'><b id='VmT6s'><form id='VmT6s'><ins id='VmT6s'></ins><ul id='VmT6s'></ul><sub id='VmT6s'></sub></form><legend id='VmT6s'></legend><bdo id='VmT6s'><pre id='VmT6s'><center id='VmT6s'></center></pre></bdo></b><th id='VmT6s'></th></span></q></dt></tr></i><div id='VmT6s'><tfoot id='VmT6s'></tfoot><dl id='VmT6s'><fieldset id='VmT6s'></fieldset></dl></div>
              <tfoot id='VmT6s'></tfoot>

                <tbody id='VmT6s'></tbody>
                <bdo id='VmT6s'></bdo><ul id='VmT6s'></ul>
              • <legend id='VmT6s'><style id='VmT6s'><dir id='VmT6s'><q id='VmT6s'></q></dir></style></legend>

                  本文介绍了将文件从 MemoryStream 附加到 C# 中的 MailMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个将文件附加到电子邮件的程序.目前我正在使用 FileStream 将文件保存到磁盘中,然后我使用

                  I am writing a program to attach a file to email. Currently I am saving file using FileStream into disk, and then I use

                  System.Net.Mail.MailMessage.Attachments.Add(
                      new System.Net.Mail.Attachment("file name")); 
                  

                  我不想将文件存储在磁盘中,我想将文件存储在内存中,然后从内存流中将其传递给 Attachment.

                  I do not want to store file in disk, I want to store file in memory and from memory stream pass this to Attachment.

                  推荐答案

                  这里是示例代码.

                  System.IO.MemoryStream ms = new System.IO.MemoryStream();
                  System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
                  writer.Write("Hello its my sample file");
                  writer.Flush();
                  writer.Dispose();
                  ms.Position = 0;
                  
                  System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
                  System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
                  attach.ContentDisposition.FileName = "myFile.txt";
                  
                  // I guess you know how to send email with an attachment
                  // after sending email
                  ms.Close();
                  

                  编辑 1

                  您可以通过 System.Net.Mime.MimeTypeNames 指定其他文件类型,例如 System.Net.Mime.MediaTypeNames.Application.Pdf

                  You can specify other file types by System.Net.Mime.MimeTypeNames like System.Net.Mime.MediaTypeNames.Application.Pdf

                  根据 Mime 类型,您需要在 FileName 中指定正确的扩展名,例如 "myFile.pdf"

                  Based on Mime Type you need to specify correct extension in FileName for instance "myFile.pdf"

                  这篇关于将文件从 MemoryStream 附加到 C# 中的 MailMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应是:5.5.1 需要身份验证? 下一篇:从 C# 发送带有附件的电子邮件,附件在 Thunderbird 中作为第 1.2 部分到达

                  相关文章

                  1. <i id='gkebt'><tr id='gkebt'><dt id='gkebt'><q id='gkebt'><span id='gkebt'><b id='gkebt'><form id='gkebt'><ins id='gkebt'></ins><ul id='gkebt'></ul><sub id='gkebt'></sub></form><legend id='gkebt'></legend><bdo id='gkebt'><pre id='gkebt'><center id='gkebt'></center></pre></bdo></b><th id='gkebt'></th></span></q></dt></tr></i><div id='gkebt'><tfoot id='gkebt'></tfoot><dl id='gkebt'><fieldset id='gkebt'></fieldset></dl></div>
                    <tfoot id='gkebt'></tfoot>
                      <bdo id='gkebt'></bdo><ul id='gkebt'></ul>
                  2. <small id='gkebt'></small><noframes id='gkebt'>

                    1. <legend id='gkebt'><style id='gkebt'><dir id='gkebt'><q id='gkebt'></q></dir></style></legend>