问题描述
大家好,
Hi All,
我们有一个Windows服务,它基本上是在Active Directory中创建用户并自动向他们发送邮件。但有时它无法发送邮件。
We have a windows service which is basically creating users in Active Directory and send mail to them automatically. But some times it fails to send the mail.
错误详细信息: -
Error Details:-
消息:服务不可用,关闭传输通道。服务器响应为:#4.x.2此会话的消息太多
类型:System.Net.Mail.SmtpException
方法:CheckResponse
行:0
$
栏目:0
Stacktrace: 在System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response)
在System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,收件人MailAddressCollection,字符串deliveryNotify,SmtpFailedRecipientException&安培;例外)
  在System.Net.Mail.SmtpClient.Send(MailMessage消息)
在Damco.Portal.Users.Utilities.SendEmail(字符串SMTPSERVER,的Int32 SMTPPORT,字符串toEmail,字符串对象,字符串体,字符串fromEmail)
   at Damco.Portal.Users.Utilities.SendNewUserEmail(String smtpServer,Int32 smtpPort,User user,String password,String fromEmail)
在Damco.Portal.Users.Utilities.CreateUser(字符串ldapConnectionString,字符串ldapAdminUser,字符串ldapAdminPassword,字符串SMTPSERVER,的Int32 SMTPPORT,用户用户,字符串fromEmail)
   在UserSyncService.Service.SyncAllUsersFromPatToAdam(字符串ldapConnectionString,字符串ldapAdminUser,字符串ldapAdminPassword,字符串SMTPSERVER,的Int32 SMTPPORT,字符串fromEmail)
Message: Service not available, closing transmission channel. The server response was: #4.x.2 Too many messages for this session
Type: System.Net.Mail.SmtpException
Method: CheckResponse
Line: 0
Column: 0
Stacktrace: at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Damco.Portal.Users.Utilities.SendEmail(String smtpServer, Int32 smtpPort, String toEmail, String subject, String body, String fromEmail)
at Damco.Portal.Users.Utilities.SendNewUserEmail(String smtpServer, Int32 smtpPort, User user, String password, String fromEmail)
at Damco.Portal.Users.Utilities.CreateUser(String ldapConnectionString, String ldapAdminUser, String ldapAdminPassword, String smtpServer, Int32 smtpPort, User user, String fromEmail)
at UserSyncService.Service.SyncAllUsersFromPatToAdam(String ldapConnectionString, String ldapAdminUser, String ldapAdminPassword, String smtpServer, Int32 smtpPort, String fromEmail)
 
在邮件发送代码: -
The Mail sending code:-
方法名称:private static void SendEmail(string smtpServer,int smtpPort,string toEmail,string subject,string body,string fromEmail)
$
{
MAILMESSAGE MAILMESSAGE =新MAILMESSAGE {从=新MailAddress(fromEmail)};
         &NBSP ;  mailMessage.To.Add(new MailAddress(toEmail));
mailMessage.Subject = subject;
mailMessage.Body = body;
//通过smtp服务器发送消息
SmtpClient客户端=新的SmtpClient(smtpServer,smtpPort);
client.Send(mailMessage);
}
Method Name : private static void SendEmail(string smtpServer, int smtpPort, string toEmail, string subject, string body, string fromEmail)
{
MailMessage mailMessage = new MailMessage {From = new MailAddress(fromEmail)};
mailMessage.To.Add(new MailAddress(toEmail));
mailMessage.Subject =subject;
mailMessage.Body = body;
// send the message via the smtp server
SmtpClient client = new SmtpClient(smtpServer, smtpPort);
client.Send(mailMessage);
}
有什么建议吗?
推荐答案
确保在SmtpClient上调用Dispose(或将其放在using语句块中)。 否则会耗尽资源,不会释放TCP连接和其他资源。 这可能就是为什么它会为会话提供太多消息
的错误。
Make sure to call Dispose on the SmtpClient (or put it in a using statement block). Otherwise it will use up resources and won't release the TCP connection and other resources. That's probably why it's giving the error about too many messages for the session.
这篇关于邮件发送问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!