ASP.NET 中的流中的附件 Excel 文件



我有一个 excel 文档。我想将其作为流中的附件邮寄。

它发送了带有附件的邮件,但我无法正确打开 excel 文件

这是我的代码:

public static string EPostaGonder(...,Stream AttachmentStream,string AttachmentFileName)
    {
      .
      .
      .
       SmtpClient mailClient = new SmtpClient(Host, Port);
       mailClient.EnableSsl = true;
       NetworkCredential cred = new NetworkCredential(KullaniciAdi, Sifre);
       mailClient.Credentials = cred;
       MailMessage ePosta = new MailMessage();
       ePosta.IsBodyHtml = true;
       ePosta.From = new MailAddress(Kimden, Isim);
       foreach (string Kime_ in Kime.Split(';'))
       {
         if (Kime_.Trim() != "")
            ePosta.To.Add(Kime_.Trim());

       ePosta.Subject = Konu;
       ePosta.Body = Mesaj.Replace("n","<br/>");
       if (Cc != "")
         ePosta.CC.Add(Cc);
       if (AttachmentStream != null)
          {
             AttachmentStream .Seek(0, SeekOrigin.Begin);                 
             ePosta.Attachments.Add(
                      new Attachment(AttachmentStream, AttachmentFileName + ".xlsx"));
          }

        try
        {
           //mailClient.SendAsync(ePosta, (object)ePosta);
           mailClient.Send(ePosta);
             return "Done";
        }
         catch (SmtpException SmtpException_)
         {
             return SmtpException_.Message;
         }
  }

使用以下代码在邮件中添加附件。只需将文件路径传递给附件构造函数即可。

Attachment attachment = new Attachment(file);
ePosta.Attachments.Add(attachment);

从流中添加附件:

ePosta.Attachments.Add( new Attachment( AttachmentStream, filename, "application/msexcel" ));

尝试类似

var attach = new MailAttachment(Server.MapPath(strFileName));
ePosta.Attachments.Add(attach);

看看这篇关于 ASP.NET 带有多个附件的电子邮件的文章

相关内容

  • 没有找到相关文章