发送电子邮件从一定义SQL服务器上报服务的提供扩展
-
09-06-2019 - |
题
我已经开发了自己的递延用于报告服务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。
我veryfied,实际上我可以发送邮件,通过使用Telnet。和它的工作。
这里是错误消息我得从SSR:
年!的通知!4!08/28/2008-11:26:17::通知6ab32b8d-296e-47a2-8d96-09e81222985c完成。成功:假,状况:异常的信息:未发送的邮件。堆栈跟踪:在MyDeliveryExtension.MailDelivery.SendMail(SubscriptionData数据,流reportStream,串reportName,串smptServerHostname,Int32smtpServerPort)在C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MailDelivery.cs:line 48 在MyDeliveryExtension.MyDelivery.提供(通知通知)在C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MyDelivery.cs:line 153,DeliveryExtension:我送货,报告:点击的发展,试图1 年!dbpolling!4!08/28/2008-11:26:17::NotificationPolling完成处理项目6ab32b8d-296e-47a2-8d96-09e81222985c
这可能与信任/代码的访问权的安全?
我的递延长被授予完全的信任rssrvpolicy.配置:
<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服务器和固态继电器安装在安全方面的当地系统。我是正确的,或是这种服务的帐户受限制的访问网络的任何资源?即使它自己的SMTP Server?
我试图改变所有SQL服务器服务的登录管理员-但还是没有任何成功。
我还想记录上SMTP server在我的代码,通过proviiding:NetworkCredential("管理员","password")和也NetworkCredential("管理员"、"密码"、"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
你也似乎是处理该报告流,但是,应该通过什么打开这一流的,不是你的方法(这不是显而易见的是,将流处置)。
你失去的一部分,栈跟踪由于你怎么重新再扔例外情况。不要扔前变,只是把是不够的。
试试这个调整:
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.
后打打闹闹的tunctionallity得到reportStream,我能够解决邮件发送的问题。
错不在SendMail方法,但somewehere人。唯一的例外是扔在这方面,SendMail虽然。鸡奸!
这就是为什么你要避免:
catch (Exception ex)
{
throw ex;
}
因为这基本上你的斗篷除外,在新的一个。
如果你使用:
catch (Exception ex)
{
throw; //note: no ex
}
它保持原来的例外和堆踪。
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();