Skip to main content
 首页 » 编程设计

c#之从自定义 SQL Server Reporting Services Delivery Extension 发送电子邮件

2024年01月13日32www_RR

我已经为 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!notification!4!08/28/2008-11:26:17::通知 6ab32b8d-296e-47a2-8d96-09e81222985c 已完成。成功:错误,状态:异常消息:发送邮件失败。堆栈跟踪:在 C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MailDelivery.cs 中的 MyDeliveryExtension.MailDelivery.SendMail(SubscriptionData data, Stream reportStream, String reportName, String smptServerHostname, Int32 smtpServerPort):第 48 行 在 C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MyDelivery.cs 中的 MyDeliveryExtension.MyDelivery.Deliver(通知通知):第 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 服务登录更改为管理员 - 但仍然没有成功。

我还尝试通过提供以下代码在我的代码中登录 SMTP 服务器:NetworkCredential("Administrator", "password") 和 NetworkCredential("Administrator", "password", "MyRepServer")

有人可以帮忙吗?

请您参考如下方法:

什么是:

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