2014-04-22 36 views
6

我有一个ASP.NET MVC应用程序必须发送一封电子邮件给收件人列表,附件详细说明了一个特定的“项目”。我通过使用SQL Server报告服务(SSRS)从报告中获得此详细信息。获取文件作为附件从字节数组发送

我从来没有真正使用过SSRS,但我从他的一个同事那里收到了一些他使用过的代码。他对报告做的是他在浏览器中将其下载到客户机器上。所以,如果我不这样做,我坐在一个包含报告数据的字节数组。

有没有一种方法可以将它作为附件发送,而无需先将文件物理写入服务器的文件系统?该报告将采用excel格式或PDF格式。我想用SmtpClient发送邮件。

+0

您有ASP.NET MVC(版本?)Web服务器上传文件,并且您想将上传的文件发送到(SMTP?)您的电子邮件地址列表? –

+0

@JossefHarush:不,我没有文件上传控件。我要附加的文件来自从SQL报告服务器检索到的SSRS报告。我可以在代码中配置以各种格式检索报告,但我将使用xsl或pdf。 – Carel

+0

谁将文件保存到文件系统?你或你的同事提供的'黑盒'解决方案? –

回答

4

要做到这一点,您需要像下面那样利用SSRS ReportManager API。

  1. 从Web服务的报告与SSRS
  2. 首先读取文件读入到内存,而不是保存到服务器或客户端
  3. 发送MemoryStream的对象直接到电子邮件服务器。

Reporting services: Get the PDF of a generated report How to send an email with attachments using SmtpClient.SendAsync?

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发送方法。 注意:SendAsync调用,因此您不会等待服务器完全发送电子邮件,然后才能使用户退出点头。

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

谢谢。这与我找到的解决方案非常相似。在做这件事时,我注意到我必须设置附件的内容类型,例如,如果它的一个xls: “邮件附件.Add(新附件(流, ,“application/vnd。ms-excel“));' – Carel

+0

啊是的MIME类型,虐待它添加在上面,以便任何人看到并使用上面的代码是坚实的。 –

7

获取一个byte[]

byte[] binaryFile = // get your file data from the SSRS ... 
string filename = "SSRS.pdf"; 

文件数据准备目标地址的列表或阵列:

string[] addresses = // get addresses somehow (db/hardcoded/config/...) 

发送SMTP消息通过SmtpClient

MailMessage mailMessage= new MailMessage(); 

mailMessage.From = new MailAddress("sender email address goes here"); 

// Loop all your clients addresses 
foreach (string address in addresses) 
{ 
    mailMessage.To.Add(address);  
} 

mailMessage.Subject = "your message subject goes here"; 
mailMessage.Body = "your message body goes here"; 

MemoryStream memoryStream = new MemoryStream(binaryFile); 
mailMessage.Attachments.Add(new Attachment(memoryStream, filename , MediaTypeNames.Application.Pdf)); 

SmtpClient smtpClient = new SmtpClient(); 
smtpClient.Send(mailMessage); 
相关问题