如果我使用这种邮件方法,则会收到以下错误消息:
处理您的请求时发生错误
这是我在控制器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);
在mailer方法中,然后可以进行注册提交,但是mailer不会继续。
实际上,我是asp.net mvc5的新手,但我声明的邮件路径与Web表单相同。请帮助我解决此邮件问题。而且我没有发布我的mvc5项目,而没有发布直接将所有文件直接上传到实时状态。
答案 0 :(得分:0)
您可以像下面这样构造SmtpClient:
SmtpClient client = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("fromAddress", "fromPassword"),
Timeout = 20000
};
替换发件人地址和密码。
注意:请不要忘记在Google的帐户安全设置中启用“允许安全程度较低的应用” 。