【问题标题】:How to grab those console messages and sent in the mail message如何获取这些控制台消息并在邮件消息中发送
【发布时间】:2013-04-03 18:41:04
【问题描述】:

我有一个模块,它像这样从 vb.net 控制台应用程序调用存储过程。这个存储过程中有很多打印语句。当我执行此控制台应用程序时,它将在控制台中显示消息。我想获取这些消息并通过邮件发送

Dim Con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand("SaveReceiptsFromFile_GFF", Con)

Try
   Using Con
      Con.Open()

      cmd.ExecuteNonQuery()

   End Using
Catch ex As SqlException
   Console.WriteLine(ex.Message)

   sendmailwhenfail()
End Try
Con.Close()

我的邮件发送模块是这样的:

Public Shared Sub sendmailwhenfail()
        Try
            Dim AnEmailMessage As New MailMessage
            AnEmailMessage.From = New MailAddress("tester@gmail.com")
            AnEmailMessage.To.Add("mymail@xxxxx.com")
            AnEmailMessage.Subject = "this is test subject"
            AnEmailMessage.Body = "this is test body"
            AnEmailMessage.Priority = MailPriority.High
            Dim SimpleSMTP As New SmtpClient("smtp.gmail.com")
            With SimpleSMTP
                .Port = 587
                .EnableSsl = True
                .Credentials = _
                New NetworkCredential("tester@gmail.com", "Tester123")
                .Send(AnEmailMessage)
            End With
            MsgBox("Email sent")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

失败时一切正常,我的问题是,如何获取这些控制台消息并在邮件消息中发送?

【问题讨论】:

    标签: asp.net sql sql-server vb.net


    【解决方案1】:

    获取相同的异常ex.Message,ex.Source并将其用作您的方法sendmailwhenfail()的参数,例如:

    try
    {
        // do your thing
    }
    catch (Exception ex)
    {
        Console.Writeline(String.Format("{0} :: {1}", ex.Source, ex.Message));
        SendMail(ex.Source, ex.Message);
    }
    

    所以,无论异常中的错误消息是什么,都会传递给 SendMail 方法。

    【讨论】:

    • 我的 sendmailwhenfail() 没有任何参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多