簡體   English   中英

從自定義SQL Server Reporting Services傳遞擴展發送電子郵件

[英]Sending e-mail from a Custom SQL Server Reporting Services Delivery Extension

我已經為Reporting Services 2005開發了自己的交付擴展,以將其與我們的SaaS營銷解決方案集成。

它獲取訂閱,並使用一組自定義參數獲取報告的快照。 然后,它呈現報告,發送帶有鏈接的電子郵件,並將報告附加為XLS。

一切正常,直到郵件傳遞...

這是我發送電子郵件的代碼:

 public static List<string> SendMail(SubscriptionData data, Stream reportStream, string reportName, string smptServerHostname, int smtpServerPort)
{
  List<string> failedRecipients = new List<string>();

  MailMessage emailMessage = new MailMessage(data.ReplyTo, data.To);
  emailMessage.Priority = data.Priority;
  emailMessage.Subject = data.Subject;
  emailMessage.IsBodyHtml = false;
  emailMessage.Body = data.Comment;

  if (reportStream != null)
  {
    Attachment reportAttachment = new Attachment(reportStream, reportName);
    emailMessage.Attachments.Add(reportAttachment);
    reportStream.Dispose();
  }

  try
  {
    SmtpClient smtp = new SmtpClient(smptServerHostname, smtpServerPort);

    // Send the MailMessage
    smtp.Send(emailMessage);
  }
  catch (SmtpFailedRecipientsException ex)
  {
    // Delivery failed for the recipient. Add the e-mail address to the failedRecipients List
    failedRecipients.Add(ex.FailedRecipient);
  }
  catch (SmtpFailedRecipientException ex)
  {
    // Delivery failed for the recipient. Add the e-mail address to the failedRecipients List
    failedRecipients.Add(ex.FailedRecipient);
  }
  catch (SmtpException ex)
  {
    throw ex;
  }
  catch (Exception ex)
  {
    throw ex;
  }

  // Return the List of failed recipient e-mail addresses, so the client can maintain its list.
  return failedRecipients;
}

SmtpServerHostname的值為localhost,端口為25。

我非常肯定我可以使用Telnet發送郵件。 而且有效。

這是我從SSRS收到的錯誤消息:

ReportingServicesService!通知!4!08/28 / 2008-11:26:17 ::通知6ab32b8d-296e-47a2-8d96-09e81222985c已完成。 成功:錯誤,狀態:異常消息:發送郵件失敗。 Stacktrace:位於MyDeliveryExtension.MailDelivery.SendMail(SubscriptionData數據,流reportStream,字符串reportName,字符串smptServerHostname,Int32 smtpServerPort)在C:\\ inetpub \\ wwwroot \\ CustomReporting \\ MyDeliveryExtension \\ MailDelivery.cs:第48行,位於MyDeliveryExtension.MyDelivery.Deliver通知)在C:\\ inetpub \\ wwwroot \\ CustomReporting \\ MyDeliveryExtension \\ MyDelivery.cs:line 153,DeliveryExtension:我的交付,報告:點擊開發,嘗試1 ReportingServicesService!dbpolling!4!08/28 / 2008-11:26:17 :: NotificationPolling完成處理項目6ab32b8d-296e-47a2-8d96-09e81222985c

這可能與信任/代碼訪問安全性有關嗎?

我的交付擴展名被授予對rssrvpolicy.config的完全信任:

   <CodeGroup 
    class="UnionCodeGroup"
    version="1"
    PermissionSetName="FullTrust"
    Name="MyDelivery_CodeGroup"
    Description="Code group for MyDelivery extension">
    <IMembershipCondition class="UrlMembershipCondition" version="1" Url="C:\Program Files\Microsoft SQL Server\MSSQL.2\Reporting Services\ReportServer\bin\MyDeliveryExtension.dll" />
   </CodeGroup> 

在這里信任會成為問題嗎?

另一個理論:SQL Server和SSRS安裝在本地系統的安全上下文中。 我是對的,還是該服務帳戶對任何網絡資源的訪問受到限制? 甚至自己的SMTP服務器?

我嘗試將所有SQL Server Services登錄更改為Administrator-但仍然沒有成功。

我還嘗試通過提供以下代碼來登錄我的代碼中的SMTP服務器:NetworkCredential(“ Administrator”,“ password”)以及NetworkCredential(“ Administrator”,“ password”,“ MyRepServer”)

有人可以幫忙嗎?

FileStream m_fileStream = null;

m_files = notification.Report.Render(format, null);
RenderedOutputFile m_renderedOutputFile = m_files[0];
m_fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
m_renderedOutputFile.Data.Seek((long)0, SeekOrigin.Begin);
byte[] arr = new byte[(int)m_renderedOutputFile.Data.Length + 1];

m_renderedOutputFile.Data.Read(arr, 0, (int)m_renderedOutputFile.Data.Length);

m_fileStream.Write(arr, 0, (int)m_renderedOutputFile.Data.Length);

m_fileStream.Close();

什么是:

at MyDeliveryExtension.MailDelivery.SendMail(SubscriptionData data, Stream reportStream, String reportName, String smptServerHostname, Int32 smtpServerPort) 
  in C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MailDelivery.cs:line 48 

at MyDeliveryExtension.MyDelivery.Deliver(Notification notification) 
  in C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MyDelivery.cs:line 153

另外,您似乎正在處置報告流,但是應該通過打開該流的任何方法來完成,而不是通過您的方法來完成(附加流來處置它不會很明顯)。

由於您重新引發異常的方式,您丟失了堆棧跟蹤的一部分。 不要拋出ex變量,僅拋出就足夠了。

試試這個調整:

public static List<string> SendMail(SubscriptionData data, Stream reportStream, string reportName, string smptServerHostname, int smtpServerPort)
{
  List<string> failedRecipients = new List<string>();

  MailMessage emailMessage = new MailMessage(data.ReplyTo, data.To) {
      Priority = data.Priority,
      Subject = data.Subject,
      IsBodyHtml = false,
      Body = data.Comment
  };

  if (reportStream != null)
    emailMessage.Attachments.Add(new Attachment(reportStream, reportName));

  try
  {
      SmtpClient smtp = new SmtpClient(smptServerHostname, smtpServerPort);

      // Send the MailMessage
      smtp.Send(emailMessage);
  }
  catch (SmtpFailedRecipientsException ex)
  {
    // Delivery failed for the recipient. Add the e-mail address to the failedRecipients List
    failedRecipients.Add(ex.FailedRecipient);

    //are you missing a loop here? only one failed address will ever be returned
  }
  catch (SmtpFailedRecipientException ex)
  {
    // Delivery failed for the recipient. Add the e-mail address to the failedRecipients List
    failedRecipients.Add(ex.FailedRecipient);
  }

  // Return the List of failed recipient e-mail addresses, so the client can maintain its list.
  return failedRecipients;
}

我試圖刪除reportStream附件:

  //if (reportStream != null)    
     //emailMessage.Attachments.Add(new Attachment(reportStream, reportName));

現在工作正常。

因此,這與reportStream有關。

在弄亂了獲取reportStream的功能之后,我得以解決郵件發送問題。

該錯誤不在SendMail方法中,但在其他地方。 不過,在SendMail的上下文中引發了異常。 煩死了!

因此,您必須避免:

catch (Exception ex)
{
    throw ex;
}

因為基本上可以將您的例外掩蓋在新的例外中。

如果您使用:

catch (Exception ex)
{
    throw; //note: no ex
}

它保留原始異常和堆棧跟蹤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM