我正在尝试使用SMTP从我的网站发送电子邮件。我每次都会收到错误。我使用我的ISP smtp服务器在本地测试它,它工作得很好。现在我在网上虽然情况并非如此。我不知道我得到的错误是什么,然后它不起作用,我收到我已编入网站的错误消息。你可以在下面看到我的代码。
我尝试了几个不同的服务器而没有运气。我知道登录/密码很好,因为我验证了它。我的托管服务提供商是winhost,但我的电子邮件通过gmail。因此,我在godaddy上设置了一个允许250个继电器的帐户。
public class EmailMeController : Controller
{
//
// GET: /EmailMe/
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(EmailModel emailModel)
{
if (ModelState.IsValid)
{
bool isOk = false;
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("no-reply@openskymedia.com", "Website Contact Form");
msg.To.Add("admin@openskymedia.com");
msg.Subject = emailModel.Subject;
string body = "Name: " + emailModel.Name + "\n"
+ "Email: " + emailModel.EmailAddress + "\n"
+ "Website: " + emailModel.WebSite + "\n"
+ "Phone: " + emailModel.Phone + "\n\n"
+ emailModel.Message;
msg.Body = body;
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.secureserver.net", 80);
NetworkCredential Credentials = new NetworkCredential("postmaster2@domain.com", "password");
smtp.Credentials = Credentials;
smtp.Send(msg);
msg.Dispose();
isOk = true;
MessageModel rcpt = new MessageModel();
rcpt.Title = "Thank You";
rcpt.Content = "Your email has been sent.";
return View("Message", rcpt);
}
catch (Exception ex)
{
}
// If we are here...something kicked us into the exception.
//
MessageModel err = new MessageModel();
err.Title = "Email Error";
err.Content = "The website is having an issue with sending email at this time. Sorry for the inconvenience. My email address is provided on the about page.";
return View("Message", err);
}
else
{
return View();
}
}
}
答案 0 :(得分:2)
您正在吞咽您的例外情况。在这个catch块中:
61. catch (Exception ex)
62. {
63. }
只写出错误:
Response.Write( ex.ToString() )
此外,请务必遍历任何内部异常,例如:
while( ex != null ){
Response.Write( "<HR>" + ex.ToString() );
ex = ex.InnerException;
}
答案 1 :(得分:1)
问题的解决方案就是这样。我最终使用winhosts SMTP。我托管的电子邮件托管在谷歌上。我无法发送到与我的托管公司属于同一域的电子邮件地址,因为它首先查找了该电子邮件地址。当它没有找到它时,它会出错。相反,我必须将电子邮件发送到我的Gmail帐户,然后根据回复地址和电子邮件中的某些文本转发到我的openskymedia地址。这有点令人费解,但它确实奏效了。下面的代码能够让我从表单发送电子邮件并且运行得非常好。
public ActionResult Index(EmailModel emailModel)
{
if (ModelState.IsValid)
{
bool isOk = false;
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("no-reply@openskymedia.com", "Website Contact Form");
msg.To.Add("jasshultz@gmail.com");
msg.Subject = emailModel.Subject;
string body = "Name: " + emailModel.Name + "\n"
+ "Email: " + emailModel.EmailAddress + "\n"
+ "Website: " + emailModel.WebSite + "\n"
+ "Phone: " + emailModel.Phone + "\n\n"
+ emailModel.Message;
msg.Body = body;
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("your.server.com");
NetworkCredential Credentials = new NetworkCredential("email@domain.com", "password");
smtp.Credentials = Credentials;
smtp.Send(msg);
msg.Dispose();
isOk = true;
MessageModel rcpt = new MessageModel();
rcpt.Title = "Thank You";
rcpt.Content = "Your email has been sent.";
return View("Message", rcpt);
}
catch (Exception ex)
{
//while (ex != null)
//{
// Response.Write("<HR>" + ex.ToString());
// ex = ex.InnerException;
//}
}
// If we are here...something kicked us into the exception.
//
MessageModel err = new MessageModel();
err.Title = "Email Error";
err.Content = "The website is having an issue with sending email at this time. Sorry for the inconvenience. My email address is provided on the about page.";
return View("Message", err);
}
else
{
return View();
}
}
答案 2 :(得分:0)
除了做Dave告诉你的事情,你是否尝试使用本地iis作为smtp服务器?您可以在web.net部分的web.config文件中执行此操作。
<system.net>
<mailSettings>
<smtp from="username@domain">
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>