Tôi có yêu cầu gửi kết quả truy vấn trong email. Tôi đang sử dụng hai phương pháp:Gửi một bảng trong email
GetDataTable(): để thực hiện truy vấn và lấy DataTable (mà cần phải được gửi bằng thư điện tử)
SendAutomatedEmail(): gửi email tự động.
Sự cố: tôi cần gửi bảng dữ liệu hoặc bảng html trong email, giống như mã bên dưới. điều này hoạt động tốt cho một chuỗi thay cho 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 = "[email protected]")
{
try
{
string mailServer = "server.com";
MailMessage message = new MailMessage(
"[email protected]",
recipient,
"Test Email",
dt.ToString()
);
SmtpClient client = new SmtpClient(mailServer);
var AuthenticationDetails = new NetworkCredential("[email protected]", "password");
client.Credentials = AuthenticationDetails;
client.Send(message);
}
catch (Exception e)
{
}
}
có bàn phải trong cơ thể của email? – AllenG
có, nội dung email là bảng – 14578446
Bạn chỉ có thể viết ra html liệu phục vụ xây dựng một bảng, và đặt nó trong cơ thể email, ví dụ .: