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

本文关键字:保存 存在 net 磁盘 asp 互联网 文件 | 更新日期: 2023-09-27 18:00:58

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

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

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

这是我根据以前的帖子完成的:

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

您可以将页面转换为流/字节数组并发送

这是代码

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

礼貌:从字节数组中获取要作为附件发送的文件