c# - 邮件程序问题:处理您的请求时发生错误。ASP.NET MVC 5
问题描述
如果我使用这个邮件方法,我会得到如下所示的错误:
处理您的请求时发生错误
这是我在控制器 ActionResult 中使用的邮件方法。
private void SendActivationEmail(string activationCodes, string emailid, string Fullname)
{
MailAddress from = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["AdminFromEmailAddress"].ToString());
MailAddress to = new MailAddress(emailid);
MailMessage message = new MailMessage(from, to);
string LinkPath = System.Configuration.ConfigurationManager.AppSettings["verifyuserpath"].ToString();
message.IsBodyHtml = true;
string strBody;
int a = 0;
strBody = "";
strBody = strBody + "Dear" + " " + Fullname + "," + "<br><br>";
strBody = strBody + "Thank you for your interest in the testing Program." + "<br><br>";
strBody = strBody + "Please click <a href=" + LinkPath + "Registers/IAActivation?emailId=" + emailid + "&uCode=" + activationCodes + "&type=" + a + ">here</a> to finish setting up this testing account, we just need to make sure this email address is yours or paste the following link into your browse" + "<br><br>";
strBody = strBody + "<a href=" + LinkPath + "Registers/IAActivation?emailId=" + emailid + "&uCode=" + activationCodes + "&type=" + a + ">" + LinkPath + "Registers/IAActivation?emailId=" + emailid + "&uCode=" + activationCodes + "&type=" + a + "" + "</a><br><br>";
strBody = strBody + "<br>With Regards" + "," + "<br>";
strBody = strBody + "testing Team";
message.Subject = "Verification Mail";
message.Body = strBody;
SmtpClient client = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["SmtpClientclientPath"].ToString());
client.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientclientPathPort"].ToString());
client.Send(message);
}
这是我从 web.config 邮件程序路径调用的应用程序设置。
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="AdminFromEmailAddress" value="info@xyz.in" />
<add key="SmtpClientclientPath" value="000.0.0.1" />
<add key="SmtpClientclientPathPort" value="25" />
<!-- Your SMTP Server IP -->
<add key="gmailHost" value="smtp.gmail.com" />
<add key="userName" value="techhelp@xyz.in" />
<add key="password" value="tech@123" />
<add key="gmailport" value="587" />
<add key="verifyuserpath" value="http://xyz.in/" />
</appSettings>
如果我注释掉这一行
client.Send(message);
在邮件方法中,然后注册提交工作但邮件不去。
实际上,我是 asp.net mvc5 的新手,但我声明的邮件路径与 Web 表单一样。请帮我解决这个邮件问题。而且我没有发布我的mvc5项目直接将所有文件上传到live没有发布。
解决方案
您可以构建您的 SmtpClient 如下所示:
SmtpClient client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("fromAddress", "fromPassword"),
Timeout = 20000
};
替换 FromAddress 和密码。
注意:不要忘记在您的 Google 帐户安全设置中启用“允许不太安全的应用程序” 。
推荐阅读
- c++ - Pre C++17 中模板参数包的“折叠”:惯用方法
- typescript - 上传到 graphql 的文件会自动解析,除非它们在对象内。这是错误还是预期行为?
- html - 响应式设计和网格:我应该把导航栏放在哪里?
- android - 设备可能具有与当前版本不匹配的陈旧 dexed jar(dexopt 错误)
- ios - XCUITest XCUIElementQuery 模态视图中缺少所有非静态文本元素
- ios - QLPreviewController 检测 QLPreviewItem 何时更改
- jquery - 如何使用jquery动态更改以禁用切换器
- typescript - 在 TS 中处理事件 - 无过载
- javascript - 获取用户的身份验证状态返回意外结果
- apache-spark - 如何处理超长线的火花任务?