【发布时间】:2019-03-22 12:49:12
【问题描述】:
我有一个 Winform 应用程序,我想从中发送电子邮件,我可以使用 sendgrid 并输入电子邮件地址来完成。问题是我要发送的电子邮件会根据情况发送给不同的人,因此“to field”需要是动态的。
我有一个 SQL 数据库,用于存储人们希望收到通知的事件的电子邮件地址,我在准备发送电子邮件时拉出该列表,但我不知道如何将这些项目放入 @987654321 的列表中@Sendgrid 想要的。
try
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MfgDataCollector"].ToString()))
{
var apiKey = "SendGrid Key";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("test@gmail.com", "test from App");
//Standard Email lists
//var tos = new List<EmailAddress>
//{
//new EmailAddress("test@gmail.com"),
//new EmailAddress("test2@gmail.com")
//};
//Query to get emails from SQL using Dapper
DynamicParameters param = new DynamicParameters();
param.Add("@Zone", Variables.Zone);
param.Add("@Gatelevel", Lbl_GateLevel.Text);
List<AlertMessages> EmailList = conn.Query<AlertMessages>("GetEmailList", param, commandType: CommandType.StoredProcedure).ToList<AlertMessages>();
//Lost here??? Not sure how to get EmailList to EmailAddresses
var tos = new List<EmailAddress> { };
string subject = txt_Subject.Text;
string plainTextContent = txt_Body.Text;
var htmlContent = txt_Body.Text;
var showAllRecipients = true; // Set to true if you want the recipients to see each others email addresses
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from,
tos,
subject,
plainTextContent,
htmlContent,
showAllRecipients
);
var response = await client.SendEmailAsync(msg);
MessageBox.Show("SendGrid Completed");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Send Email Notification - SendGrid - Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
警报消息类
public class AlertMessages
{
public int ID { get; set; }
public string EmailAddress { get; set; }
public int ZoneID { get; set; }
public string Zone { get; set; }
public string GateLevel { get; set; }
public string Emails { get; set; }
}
【问题讨论】:
-
在你的
AlertMessages类中EmailAddress和Emails有什么区别?