L'invio di e-mail Personalizzata a SQL Server Reporting Services Estensione per il Recapito
-
09-06-2019 - |
Domanda
Ho sviluppato la mia estensione per il recapito per Reporting Services 2005, per l'integrazione di questa con il nostro SaaS soluzione di marketing.
Ci vuole l'abbonamento, e prende una snapshot del report con un set di parametri personalizzato.Viene quindi eseguito il rendering del report, invia una e-mail con un link e la relazione, allegata in formato XLS.
Tutto funziona bene, fino a quando la consegna della posta...
Ecco il mio codice per l'invio di e-mail:
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;
}
Valori per SmtpServerHostname è localhost e la porta 25.
Ho veryfied che in realtà posso inviare messaggi di posta, utilizzando Telnet.E funziona.
Ecco il messaggio di errore mi da SSRS:
ReportingServicesService!notifica!4!08/28/2008-11:26:17::Notifica 6ab32b8d-296e-47a2-8d96-09e81222985c completato.Successo:Falso, Lo Status Di:Messaggio Di Eccezione:Errore durante l'invio della posta.Stacktrace:a MyDeliveryExtension.MailDelivery.SendMail(SubscriptionData dati, Flusso reportStream, String reportName, String smptServerHostname, Int32 smtpServerPort) in C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MailDelivery.cs:line 48 a MyDeliveryExtension.MyDelivery.Consegnare(notifica la Notifica) in C:\inetpub\wwwroot\CustomReporting\MyDeliveryExtension\MyDelivery.cs:line 153, DeliveryExtension:Il Mio Recapito, Report:Clic Sviluppo, Tentativo 1 ReportingServicesService!dbpolling!4!08/28/2008-11:26:17::NotificationPolling finito l'elaborazione dell'elemento 6ab32b8d-296e-47a2-8d96-09e81222985c
Questo potrebbe avere qualcosa a che fare con la Fiducia/Sicurezza dall'Accesso di Codice?
La mia estensione per il recapito è concesso piena fiducia in 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>
Poteva fidarsi essere un problema?
Un'altra teoria:SQL Server e SSRS è stato installato nel contesto di protezione del Sistema Locale.Sono di destra, o è questo account di servizio limitato l'accesso a qualsiasi risorsa di rete?Anche il proprio Server SMTP?
Ho provato a cambiare tutti i Servizi SQL Server accessi da Amministratore, ma ancora senza successo.
Ho provato anche la registrazione sul server SMTP nel mio codice, da proviiding:NetworkCredential("Amministratore", "password") e anche NetworkCredential("Amministratore", "password", "MyRepServer")
Chiunque può aiutare qui, per favore?
Soluzione
Che cosa è in:
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
Anche a te sembra di smaltire il rapporto di flusso, ma che dovrebbe essere fatto da qualsiasi inaugurato il flusso, non è il tuo metodo (non sarà ovvio che il collegamento di un flusso dispone).
Stai perdendo parte del tuo stack trace a causa di come si ri-generare eccezioni.Non gettare l'ex variabile, a pochi passi è sufficiente.
Provare questo tweak:
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;
}
Altri suggerimenti
Ho provato a rimuovere la reportStream Allegato:
//if (reportStream != null)
//emailMessage.Attachments.Add(new Attachment(reportStream, reportName));
E ora funziona benissimo.
Quindi è qualcosa a che fare con il reportStream.
Dopo di scherzare con il tunctionallity che ottiene il reportStream, sono stato in grado di risolvere il problema di invio di posta elettronica.
L'errore non era nel metodo SendMail, ma somewehere altro.L'eccezione è stata lanciata nel contesto, di SendMail però.Inculata!
Ecco perché è necessario evitare:
catch (Exception ex)
{
throw ex;
}
Che, fondamentalmente, mantelli tuo eccezione in uno nuovo.
Se si utilizza:
catch (Exception ex)
{
throw; //note: no ex
}
Mantiene l'eccezione originale e stack.
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();