【发布时间】:2018-11-23 17:08:00
【问题描述】:
到目前为止,设法:
- 通过 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 点?
【问题讨论】:
-
不确定网络作业,但您可以使用逻辑应用轻松实现此目的,无需编码。参考我的文章here
标签: sql azure azure-sql-database azure-web-app-service azure-webjobs