gpt4 book ai didi

c# - 在电子邮件中发送表格

转载 作者:太空狗 更新时间:2023-10-29 20:59:25 28 4
gpt4 key购买 nike

我需要通过电子邮件发送查询结果。我正在使用两种方法:

GetDataTable() : 执行查询并获取数据表(需要通过邮件发送)

SendAutomatedEmail():发送自动电子邮件。

问题:我需要在电子邮件中发送数据表或 html 表,如下面的代码。这适用于代替 dataTable 的字符串

public static void Main(string[] args)
{
DataTable datatable = GetDataTable();
SendAutomatedEmail(datatable );
}

public static DataTable GetDataTable(string CommandText)
{
string cnString = ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(cnString);

string CommandText = "select * from dbo.fs010100 (nolock)";
SqlCommand sqlCommand = new SqlCommand( CommandText, sqlConnection);

SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;

DataTable dataTable = new DataTable();
dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;

// Adds or refreshes rows in the DataSet to match those in the data source
try
{
sqlDataAdapter.Fill(dataTable);
sqlConnection.Close(dataTable );
}
catch (Exception _Exception)
{
sqlConnection.Close();
//Console.WriteLine(_Exception.Message);
return null;
}

return dataTable;
}


public static void SendAutomatedEmail(DataTable dt, string recipient = "user@domain.com")
{
try
{
string mailServer = "server.com";

MailMessage message = new MailMessage(
"it@domain.com",
recipient,
"Test Email",
dt.ToString()
);
SmtpClient client = new SmtpClient(mailServer);
var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
client.Credentials = AuthenticationDetails;
client.Send(message);
}
catch (Exception e)
{

}

}

最佳答案

好的,现在试试这个:

public static void Main(string[] args)
{
DataSet dataSet = getDataSet();
string htmlString= getHtml(dataSet);
SendAutomatedEmail(htmlString, "email@domain.com");
}

public static DataSet getDataSet(string CommandText)
{
string cnString = ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(cnString);

string CommandText = "select * from dbo.fs010100 (nolock)";
SqlCommand sqlCommand = new SqlCommand( CommandText, sqlConnection);

SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;

DataSet dataSet = new DataSet();

try
{

sqlDataAdapter.Fill(dataSet, "header");
sqlConnection.Close();
}
catch (Exception _Exception)
{
sqlConnection.Close();

return null;
}

return dataSet;

}


public static string getHtml(DataSet dataSet)
{
try
{
string messageBody = "<font>The following are the records: </font><br><br>";

if (dataSet.Tables[0].Rows.Count == 0)
return messageBody;
string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
string htmlTableEnd = "</table>";
string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
string htmlHeaderRowEnd = "</tr>";
string htmlTrStart = "<tr style =\"color:#555555;\">";
string htmlTrEnd = "</tr>";
string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
string htmlTdEnd = "</td>";

messageBody+= htmlTableStart;
messageBody += htmlHeaderRowStart;
messageBody += htmlTdStart + "Column1 " + htmlTdEnd;
messageBody += htmlHeaderRowEnd;

foreach (DataRow Row in notShippedDataSet.Tables[0].Rows)
{
messageBody = messageBody + htmlTrStart;
messageBody = messageBody + htmlTdStart + Row["fieldName"] + htmlTdEnd;
messageBody = messageBody + htmlTrEnd;
}
messageBody = messageBody + htmlTableEnd;


return messageBody;
}
catch (Exception ex)
{
return null;
}
}

public static void SendAutomatedEmail(string htmlString, string recipient = "user@domain.com")

{
try
{
string mailServer = "server.com";

MailMessage message = new MailMessage("it@domain.com", recipient);
message .IsBodyHtml = true;
message .Body = htmlString;
message .Subject = "Test Email";

SmtpClient client = new SmtpClient(mailServer);
var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
client.Credentials = AuthenticationDetails;
client.Send(message);
}
catch (Exception e)
{

}

}

关于c# - 在电子邮件中发送表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8811353/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com