2014-04-30 47 views
1

我已經寫了一封用於asp.net的郵件發送代碼。它工作正常。我可以附加保存在磁盤上的文件。現在我想附加一個在互聯網上的文件。我可以下載該文件並將其保存在磁盤的某個位置並附加該文件。但我不想將文件保存在磁盤上。我只需要在內存中下載文件並將其附加到我的郵件。需要幫助來做到這一點。將文件從Internet上附加到郵件中,但不保存在asp.net的磁盤中

這裏是我發送電子郵件

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

     mail.From = new MailAddress("[email protected]", "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("[email protected]", "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; 
      } 
     } 
    } 

我怎樣才能下載的文件在內存中,並附加了嗎?

回答

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("[email protected]", "[email protected]"); 
      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

0

這是我最後只是基於以前的帖子:

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); 
} 
相關問題