简体   繁体   English

如何获取那些控制台消息并通过邮件发送

[英]How to grab those console messages and sent in the mail message

I have module which is invoking a stored procedure from the vb.net console application like this. 我有从这样的vb.net控制台应用程序调用存储过程的模块。 This stored procedure has lot of print statements in it. 此存储过程中包含许多打印语句。 When I execute this console application, it will display messages in the console. 当我执行此控制台应用程序时,它将在控制台中显示消息。 I want to grab those messages and send through a mail 我想获取这些消息并通过邮件发送

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()

My module for sending mail is like this : 我发送邮件的模块是这样的:

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

Everything is working fine when fail, My question is, how to grab those console messages and sent in the mail message? 失败时一切正常,我的问题是,如何获取那些控制台消息并发送到邮件中?

Take the same Exception ex.Message, ex.Source and use it as parameters for your method sendmailwhenfail(), for example: 以相同的异常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);
}

so, whatever the error message is in the exception will also be passed onto the SendMail method. 因此,无论异常中的错误消息是什么,都将传递到SendMail方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM