【问题标题】:Attach file from internet to mail without saving it in disk in asp.net将文件从 Internet 附加到邮件,而不将其保存在 asp.net 的磁盘中
【发布时间】:2014-04-30 08:24:10
【问题描述】:

我为asp.net 编写了一个邮件发送代码。它工作正常。我可以附加保存在磁盘上的文件。现在我想附加一个在互联网上的文件。我可以下载该文件并将其保存在磁盘上的某个位置并附加该文件。但我不想将文件保存在磁盘上。我只需要下载内存中的文件并将其附加到我的邮件中。需要帮助。

这是我发送电子邮件的代码

    public void SendMail(string To, string Subject, string Body)
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("noreply@xyz.com", "xyz");
        mail.To.Add(new MailAddress(To));
        mail.Subject = Subject;

        mail.Body = Body;
        mail.IsBodyHtml = true;

        using (SmtpClient smtpClient = new SmtpClient())
        {
            try
            {
                smtpClient.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

我想要这样的东西

    public void SendMail(string To, string Subject, string Body, URI onlineFileURI)
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("noreply@xyz.com", "xyz");
        mail.To.Add(new MailAddress(To));
        mail.Subject = Subject;

        mail.Body = Body;
        mail.IsBodyHtml = true;

        //Create the attachment from URL
        var attach = [Attachemnt from URL]

        mail.Attachments.Add(attach)

        using (SmtpClient smtpClient = new SmtpClient())
        {
            try
            {
                smtpClient.Send(mail);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

如何下​​载内存中的文件并附加它?

【问题讨论】:

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


    【解决方案1】:

    这就是我根据之前的帖子最终做到的方式:

    var url = "myurl.com/filename.jpg"
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse())
    using (Stream responseStream = HttpWResp.GetResponseStream())
    using (MemoryStream ms = new MemoryStream())
    {
        responseStream.CopyTo(ms);
        ms.Seek(0, SeekOrigin.Begin);
        Attachment attachment = new Attachment(ms, filename, MediaTypeNames.Image.Jpeg);
        message.Attachments.Add(attachment);
        _smtpClient.Send(message);
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过将页面转换为流/字节数组并发送它来实现

      这里是代码

      string strReportUser = "RSUserName";
      string strReportUserPW = "MySecretPassword";
      string strReportUserDomain = "DomainName";
      
      string sTargetURL = "http://SqlServer/ReportServer?" +
         "/MyReportFolder/Report1&rs:Command=Render&rs:format=PDF&ReportParam=" +
         ParamValue;
      
      HttpWebRequest req =
            (HttpWebRequest)WebRequest.Create( sTargetURL );
      req.PreAuthenticate = true;
      req.Credentials = new System.Net.NetworkCredential(
          strReportUser,
          strReportUserPW,
          strReportUserDomain );
      
      HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();
      
      Stream fStream = HttpWResp.GetResponseStream();
      
      HttpWResp.Close();
      
      //Now turn around and send this as the response..
      ReadFullyAndSend( fStream );
      ReadFullyAnd send method. NB: the SendAsync call so your not waiting for the server to send the email completely before you are brining the user back out of the land of nod.
      
      public static void ReadFullyAndSend( Stream input )
      {
         using ( MemoryStream ms = new MemoryStream() )
         {
            input.CopyTo( ms );
      
              MailMessage message = new MailMessage("from@foo.com", "too@foo.com");
                  Attachment attachment = new Attachment(ms, "my attachment",, "application/vnd.ms-excel");
                  message.Attachments.Add(attachment);
                  message.Body = "This is an async test.";
      
                  SmtpClient smtp = new SmtpClient("localhost");
                  smtp.Credentials = new NetworkCredential("foo", "bar");
                  smtp.SendAsync(message, null);
         }
      } 
      

      礼貌:Get file to send as attachment from byte array

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 2014-12-01
        • 1970-01-01
        • 2015-12-14
        • 2021-05-03
        • 2021-04-13
        相关资源
        最近更新 更多