c# - 无法从与 slack 集成的机器人发送邮件
问题描述
我有一个使用 c# 在框架 v4 中制作的机器人。如果无法从 qna maker 获取答案,该机器人会向管理员发送电子邮件。它在本地模拟器上完全可以正常工作,我可以发送邮件,但是当通过 slack 发布时机器人抛出错误。这是代码私有异步任务 ProcessSampleQnAAsync(WaterfallStepContext stepContext, CancellationToken cancelToken) {
var results = await _botServices.SampleQnA.GetAnswersAsync(stepContext.Context);
if (results.Any())
{
await stepContext.Context.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
}
else
{
var question = stepContext.Context.Activity.Text;
await stepContext.Context.SendActivityAsync(MessageFactory.Text(_configuration["DefaultAnswer"]), cancellationToken);
string mailbody = "<h4><b>Hi Admin , following question was not answered from bot : </b> </h4>" + question;
Common objCommon = new Common(_configuration);
objCommon.SendEmail(_configuration["EmailSubject_UnAnswerQuestion"], mailbody);
}
}
appsetting.json
"AdminEmailAddress": "****@****.com", //to Email Address
"FromEmailAddress": "******@gmail.com", // From email address
"SMTP_Host": "smtp.gmail.com", // SMTP client Host Name
"SMTP_Port": "587", // SMTP port number
"SMTP_UserName": "****@gmail.com", // email server user name (Email server
network credentials)
"SMTP_Password": "******", // email server password (Email server network
credentials)
"EmailSubject_UnAnswerQuestion": "Question whose answer not found", //
Subject string for un- answere question email.
"DefaultAnswer": "Sorry, could not find an answer in the Q and A system.", //
default answere if answere not found in QnA.
"DelayTimeInSeconds": "60" // Feed back dely time in seconds.
smtp的常用逻辑
public class Common { private readonly IConfiguration _configuration; public Common(IConfiguration configuration) { _configuration = configuration; } public bool SendEmail(string subject, string mailbody) { string to = _configuration["AdminEmailAddress"]; //To address string from = _configuration["FromEmailAddress"]; //From address MailMessage message = new MailMessage(from, to); message.Subject = subject; message.Body = mailbody; message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; SmtpClient client = new SmtpClient(_configuration["SMTP_Host"], int.Parse(_configuration["SMTP_Port"])); //Gmail smtp System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential(_configuration["SMTP_UserName"], _configuration["SMTP_Password"]); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = basicCredential1; try { client.Send(message); return true; }catch (Exception ex) { throw ex; }}
解决方案
推荐阅读
- c# - 来自变量的字体样式
- python - FFT - 峰峰值、峰值、RMS
- swift - 在两个视图之间添加约束时要扩展哪个视图
- c# - C# ASP 通用处理程序 - 回调内部的响应问题
- javascript - 使用JQuery获取最后一个输入字段的输入值
- vba - Excel 代码中的应用程序定义或对象定义错误
- react-native - “RCTCustomWebView”的不变违规本机组件不存在
- android - 现在我想使用 ArrayAdapter 将列表视图的特定数据表单显示到另一个列表视图中
- javascript - 我如何删除我输入的最后一个元素,如果我想要所有元素?
- kotlin - How to Resolve Function Template Generics for Signal/Slot System?