0

我有一个模块,它像这样从 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

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

4

1 回答 1

0

获取相同的异常 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 方法。

于 2013-04-03T18:50:22.010 回答