我有一段C#代码,它响应来自Azure的HTTP触发器并被执行。这段代码会发送一封包含警告的电子邮件:
#r "Newtonsoft.Json"
#r "SendGrid"
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static SendGridMessage Run(HttpRequest req, ILogger log)
{
string requestBody = new StreamReader(req.Body).ReadToEnd();
log.LogInformation(requestBody);
var notifications = JsonConvert.DeserializeObject<IList<Notification>>(requestBody);
SendGridMessage message = new SendGridMessage();
message.Subject = "Sensor Anomaly Warning";
var content = "The following device has started registering anomalies:<br/><br/><table><tr><th>time</th><th>value</th><th>lower bound</th><th>upper bound</th><th>device</th></tr>";
foreach(var notification in notifications) {
log.LogInformation($" - time: {notification.time}, value: {notification.value}, lowerbound: {notification.lowerbound}, upperbound: {notification.upperbound}, device: {notification.device}");
content += $"<tr><td>{notification.time}</td><td>{notification.value}</td><td>{notification.lowerbound}</td><td>{notification.upperbound}</td><td>{notification.device}</td></tr>";
}
content += "</table>";
message.AddContent("text/html", content);
return message;
}
public class Notification
{
public string time { get; set; }
public string value { get; set; }
public string lowerbound { get; set; }
public string upperbound { get; set; }
public string device { get; set; }
}
现在,我想执行这段发送电子邮件的代码,但是基于notification.alert
的值,如果异常开始,notification.alert
存储0,如果警报停止,notification.alert
存储1.来自python,这就像设置一个“if else”语句一样简单,在每种情况下都会调用这个函数。
然而对于这个C#代码,没有函数可以调用。这段代码发送一封电子邮件,但它只是创建一个类。无论如何,我想知道是否可以在C#中使用基于notification.alert
值的“if else”语句,在一种情况下发送一封电子邮件,内容类似于“该设备已开始注册异常”,而在另一种情况下发送“该设备已停止注册异常”。我不能这样做,因为我必须处理notification
对象,它已经在类中了。
我必须说,我不是一个C#开发人员,而是一个Python开发人员,因此产生了怀疑。
1条答案
按热度按时间k10s72fa1#
您可以使用if/else语句或switch来更新基于
notification.alert
的电子邮件正文。此函数不发送电子邮件,它只是为电子邮件正文文本设置消息。