Azure WebJob 到:从 SQL 创建 CSV 文件并通过电子邮件作为附件发送
Azure WebJob to: create CSV file from SQL and send as attachment via email
到目前为止,设法:
- 通过 Visual Studio 创建 C# Azure WebJob 项目并将其发布到 Web 应用程序,即:
- 连接到 Azure SQL 数据库并执行 SQL 查询(通过 SqlDataReader)
- 将 SqlDataReader 结果添加到电子邮件正文中
- 发送电子邮件
除了上述之外,在上述第 3 点和第 4 点之间的某处,我需要:
- 创建 .CSV 文件
- 从 SqlDataReader 填充 .CSV 文件
- 通过电子邮件将 .CSV 文件作为附件发送
从 SqlDataReader 填充 CSV 的结果集如下所示:
asdasd@gmail.com ,11/19/2018
asdasdasd@gmail.com ,11/19/2018
asdasdasasdas@live.co.uk ,11/19/2018
asdasddsa@hotmail.com ,11/19/2018
asdasd@hotmail.com ,11/19/2018
asdasddsa@hotmail.com ,11/19/2018
asdasasd@gmail.com ,11/18/2018
以下是我目前所拥有的:
public static void Main(string[] args)
{
SmtpClient smtp = new SmtpClient();
int SMTP_PORT = 587;
Int32.TryParse(ConfigurationManager.AppSettings["SMTP_PORT"], out SMTP_PORT);
smtp.Port = SMTP_PORT;
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"], ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
string mailFrom = ConfigurationManager.AppSettings["SMTP_MAIL_FROM"];
string mailSubject = ConfigurationManager.AppSettings["SMTP_MAIL_SUBJECT"];
using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["AzureDBConnString"]))
{
connection.Open();
var queryString = @"SELECT * FROM MyTable WHERE Status = 1";
using (SqlCommand command = new SqlCommand(queryString, connection))
{
command.CommandTimeout = 120;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read()) // loop each user and send email
{
bool emailSentSuccess = true;
using (MailMessage mail = new MailMessage())
{
try
{
mail.From = new MailAddress(mailFrom);
mail.To.Add(new MailAddress(reader["EmailAddress"].ToString()));
mail.IsBodyHtml = true;
mail.Subject = mailSubject;
mail.Body = reader["EmailBody"].ToString();
smtp.Send(mail);
}
catch (Exception ex)
{
emailSentSuccess = false;
}
}
}
}
}
}
}
问题: 我怎样才能达到第 5、6、7 点?
您可以在SqlCommand
中参考以下代码。
using (SqlCommand command = new SqlCommand(queryString, connection))
{
command.CommandTimeout = 120;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
StringBuilder sb = new StringBuilder();
List<string> columnNames = new List<string>();
List<string> rows = new List<string>();
for (int i = 0; i < reader.FieldCount; i++)
{
string tmpColumnName = reader.GetName(i);
columnNames.Add(tmpColumnName);
}
sb.Append(string.Join(",", columnNames.ToArray())).Append("\r\n");
List<string> currentRow = new List<string>();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
object item = reader[i];
sb.Append(item.ToString().Replace(",", ";") + ',');
}
sb.Append("\r\n");
}
bool emailSentSuccess = true;
using (MailMessage mail = new MailMessage())
{
try
{
using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString())))
{
Attachment attachment = new Attachment(stream, new ContentType("text/csv"));
attachment.Name = "hello.csv";
mail.Attachments.Add(attachment);
mail.From = new MailAddress(mailFrom);
mail.To.Add(new MailAddress(reader["EmailAddress"].ToString()));
mail.IsBodyHtml = true;
mail.Subject = mailSubject;
mail.Body = reader["EmailBody"].ToString();
smtp.Send(mail);
}
}
catch (Exception ex)
{
emailSentSuccess = false;
}
}
}
}
我测试的输出如下:
到目前为止,设法:
- 通过 Visual Studio 创建 C# Azure WebJob 项目并将其发布到 Web 应用程序,即:
- 连接到 Azure SQL 数据库并执行 SQL 查询(通过 SqlDataReader)
- 将 SqlDataReader 结果添加到电子邮件正文中
- 发送电子邮件
除了上述之外,在上述第 3 点和第 4 点之间的某处,我需要:
- 创建 .CSV 文件
- 从 SqlDataReader 填充 .CSV 文件
- 通过电子邮件将 .CSV 文件作为附件发送
从 SqlDataReader 填充 CSV 的结果集如下所示:
asdasd@gmail.com ,11/19/2018
asdasdasd@gmail.com ,11/19/2018
asdasdasasdas@live.co.uk ,11/19/2018
asdasddsa@hotmail.com ,11/19/2018
asdasd@hotmail.com ,11/19/2018
asdasddsa@hotmail.com ,11/19/2018
asdasasd@gmail.com ,11/18/2018
以下是我目前所拥有的:
public static void Main(string[] args)
{
SmtpClient smtp = new SmtpClient();
int SMTP_PORT = 587;
Int32.TryParse(ConfigurationManager.AppSettings["SMTP_PORT"], out SMTP_PORT);
smtp.Port = SMTP_PORT;
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SMTP_USERNAME"], ConfigurationManager.AppSettings["SMTP_PASSWORD"]);
smtp.Host = ConfigurationManager.AppSettings["SMTP_HOST"];
string mailFrom = ConfigurationManager.AppSettings["SMTP_MAIL_FROM"];
string mailSubject = ConfigurationManager.AppSettings["SMTP_MAIL_SUBJECT"];
using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["AzureDBConnString"]))
{
connection.Open();
var queryString = @"SELECT * FROM MyTable WHERE Status = 1";
using (SqlCommand command = new SqlCommand(queryString, connection))
{
command.CommandTimeout = 120;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read()) // loop each user and send email
{
bool emailSentSuccess = true;
using (MailMessage mail = new MailMessage())
{
try
{
mail.From = new MailAddress(mailFrom);
mail.To.Add(new MailAddress(reader["EmailAddress"].ToString()));
mail.IsBodyHtml = true;
mail.Subject = mailSubject;
mail.Body = reader["EmailBody"].ToString();
smtp.Send(mail);
}
catch (Exception ex)
{
emailSentSuccess = false;
}
}
}
}
}
}
}
问题: 我怎样才能达到第 5、6、7 点?
您可以在SqlCommand
中参考以下代码。
using (SqlCommand command = new SqlCommand(queryString, connection))
{
command.CommandTimeout = 120;
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
StringBuilder sb = new StringBuilder();
List<string> columnNames = new List<string>();
List<string> rows = new List<string>();
for (int i = 0; i < reader.FieldCount; i++)
{
string tmpColumnName = reader.GetName(i);
columnNames.Add(tmpColumnName);
}
sb.Append(string.Join(",", columnNames.ToArray())).Append("\r\n");
List<string> currentRow = new List<string>();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
object item = reader[i];
sb.Append(item.ToString().Replace(",", ";") + ',');
}
sb.Append("\r\n");
}
bool emailSentSuccess = true;
using (MailMessage mail = new MailMessage())
{
try
{
using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(sb.ToString())))
{
Attachment attachment = new Attachment(stream, new ContentType("text/csv"));
attachment.Name = "hello.csv";
mail.Attachments.Add(attachment);
mail.From = new MailAddress(mailFrom);
mail.To.Add(new MailAddress(reader["EmailAddress"].ToString()));
mail.IsBodyHtml = true;
mail.Subject = mailSubject;
mail.Body = reader["EmailBody"].ToString();
smtp.Send(mail);
}
}
catch (Exception ex)
{
emailSentSuccess = false;
}
}
}
}
我测试的输出如下: