在活动服务器上出现SMTP类错误
本文关键字:SMTP 错误 活动 服务器 | 更新日期: 2023-09-27 17:51:25
我有一个网站与ASP。.NET MVC后端运行。NET 3.5。在这个网站上,有一个脚本使用gmail作为邮件服务发送电子邮件。该脚本在我的开发机器上运行并在本地发送邮件,但是一旦我将其上传到活动服务器,它就会失败。目前它给我的唯一错误信息是(因为我告诉过它,你将在下面看到):
The transport failed to connect to the server.
以下是邮件发送脚本的代码:
using System.Net.Mail;
using System.Web.Mail;
using MailMessage = System.Web.Mail.MailMessage;
namespace MySite.Helpers
{
public class GmailHelper
{
private readonly int _port = 465;
private readonly string _accountName;
private readonly string _password;
public GmailHelper(string accountName, string password)
{
_accountName = accountName;
_password = password;
}
public GmailHelper(string accountName, string password, int port)
{
_accountName = accountName;
_password = password;
_port = port;
}
public void Send(string from, string to, string subject, string body, bool isHtml)
{
Send(from, to, subject, body, isHtml, null);
}
public void Send(string from, string to, string subject, string body, bool isHtml, string[] attachments)
{
var mailMessage = new MailMessage
{
From = from,
To = to,
Subject = subject,
Body = body,
BodyFormat = isHtml ? MailFormat.Html : MailFormat.Text
};
// Add attachments
if (attachments != null)
{
foreach (var t in attachments)
{
mailMessage.Attachments.Add(new Attachment(t));
}
}
// Authenticate
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
// Username for gmail - email@domain.com for email for Google Apps
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _accountName);
// Password for gmail account
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _password);
// Google says to use 465 or 587. I don't get an answer on 587 and 465 works - YMMV
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", _port.ToString());
// STARTTLS
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);
// assign outgoing gmail server
SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(mailMessage);
}
}
}
它的名字是这样的:
[HttpPost]
public ActionResult Employment(EmploymentModel model, FormCollection collection)
{
if (!ModelState.IsValid)
return View(model);
var results = new EmploymentViewModel();
try
{
results.Position = model.Position;
results.FirstName = model.FirstName;
results.LastName = model.LastName;
results.ContactPhone = model.ContactPhone;
results.OtherPhone = model.OtherPhone;
results.Address = model.Address;
results.Email = model.Email;
results.HsDiploma = model.HsDiploma.ToString();
results.CollegeYears = model.CollegeYears;
results.Skills = model.Skills;
results.Employment = model.Employment;
results.DateSent = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
var gmail = new GmailHelper("noreply@mysite.com", "[*removed*]");
var fromAddress = new MailAddress("noreply@mysite.com", "MySite");
var toAddress = new MailAddress("applications@mysite.com", "MySite Employment");
var subject = string.Format("Employment Application for {0} {1}", model.FirstName, model.LastName);
gmail.Send(fromAddress.Address, toAddress.Address, subject, EmployMailBody(results), false);
}
catch (Exception ex)
{
results.Sent = false;
results.Title = "Oops, our mail drone seems to have malfunctioned!";
results.Message = string.Format("We appologize, {0} {1}, but our email system has encountered an error and your email was not sent.</p>",
results.FirstName, results.LastName);
results.Message += Environment.NewLine + "<p>Please try your request later, or fax your résumé to our Corporate office.";
results.Message += Environment.NewLine + JQueryHelpers.GenerateErrorField(ex.Message);
return View("EmploymentSubmit", results);
}
results.Sent = true;
results.Title = "Thank you for your submission!";
results.Message = string.Format("Thank you for your interest in joining our team, {0} {1}!</p>", results.FirstName, results.LastName);
results.Message += Environment.NewLine + "<p>We have successfully recieved your information and will contact you shortly at the number your have provided.";
return View("EmploymentSubmit", results);
}
我99%肯定它在网站上出现之前是正常的,但我可能错了,因为我已经更新了一个月左右的网站。
是否有一些额外的步骤,我可以采取调试这个"更好"跟踪潜在的问题,或者我搞砸了我的代码在某个地方不小心?
谢谢!
<标题> EDIT1 h1> 以我更新类严格使用System.Net.Mail
。我成功地从我的开发机器发送了一条消息。然而,当我将站点上传到服务器时,我得到了一个新的错误消息:
Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
这个网站是通过godaddy.com托管的,在搜索了一下之后,似乎godaddy只允许通过他们的smtp服务器中继。看起来我必须更改主机提供商才能使其正常工作。
谢谢!
<标题> EDIT2 h1> 从godaddy转移到gmail的原因是最初当我有这个脚本时,电子邮件将需要15到45分钟才能到达目的地。这可能是我之前使用的弃用代码,但无论如何,它现在都在几秒钟内被分派和到达,就像它应该的那样。这是我的GoDaddy帮助类,以防它会帮助别人:public class GoDaddyHelperNet
{
private readonly int _port = 25;
private readonly MailAddress _accountName;
private readonly string _password;
private readonly string _host = "relay-hosting.secureserver.net";
public GoDaddyHelperNet(MailAddress accountName, string password)
{
_accountName = accountName;
_password = password;
}
public GoDaddyHelperNet(MailAddress accountName, string password, int port)
{
_accountName = accountName;
_password = password;
_port = port;
}
public GoDaddyHelperNet(MailAddress accountName, string password, int port, string host)
{
_accountName = accountName;
_password = password;
_port = port;
_host = host;
}
public void Send(MailAddress to, string subject, string body, bool isHtml)
{
Send(_accountName, to, subject, body, isHtml);
}
public void Send(MailAddress from, MailAddress to, string subject, string body, bool isHtml)
{
var smtp = new SmtpClient
{
Host = _host,
Port = _port,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(from.Address, _password),
Timeout = 15000
};
using (var message = new System.Net.Mail.MailMessage(from.Address, to.Address)
{
Subject = subject,
Body = body,
})
smtp.Send(message);
}
}
标题>标题>
可能是防火墙规则或其他网络设备阻止了您。验证您可以从您的web服务器Telnet到gmail服务器,并可以发送电子邮件。
http://www.wikihow.com/Send-Email-Using-Telnet如果你不能,你可能需要和你的网络管理员谈谈。我有大约一半客户的网络服务器都有这样的问题
Gmail对SMTP中继有很多限制。每天不超过100人,IIRC。对每条消息的收件人也有限制。限制邮件/附件的大小。如果你超过了限制或者Gmail已经决定你看起来像一个垃圾邮件艺人,他们的SMTP服务器很可能会拒绝连接一天左右。