我有一个模块,它从这个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
失败时一切正常,我的问题是,如何获取这些控制台消息并通过邮件消息发送?
答案 0 :(得分: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方法。