简体   繁体   English

C# - 发送邮件失败

[英]C# - Failure sending mail

This code works fine on my local machine ut when I deploy it. 当我部署它时,此代码在我的本地计算机上工作正常。 it gives Failure sending mail error.. Please Help... 它给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);

}

you can try this, if you are using gmail : 你可以试试这个,如果你使用的是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)
  {               
  }

As you have mentioned in your question works fine on my local machine . 正如您在问题中提到的, 在我的本地计算机上工作正常
It is suggesting that the problem is of credential which you are providing to send the mail. 这表明问题是您提供的用于发送邮件的凭证。

Edit 1 编辑1

If you are using you own domain credential then it is not going to work on the server. 如果您使用自己的域凭据,那么它将无法在服务器上运行。
The user IIS should have enough authority to send mail. 用户IIS应具有足够的权限来发送邮件。
IIS User More detail IIS用户更多细节
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis

Here is a SO link 这是一个SO链接
How to send email through IIS7? 如何通过IIS7发送电子邮件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM