将文件从互联网附加到邮件而不将其保存在asp.net中的磁盘中

时间:2014-04-30 08:24:11

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

我已经为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;
            }
        }
    }

如何在内存中下载文件并附加?

2 个答案:

答案 0 :(得分:3)

这就是我最近根据以前的帖子做的事情:

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);
}

答案 1 :(得分:0)

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

这是代码

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