Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用sendgrid(C#)发送电子邮件,并且没有异常跟踪_C#_Email_Sendgrid - Fatal编程技术网

无法使用sendgrid(C#)发送电子邮件,并且没有异常跟踪

无法使用sendgrid(C#)发送电子邮件,并且没有异常跟踪,c#,email,sendgrid,C#,Email,Sendgrid,我在代码中使用SendAsync方法来触发电子邮件。电子邮件代码打印在下面 public async Task SendAsync(IdentityMessage message) { if (message.Destination != null) await configSendGridasync(message); } private async Task configSendGridasync(IdentityMessage message) { var myMessage =

我在代码中使用
SendAsync
方法来触发电子邮件。电子邮件代码打印在下面

public async Task SendAsync(IdentityMessage message) {
 if (message.Destination != null)
  await configSendGridasync(message);
}
private async Task configSendGridasync(IdentityMessage message) {
 var myMessage = new SendGridMessage();
 myMessage.AddTo(message.Destination);
 myMessage.From = new MailAddress(ConfigurationManager.AppSettings["mailAccount"], "XYC Social");
 myMessage.Subject = message.Subject;
 myMessage.Text = message.Body;
 myMessage.Html = message.Body;

 var credentials = new NetworkCredential(
  ConfigurationManager.AppSettings["mailAccount"],
  ConfigurationManager.AppSettings["mailPassword"]
 );

 // Create a Web transport for sending email.
 var transportWeb = new Web(credentials);

 // Send the email.
 if (transportWeb != null) {
  await transportWeb.DeliverAsync(myMessage);
 } else {
  Trace.TraceError("Failed to create Web transport.");
  await Task.FromResult(0);
 }
}
在控制器中,我使用以下代码触发电子邮件

public async Task < ActionResult > ForgotPassword(XYCSocial.Models.ManageUserViewModel model) {
 IdentityMessage msg = new IdentityMessage {
  " Message",
  Subject = "Reset Password",
  Destination = result.Email
 };
 try {
  await _emailService.SendAsync(msg);
 } catch (Exception ex) {
  ViewBag.Status = "Error : " + ex.Message;
  throw;
 }

 TempData["Success"] = " Some Message";
 return RedirectToAction("Login");
}
} else {
 TempData["Failed"] = "Please enter valid User name";
 return RedirectToAction("Login");
}
} catch (Exception) {


 TempData["Failed"] = "Email Send Failed..Please Try After Some Time";
 return RedirectToAction("Login");
公共异步任务放弃密码(XYCSocial.Models.ManageUserViewModel){
IdentityMessage msg=新IdentityMessage{
“信息”,
Subject=“重置密码”,
Destination=result.Email
};
试一试{
wait_emailService.SendAsync(msg);
}捕获(例外情况除外){
ViewBag.Status=“错误:”+ex.消息;
投掷;
}
TempData[“Success”]=“Some Message”;
返回重定向操作(“登录”);
}
}否则{
TempData[“失败”]=“请输入有效用户名”;
返回重定向操作(“登录”);
}
}捕获(例外){
TempData[“失败”]=“电子邮件发送失败..请稍后再试”;
返回重定向操作(“登录”);
然而,当我在控制器中调用SendAsync方法来触发电子邮件时,它只是执行而没有错误,但电子邮件从未发送

经过很长一段时间,它终于说:

电子邮件发送失败

从上面的
TempData[“Failed”]
标记中,我已验证了sendgrid帐户中的suppressions文件夹,但没有垃圾邮件、错误电子邮件地址或阻止等迹象


同样的代码一直有效到2017年2月17日,但之后就不起作用了。

使用类似的代码,我也遇到了同样的问题。今晚我在sendgrid GitHub页面上找到了一个更新的示例

这适用于Sendgrid Mail的V3版本

我可以确认,将此代码与Azure托管的MVC 5解决方案一起使用是有效的:

var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("test@example.com", "Example User");
        var subject = "Hello World from the SendGrid CSharp SDK!";
        var to = new EmailAddress("test@example.com", "Example User");
        var plainTextContent = "Hello, Email!";
        var htmlContent = "<strong>Hello, Email!</strong>";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
        var response = await client.SendEmailAsync(msg);
var apiKey=Environment.GetEnvironmentVariable(“您的发送网格密钥的环境变量的名称”);
var client=新的SendGridClient(apiKey);
var from=新电子邮件地址(“test@example.com“,”示例用户“);
var subject=“SendGrid CSharp SDK中的Hello World!”;
var to=新的电子邮件地址(“test@example.com“,”示例用户“);
var plainTextContent=“你好,电子邮件!”;
var htmlContent=“你好,电子邮件!”;
var msg=MailHelper.CreateSingleEmail(发件人、收件人、主题、明文内容、htmlContent);
var response=wait client.sendmailasync(msg);

您需要替换整个配置方法并将环境变量添加到Azure portal manager中(您需要从Sendgrid站点获取)

更新:我升级了Sendgrid的版本,该版本允许我在var transportWeb=new Web(APIKEY)中获取APIKEY;问题得到了解决。