【问题标题】:Using SendMessage to email a file attachment without saving the file使用 SendMessage 通过电子邮件发送文件附件而不保存文件
【发布时间】:2014-07-20 16:30:41
【问题描述】:

我可以发送电子邮件和所有内容,但我无法创建有效的 Attachment() 以放入我的电子邮件中。我在网上找到的所有示例都假定它以某种方式保存在我的机器上本地并通过路径链接它,但事实并非如此。在我的方法中,我使用 Winnovative 创建文件,然后我想将其附加到电子邮件中并发送。不涉及储蓄。

protected void SendEmail_BtnClick(object sender, EventArgs a) 
{
    if(IsValidEmail(EmailTextBox.Text)) 
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(EmailTextBox.Text);
            mailMessage.From = new MailAddress("my email");
            mailMessage.Subject = " Your Order";

            string htmlstring = GenerateHTMLString();

            Document pdfDocument = GeneratePDFReport(htmlstring);
            //Attachment attachment = new Attachment("Receipt.pdf",pdfDocument);
            //mailMessage.Attachments.Add();

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
            //needs to change this password
            smtpClient.Credentials = new NetworkCredential("j@gmail.com","password"); //change password to password of email
            smtpClient.EnableSsl = true;
            try 
            {
                smtpClient.Send(mailMessage);
                EmailErrorMessage("Email Sent");
            }
            catch(Exception exc) 
            {
                EmailErrorMessage("Email could not be sent");
            }

        }
        catch (Exception ex)
        {

            EmailErrorMessage("Could not send the e-mail. Error: "+ex.Message);
        }
    }
    else 
    {
        EmailErrorMessage("Please input a valid email.");
    }
}

编辑:这是我完成的解决方案。

                MailMessage mailMessage = CreateMailMessage();
                SmtpClient smtpClient = CreateSMTPClient();
                string htmlstring = GenerateHTMLString();
                Document pdfDocument = GeneratePDFReport(htmlstring);

                // Save the document to a byte buffer
                byte[] pdfBytes;
                using (var ms = new MemoryStream())
                {
                    pdfDocument.Save(ms);
                    pdfBytes = ms.ToArray();
                }
                smtpClient.Send(mailMessage);
                EmailErrorMessage("Email Sent");



                // create the attachment
                Attachment attach;
                using (var ms = new MemoryStream(pdfBytes))
                {
                    attach = new Attachment(ms, "application.pdf");
                    mailMessage.Attachments.Add(attach);
                    try
                    {
                        smtpClient.Send(mailMessage);
                        EmailErrorMessage("Email Sent");
                    }
                    catch (Exception exc)
                    {
                        EmailErrorMessage("Could not send the e-mail properly. Error: " + exc.Message);
                    }
                } 

结果:电子邮件发送,但附件名为 application_pdf 而不是 application.pdf。有没有办法解决这个问题?

【问题讨论】:

  • byte[] pdfBytes =pdfDocumnet.GetBytes()//如何弄清楚如何读取字节数组Attachment att = new Attachment(new MemoryStream(pdfBytes),string name);
  • 您需要将Document 保存到内存流中,然后将MemoryStream 用作Attachment constructor 的参数。
  • 除其他外,您的 MailMessageSmtpClient 对象需要位于 using 块中:using (var mailMessage = new MailMessage()){using (var smtpClient = new SmtpClient("smtp.gmail.com")){...}}

标签: c# asp.net email-attachments winnovative


【解决方案1】:

您可以使用流对象来保存 pdf 字符串。您可以使用此流对象进行附件。请在微软网站探索附件课程。

http://msdn.microsoft.com/en-us/library/System.Net.Mail.Attachment(v=vs.110).aspx

【讨论】:

    【解决方案2】:

    byte[] pdfBytes = pdfDocumnet.GetBytes() //some how figure it out to read the byte array

    您可以从MemoryStream 读取字节并将其发送到邮件附件,如下所示

    Attachment att = new Attachment(new MemoryStream(pdfBytes), name);

    【讨论】:

      【解决方案3】:

      我没有使用过 Winnovate 的 PDF,但快速查看他们的文档表明应该可以使用以下内容:

      // Save the document to a byte buffer
      byte[] pdfBytes;
      using (var ms = new MemoryStream())
      {
          pdfDocument.Save(ms);
          pdfBytes = ms.ToArray();
      }
      
      // create the attachment
      Attachment attach;
      using (var ms = new MemoryStream(pdfBytes))
      {
          attach = new Attachment(ms, "application/pdf");
      }
      
      // and add it to the message
      mailMessage.Attachments.Add(attach);
      

      在 cmets 之后编辑:

      如果可能是流必须保持打开状态才能发送消息。也许使用它来添加附件并发送消息:

      Attachment attach;
      var attachStream = new MemoryStream(pdfBytes);
      try
      {
          attachStream = new Attachment(ms, "application/pdf");
          mailMessage.Attachments.Add(attachStream);
          // do other stuff to message
          // ...
          // and then send it.
          smtpClient.Send(MailMessage)
      }
      finally
      {
          attachStream.Close();
      }
      

      【讨论】:

      • 在内部的 try{} catch(Exception exp) 中,我得到一个内部异常:无法访问已关闭的流。我不确定在这里做什么。我调查了一下,发现了这个 stackoverflow link,但我不太确定我是否理解这个问题。
      • @LordHoneydew:如果您的代码与我在此处发布的代码相同,我看不出您是如何获得该异常的。在哪一行抛出异常?请注意,我在这里创建了两个不同的内存流。您的链接问题中的问题是 StreamWriter 已关闭流。你不应该在这里遇到这个问题。
      • 它被抛出 smtpClient.Send(MailMessage)。是的,我的代码在这里完全一样,只是删除了我的凭据。
      • 嘿抱歉我周末休息了。 attachStream = new Attachment 给了我无法将附件分配给内存流的错误。另外,我不知道为什么,但我收到一个错误,即 MailMessage 不是适用的变量或成员,尽管它以蓝色突出显示
      • 回到原来的设置,我如何才能让流不关闭?因为这似乎是问题所在。
      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 2016-04-29
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      相关资源
      最近更新 更多