-
-
Notifications
You must be signed in to change notification settings - Fork 805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The IMAP server has unexpectedly disconnected. Microsoft Exchange Server 2003 #1512
Comments
I'm having a similar issue. I am trying to connect to Yahoo. Windows 10/11 I placed my code in Pastebin since it was long: The settings I tried:
My Error: MailKit.Net.Smtp.SmtpProtocolException: 'The SMTP server has unexpectedly disconnected.' " at MailKit.Net.Smtp.SmtpStream.ReadAhead(CancellationToken cancellationToken)\r\n at MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken)\r\n at MailKit.Net.Smtp.SmtpStream.SendCommand(String command, CancellationToken cancellationToken)\r\n at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)\r\n at MailKit.MailService.Authenticate(Encoding encoding, String userName, String password, CancellationToken cancellationToken)\r\n at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken)\r\n at SSS.SuburbanEmail.Email.SendEmail.Send(EmailSetting emailSetting, EmailMessage emailMessage) in D:\Source\Suburban\Miscellaneous\SuburbanMail\Email\SendEmail.cs:line 156" Any suggestions? |
Port 995 is POP3, not SMTP. The SMTP ports are: 25, 465 (SslOnConnect), or 587. There's nothing MailKit can do in your situation. Your network connection was lost, either because the server disconnected your or because your router went down or something. |
@jstedfast Sorry, that was a typo but as I said in the comment, I tried various port. Not sure why 995 was there for that. I've tried various physical locations attempting to connect. I'm getting the same message regardless where I connect. I do not believe this is a network issue as I can send and receive with any other account but Yahoo. Thanks for your input! |
The settings you want for Yahoo SMTP are: host: smtp.mail.yahoo.com Based on the StackTrace, I can see that the SmtpProtocolException was thrown in SmtpStream.cs in the ReadAhead() method which only throws that exception if it reads 0 bytes from the server (a 0-byte read signifies that the other end has closed the connection). From the Socket.Receive() documentation:
|
Thanks@jstedfast |
lumisoft.net 2 from nuget |
I'm not sure how that is supposed to help, but ok. If Socket.Receive() returns 0 bytes, then it means the server has disconnected. Just because it doesn't disconnect lumisoft.net 1 time doesn't mean it's a bug in MailKit code anywhere. It just means you got lucky. |
I just want to give more information to help you, thanks for your great job! |
try {
int result = return Socket.Receive (buffer, offset, count, SocketFlags.None);
return result;
} catch (SocketException ex) {
throw new IOException (ex.Message, ex);
}
|
Thanks! |
Thanks |
The Socket is set to use Blocking IO with a ReadTimeout of 120000 milliseconds (2 minutes). What this means is that the Receive() method will not return until it has received data or until it has waited 2 minutes and still no data has been received. In the latter case, it would throw a SocketException with a SocketError value of TimedOut. What happened instead is that Receive() returned saying 0 bytes were read which means that the server closed the connection. Note: Socket.Available is also 0, meaning the server has sent no response to the NAMESPACE command other than, presumably, a FIN packet. |
Does the lumisoft.net IMAP client send the Can you get me a log from lumisoft's IMAP session that works? |
For reference, the code that calls NetworkStream.Read() (which is the method I had you set a breakpoint in) is ImapStream.ReadAhead(). If you look in that method (which is also very simple), you can see that it attempts to read into an input buffer. If any data is read, it immediately logs the received data (which is how I know that the Exchange server is not sending a response to the The line above the Stream.Read() call is: network?.Poll (SelectMode.SelectRead, cancellationToken); In your case, You can find the Poll() method toward the bottom of NetworkStream.cs on line 285. Since your code does not pass a You could try creating a using var cts = new CancellationTokenSource();
client.Authenticate(userName, password, cts.Token); |
Just to throw it out there, my issue ended up being that the user setup extra security on the account which required an App password on Yahoo. For reference: https://help.yahoo.com/kb/SLN15241.html Thanks for the help! |
@TechGuyAlabama I completely forgot that Yahoo has done that now, too. Just about all of the major free mail servers out there are now requiring OAuth and/or App-Specific Passwords for added security and will no longer accept the user's normal password. |
|
Seems that your other IMAP library doesn't send a NAMESPACE command. Maybe the Exchange server breaks when it receives that command? You'll have to edit ImapEngine.cs to make it ignore the NAMESPACE capability to work around that. |
It also doesn't seem to send the CAPABILITY command (before or after logging in) to find out what commands the IMAP server supports. |
Try editing ImapEngine.cs on line 2589 and changing the line: if ((Capabilities & ImapCapabilities.Namespace) != 0) { to: if (false && (Capabilities & ImapCapabilities.Namespace) != 0) { If this fixes the issue, I'll add a QuirksMode for Exchange 2003 to avoid using the NAMESPACE command. |
Fixed the issue! |
Ok, I'll add a work-around for Exchange 2003. |
https://help.aliyun.com/document_detail/36576.html SMTP using var smtp = new SmtpClient(); thanks, it has been resolved my issue and successfully sent mail. |
Describe the bug
Authenticate Exception
Platform (please complete the following information):
Exception
Connected to imap://...:143/?starttls=when-available
S: * OK Microsoft Exchange Server 2003 IMAP4rev1 服务器版本 6.5.7638.1 (..com) 已就绪。
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 IDLE LOGIN-REFERRALS MAILBOX-REFERRALS NAMESPACE LITERAL+ UIDPLUS CHILDREN
S: A00000000 OK CAPABILITY completed.
IMAP Connected
C: A00000001 LOGIN ******** ********
S: A00000001 OK LOGIN completed.
C: A00000002 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 IDLE LOGIN-REFERRALS MAILBOX-REFERRALS NAMESPACE LITERAL+ UIDPLUS CHILDREN
S: A00000002 OK CAPABILITY completed.
C: A00000003 NAMESPACE
IMAPMailKit.Net.Imap.ImapProtocolException: The IMAP server has unexpectedly disconnected.
at MailKit.Net.Imap.ImapStream.ReadAhead(Int32 atleast, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapStream.ReadToken(String specials, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapEngine.ReadTokenAsync(Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapCommand.StepAsync(Boolean doAsync)
at MailKit.Net.Imap.ImapEngine.IterateAsync(Boolean doAsync)
at MailKit.Net.Imap.ImapEngine.RunAsync(ImapCommand ic, Boolean doAsync)
at MailKit.Net.Imap.ImapEngine.QueryNamespacesAsync(Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapClient.OnAuthenticatedAsync(String message, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapClient.AuthenticateAsync(Encoding encoding, ICredentials credentials, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
at MailKit.MailService.Authenticate(Encoding encoding, String userName, String password, CancellationToken cancellationToken)
at MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken)
at ConsoleApp1.Program.Main(String[] args) in D:\ConsoleApp1\ConsoleApp1\Program.cs:line 77
Code Snippets
The text was updated successfully, but these errors were encountered: