我有两个独立的功能 一种是获取特定列的计数 另一个是通过c#代码发送邮件 两者都运作良好 我想将数据表中的计数值发送到邮件正文?有人帮我吗?
public static void push_notify()
{
var no = "25/10/2017";
try
{
string connection = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "sampleone";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@createdOn",no);
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine();
for (int x = 0; x < dt.Columns.Count; x++)
{
Console.Write(row[x].ToString() + " ");
}
}
con.Close();
}
catch(Exception)
{
}
}
public static void email_send()
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("someemail@somedomain.com");
mail.To.Add("somereceiver@somedomain.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\Users\\vijay\\Downloads\\dancdan.xml"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("someemail@somedomain.com", "somepassword");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}
答案 0 :(得分:0)
您的第一个函数应该返回您要通过电子邮件发送的数据,如下所示:
public static List<string> push_notify()
{
var no = "25/10/2017";
var result = new List<string>();
try
{
string connection = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(connection);
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "sampleone";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@createdOn",no);
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine();
for (int x = 0; x < dt.Columns.Count; x++)
{
var value = row[x].ToString();
Console.Write(value + " ");
result.Add(value);
}
}
con.Close();
return result;
}
catch(Exception)
{
return result;
}
}
第二个函数应该调用第一个函数,就像这样(假设它们在同一个命名空间中):
public static void email_send()
{
using (MailMessage mail = new MailMessage())
{
var dataToSend = push_notify();
mail.From = new MailAddress("someemail@somedomain.com");
mail.To.Add("somereceiver@somedomain.com");
mail.Subject = "Hello World";
mail.Body = string.Join(",", dataToSend);
mail.IsBodyHtml = true;
mail.Attachments.Add(new Attachment("C:\\Users\\vijay\\Downloads\\dancdan.xml"));
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new NetworkCredential("someemail@somedomain.com", "somepassword");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}
这应该可以解决问题。但是,请尝试通过评论考虑您收到的所有建议。