简体   繁体   English

C#将Console.Writeline输出转换为用于电子邮件的字符串

[英]C# Converting Console.Writeline outputs to String for email

I have a C# Console application which outputs the data taken from mySQL however the output needs to be sent/redirected to an email via SMTP (Gmail). 我有一个C#控制台应用程序,该应用程序输出从mySQL获取的数据,但是需要通过SMTP(Gmail)将输出发送/重定向到电子邮件。 Is it possible to convert all the output to a string or stream like PHP which could read all the output and echo them out in the email under messages ? 是否可以将所有输出转换为类似PHP的字符串或流,该字符串或流可以读取所有输出并在messages下的电子邮件中回显它们?

An Example of the code: 代码示例:

myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
    string datetype = (myReader["Expiry_Date"].ToString());
    DateTime dateformat = DateTime.Parse(datetype); 

    if(dateformat.Date <= calDate3b && dateformat.Date>=timenow)
    {
        Console.WriteLine(myReader["Number"].ToString());
        Console.WriteLine(myReader["Name"].ToString());
        Console.WriteLine(myReader["Number_Yolo"].ToString());
        Console.WriteLine(myReader["Date"].ToString());

        Console.WriteLine();                        
    }
}

Console.WriteLine("Your Message");
mail.Body = <contains the console output>;

I have tried StreamOutput but it does not work. 我已经尝试了StreamOutput但是它不起作用。

myReader = myCommand.ExecuteReader();
var message = new StringBuilder();
        while (myReader.Read())
        {
            string datetype = (myReader["Expiry_Date"].ToString());
            DateTime dateformat = DateTime.Parse(datetype); 

            if(dateformat.Date <= calDate3b && dateformat.Date>=timenow)
            {
                message.AppendLine(myReader["Number"].ToString());
                message.AppendLine(myReader["Name"].ToString());
                message.AppendLine(myReader["Number_Yolo"].ToString());
                message.AppendLine(myReader["Date"].ToString());
                message.AppendLine();                    
            }
        }

Console.WriteLine(message.ToString());
mail.Body = message.ToString();

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

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