我有一个运行.NET 3.5的ASP.NET MVC后端的网站。在这个网站上,有一个脚本使用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%肯定它在它上网之前它是有用的,但我可能会错误,因为我已经有一个月左右,因为我不得不更新网站。
我是否可以采取一些额外步骤来调试“更好”以追踪潜在问题,或者我是否意外地将代码搞砸了?
谢谢!
所以我更新了类以严格使用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服务器进行中继。看起来我必须更改主机提供商才能使其正常工作。
谢谢!
我从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);
}
}
答案 0 :(得分:0)
可能存在阻止您的防火墙规则或其他网络设备。验证您是否可以从Web服务器远程登录到gmail服务器并发送电子邮件。
http://www.wikihow.com/Send-Email-Using-Telnet
如果你不能,你可能不得不和网络管理员交谈。我在大约一半的客户端Web服务器上都有这样的电子邮件问题。
答案 1 :(得分:0)
Gmail对SMTP中继有很多限制。 IIRC每天不超过100名受助人。还限制每封邮件的收件人。消息/附件大小限制。如果您超出限制或Gmail认定您看起来像垃圾邮件 artiste ,则他们的SMTP服务器可能会在一天左右拒绝连接。