有关使用MVC发送电子邮件的问题 [英] Problem about sending email with MVC
问题描述
在localhost中它工作正常,
在实时它提供错误:
错误消息:
请求类型'System.Net.Mail.SmtpPermission,System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的权限失败
帮帮我,我的代码是:
公共ActionResult电邮(RegInfo注册)
{
try
{
var from = System.Configuration.ConfigurationManager.AppSettings [UserName];
var smtpHost = System.Configuration .ConfigurationManager.AppSettings [Host];
var mailPwd = System.Configuration.ConfigurationManager.AppSettings [MailPwd];
var smtpPort = Convert.ToInt32(System .Configuration.ConfigurationManager.AppSettings [Port]);
var callBackUrl = System.Configuration.ConfigurationManager.AppSettings [CallBackUrl] + registration.ROLEBYID;
varmsg = new MailMessage(from,registration.EMAIL){Subject =Verify your Account};
var Body = string.Format(嗨{0}
您的帐户已注册。
您的ID为:{1}
请点击< a href = \{2} \> here,registration.NAME,registration.ROLEBYID,callBackUrl)确认您的帐户;
msg.Body = Body;
msg.IsBodyHtml = true;
var smtp = new SmtpClient()
{
主机= smtpHost,
端口= smtpPort,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(from,mailPwd),EnableSsl = true
};
smtp.Send(msg);
返回RedirectToAction(创建);
}
catch(例外情况)
{
TempData [ error] = ex.Message;
返回RedirectToAction(创建);
}
}
我尝试了什么:
将ssl更改为false,但不能正常工作
In localhost it works fine,
In live it provide Error:
Error msg:
Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed
help me my code is:
public ActionResult Email(RegInfo registration)
{
try
{
var from = System.Configuration.ConfigurationManager.AppSettings["UserName"];
var smtpHost = System.Configuration.ConfigurationManager.AppSettings["Host"];
var mailPwd = System.Configuration.ConfigurationManager.AppSettings["MailPwd"];
var smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
var callBackUrl = System.Configuration.ConfigurationManager.AppSettings["CallBackUrl"] + registration.ROLEBYID;
var msg = new MailMessage(from, registration.EMAIL) { Subject = "Verify Your Account" };
var Body = string.Format("Hi {0}
Your Account Registerd.
Your Id is :{1}
Please confirm your account by clicking <a href=\"{2}\">here", registration.NAME, registration.ROLEBYID, callBackUrl);
msg.Body = Body;
msg.IsBodyHtml = true;
var smtp = new SmtpClient()
{
Host = smtpHost,
Port = smtpPort,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(from, mailPwd), EnableSsl = true
};
smtp.Send(msg);
return RedirectToAction("Create");
}
catch (Exception ex)
{
TempData["error"] = ex.Message;
return RedirectToAction("Create");
}
}
What I have tried:
Changed ssl to false , but not working
推荐答案
有几件事可以导致这种情况。这是第一个谷歌搜索结果: c# - SecurityException:请求System.Net.Mail.SmtpPermission类型的权限 - Stack Overflow [ ^ ]。
There are several things that can cause this. This is the first google search result: c# - SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission' - Stack Overflow[^].
这篇关于有关使用MVC发送电子邮件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!