[英]ASP.NET MVC SMTPClient not working on hosted environment
When I run my application locally on localhost, the e-mail is sent out successfully with no exceptions. 当我在本地主机上本地运行我的应用程序时,该电子邮件将毫无例外地成功发送出去。 But when I publish the application and try to re-create the e-mail it does not send the e-mail nor sends the View with the corresponding error. 但是,当我发布应用程序并尝试重新创建电子邮件时,它既不发送电子邮件也不发送带有相应错误的View。
try
{
using (var mail = new MailMessage())
{
const string email = "*******@gmail.com";
const string password = "*********";
var loginInfo = new NetworkCredential(email, password);
mail.From = new MailAddress("*******@gmail.com");
mail.To.Add(new MailAddress("email@email.com"));
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
try
{
using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(mail);
}
}
finally
{
mail.Dispose();
}
}
}
catch (SmtpFailedRecipientsException ex)
{
foreach (SmtpFailedRecipientException t in ex.InnerExceptions)
{
var status = t.StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
return View(status);
}
else
{
return View(status);
}
}
}
catch (SmtpException Se)
{
// handle exception here
return View(Se);
}
catch (Exception ex)
{
return View(ex);
}
The error message is not displayed here, however, it is possible that is a client authentication error. 错误消息未在此处显示,但是,可能是客户端身份验证错误。 Please try: 请试试:
Another thing to note, you do not need to try .. finally on your MailMessage() object. 要注意的另一件事是,您最终不必在MailMessage()对象上尝试..。 Since it is already wrapped in a using statement, when it reaches the end of the using block is will dispose it for you and any errors will get caught in your outer try catch block. 由于它已经被包装在using语句中,当它到达using块的末尾时,它将为您处理它,并且任何错误都将被捕获在您的外部try catch块中。
Remove all that exception handling, and try again. 删除所有这些异常处理,然后重试。 Find out what the error is, then go from there. 找出错误所在,然后从那里继续。
I suspect the reason youre having a hard time is b/c of the way you have your try/catch's set up. 我怀疑您遇到困难的原因是尝试/捕获的建立方式中的b / c。 The inner try/catch strikes me as being a little odd. 内在的尝试/追赶使我感到有点奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.