C# - 发送邮件失败

时间:2013-03-21 06:41:53

标签: c# asp.net .net

此代码在我的本地计算机上正常工作,但在部署时。它会出现Failure sending mail错误..请帮助...

MailAddress addrsTo = new MailAddress(toEmail);
MailAddress addrsFrom = new MailAddress("XXX@XXX.com", "XXX Title");

MailMessage mailmsg = new MailMessage(addrsFrom, addrsTo);
mailmsg.Subject = mailSbjct;

mailmsg.Body = "XXX Body";

SmtpClient smtp = new SmtpClient("mail.XXX.com");
smtp.EnableSsl = false;
smtp.Port = 26;
smtp.Credentials = new NetworkCredential("XXX@XXX.com", "XXXXXXX");

try {
    smtp.Send(mailmsg);
} catch (Exception exc) {
    throw new XXXException(1234, "---" + exc.Message);

}

2 个答案:

答案 0 :(得分:7)

如果您使用gmail

,可以试试这个
 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
   smtp.Send(mail);                
  }
  catch (Exception ex)
  {               
  }

答案 1 :(得分:-1)

正如您在问题中提到的,在我的本地计算机上正常运行
这表明问题是您提供的用于发送邮件的凭证。

编辑1

如果您使用您拥有的域凭据,则无法在服务器上运行。
用户IIS应具有足够的权限来发送邮件。
IIS用户更多细节
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis

这是一个SO链接
How to send email through IIS7?