Ejemplo de proceso dll para envío de correo con informes PDF adjuntos

Publicado más de 3 años hace por Flexygo Team

Publicar un tema
Flexygo Team
Flexygo Team

En este ejemplo veremos cómo enviar un correo utilizando las tablas de Mails_Outbox y Mails_Outbox_Attachments

Además insertamos un aviso para que se vea en la campana de notificaciones y nos lleve al mail enviado.




Public Shared Function CobroEnviarJustificante(ByVal Entity As EntityObject, ByRef Ret As ProcessHelper, aReportName As String, aEmail As String, aSubject As String, aBody As String, aPath As String, aFileName As String, aCC <span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>As Object) As Boolean
        Dim dm As New FLEXYGO.Data.DataManager("DataConnectionString")
        Dim cdm As New FLEXYGO.Data.DataManager("ConfConnectionString")
        Dim t As New Translator
        Dim Agente As String = dm.GetValue(String.Format("Select Nombre + ' ' + Apellidos from Empleados_Datos where IdEmpleado={0}", Entity("IdEmpleado")))

        Try

            Dim Mail As New EntityObject("sysOutboxMail", Ret.ConfToken)
            Dim Notice As New EntityObject("sysNotice", Ret.ConfToken)

            Dim ReportDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
            Dim PathAttachment As String = Ret.ConfToken.MapPath(aPath & aFileName)

            If Not IO.Directory.Exists(Ret.ConfToken.MapPath(aPath)) Then
                IO.Directory.CreateDirectory(Ret.ConfToken.MapPath(aPath))
            End If
            ReportDoc = Entity.GetCrystalReportDocument(aReportName, "", FLEXYGO.Reporting.eReportModes.PDF, Nothing).ReportDoc
            ReportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Ret.ConfToken.MapPath(PathAttachment))


            Mail("Subject") = aSubject
            Mail("To") = aEmail
            Mail("FromEmail") = System.Configuration.ConfigurationManager.GetSection("system.net/mailSettings/smtp").From.ToString()
            Mail("FromName") = Agente
            Mail("Body") = aBody
            Mail("Status") = 1
            Mail("ObjectName") = "crm_Actuacion"
            Mail("ObjectWhere") = "Seguimiento.IdSeguimiento=" & Entity("IdSeguimiento")
            Mail("ReplyTo") = Ret.ConfToken.UserSecurity.Email

            Notice("Title") = t.Translate("Envío de justificante de cobro a", eTranslation_Areas.Messages) & " " & aEmail
            Notice("Message") = aSubject
            Notice("ReminderTime") = Date.Now
            Notice("ExpiryTime") = Date.Now.AddDays(7)
            Notice("AllUsers") = "false"
            Notice("AllUsersIsSent") = "false"
            Notice("MethodName") = "app"
            Notice("TypeId") = "object"
            Notice("PageTypeId") = "View"
            Notice("ObjectName") = "sysOutboxMail"
            Notice("PageName") = ""
            Notice("TargetId") = "popup"
            Notice("InsertDate") = Date.Now



            If Mail.Insert() Then
                Notice("ObjectWhere") = "(Mails_Outbox.Id='" & Mail("id").ToString & "')"

                Dim Flag As Boolean = False
                Dim MailAttachmentError As New Exception
                Dim MailAttachment As New EntityObject("sysOutboxMailAttachment", Ret.ConfToken)

                MailAttachment("MailId") = Mail("Id")
                MailAttachment("Path") = PathAttachment
                MailAttachment("FileName") = aFileName

                If Not MailAttachment.Insert() Then
                    Flag = True
                    MailAttachmentError = MailAttachment.LastException
                Else
                    If Not Notice.Insert() Then
                        Flag = True
                        MailAttachmentError = MailAttachment.LastException
                        Return True
                    Else
                        Dim NoticeUser As New EntityObject("sysNoticeUser", Ret.ConfToken)
                        NoticeUser("NoticeId") = Notice("NoticeId")
                        NoticeUser("UserId") = Ret.ConfToken.UserSecurity.UserId

                        If Not NoticeUser.InsertProcess(NoticeUser.TableName, Settings.ObjectSettings.eUpdateType.Standard, Nothing) Then
                            Flag = True
                            MailAttachmentError = MailAttachment.LastException
                            Return True
                        End If
                    End If
                End If

                If Flag Then
                    Ret.Success = False
                    Ret.LastException = MailAttachmentError
                    Return False
                End If
            Else
                Ret.Success = False
                Ret.LastException = Mail.LastException
                Return False
            End If

            Ret.Success = True
            Ret.SuccessMessage = t.Translate("Se ha enviado el correo", eTranslation_Areas.Messages)
            Return True

        Catch ex As Exception
            Ret.Success = False
            Ret.LastException = ex
            Return False
        Finally
            If dm IsNot Nothing Then
                dm.Close()
            End If
            If cdm IsNot Nothing Then
                cdm.Close()
            End If
        End Try

    End Function


0 Votos


0 Comentarios

Iniciar sesión o Registrarse para publicar un comentario