0

I'm trying to create an email in the drafts folder on Outlook/Office 365. The problem is that the server keeps resetting the connection. Sometimes the code works, other times it causes an exception with either the message "10054:Connection reset by peer" or "10053:Software caused connection abort".

The errors seem to be dependent on the size of the attachment. Larger than 500k is almost always an error, Less than 100k is usually fine. A file of approx 500k is about 50:50 whether it will succeed or not. I'm fairly sure it's not the content of the attachment as the same file will sometimes work, sometimes fail, but its size dictates the likelihood of failure. If it succeeds, then it all looks fine on the server.

The code seems to work fine on gmail, it just has problems on outlook/office 365 (& hotmail). I'm using the latest openssl libs from Fulgan.

Can anyone advise?

bool SendToOutlook365Drafts()
{
    TIdMessage *Message = new TIdMessage(NULL);

    TIdMessageBuilderHtml *Builder = new TIdMessageBuilderHtml;
    Builder->PlainText->Text = "plain text goes here";
    Builder->Html->Text = "HTML goes here";
    Builder->Attachments->Add("S:\\test.zip");
    Builder->FillMessage(Message);
    Message->Flags << mfDraft;
    Message->From->Text = IMAPUsername;

    TIdSSLIOHandlerSocketOpenSSL *SSLHandler = new TIdSSLIOHandlerSocketOpenSSL(NULL);
    SSLHandler->SSLOptions->Method = sslvTLSv1_2;
    SSLHandler->SSLOptions->SSLVersions << sslvTLSv1_2;
    SSLHandler->SSLOptions->SSLVersions << sslvTLSv1_1;
    SSLHandler->SSLOptions->SSLVersions << sslvTLSv1; // Allow all TLS methods
    SSLHandler->SSLOptions->SSLVersions << sslvSSLv3;
    SSLHandler->SSLOptions->SSLVersions << sslvSSLv23;
    SSLHandler->SSLOptions->SSLVersions << sslvSSLv2; // Allow all SSL methods
    SSLHandler->SSLOptions->Mode = sslmUnassigned;

    TIdIMAP4 *IMAPClient = new TIdIMAP4(NULL);
    IMAPClient->IOHandler = SSLHandler;
    IMAPClient->Host = IMAPAddress;                  // outlook.office365.com
    IMAPClient->Port = IMAPPort;                     // 993
    IMAPClient->Username = IMAPUsername;
    IMAPClient->Password = IMAPPassword;
    IMAPClient->UseTLS = utUseImplicitTLS;

    try{
        IMAPClient->Connect(10000);

        IMAPClient->AppendMsg("Drafts", Message, Message->Flags);   // exception thrown here. Either "10054:Connection reset by peer" or "10053:Software caused connection abort"
        IMAPClient->Disconnect();
        delete IMAPClient;
        delete SSLHandler;
        delete Message;
        return true;
    }
    catch(Exception &exception)
    {
        IMAPClient->Disconnect();
        ErrorDialog(exception.Message);
        delete IMAPClient;
        delete SSLHandler;
        delete Message;
        return false;
    }
}

Edit: Output from IMAP log below:-

Stat Connected.
Recv 02/10/2019 20:25:24: * OK The Microsoft Exchange IMAP4 service is ready. [TABOAFgAUAAyADYANQBDAEEAMAAwADkAMwAuAEcAQgBSAFAAMgA2ADUALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]<EOL>
Sent 02/10/2019 20:25:24: C1 LOGIN des@+++++.onmicrosoft.com "+++++++++"<EOL>
Recv 02/10/2019 20:25:24: C1 OK LOGIN completed.<EOL>
Sent 02/10/2019 20:25:24: C2 CAPABILITY<EOL>
Recv 02/10/2019 20:25:24: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+<EOL>C2 OK CAPABILITY completed.<EOL>
Sent 02/10/2019 20:25:25: C3 APPEND "Drafts" (\Draft) {816681+}<EOL>
Sent 02/10/2019 20:25:25: From: "des@+++++.onmicrosoft.com" <des@+++++.onmicrosoft.com><EOL>Content-Type: multipart/mixed; boundary="AB7sooOH0c7ZwPjBXeilIv=_zBKjZ16keW"<EOL>MIME-Version: 1.0<EOL>Date: Wed, 2 Oct 2019 20:25:24 +0100<EOL><EOL>
Sent 02/10/2019 20:25:25: This is a multi-part message in MIME format<EOL><EOL>--AB7sooOH0c7ZwPjBXeilIv=_zBKjZ16keW<EOL>Content-Type: multipart/alternative;<EOL>   boundary="UvatywYlNKXglQCy=_QgJ3A4j1k29J4UFf"<EOL><EOL>--UvatywYlNKXglQCy=_QgJ3A4j1k29J4UFf<EOL>Content-Type: text/plain<EOL>Content-Transfer-Encoding: quoted-printable<EOL>Content-Disposition: inline<EOL><EOL>plain text goes here<EOL><EOL>--UvatywYlNKXglQCy=_QgJ3A4j1k29J4UFf<EOL>Content-Type: text/html<EOL>Content-Transfer-Encoding: quoted-printable<EOL>Content-Disposition: inline<EOL><EOL>HTML goes here<EOL><EOL>--UvatywYlNKXglQCy=_QgJ3A4j1k29J4UFf--<EOL><EOL>--AB7sooOH0c7ZwPjBXeilIv=_zBKjZ16keW<EOL>Content-Type: application/zip;<EOL>  name="test.zip"<EOL>Content-Transfer-Encoding: base64<EOL>Content-Disposition: attachment;<EOL> filename="test.zip"<EOL><EOL>LyoqKioqKioqKioqKioqKioqICBTVEFOREFSRCBGSUxFIEhFQURFUiAtIFNUQVJUICoqKioqKioq<EOL>KioqKioqKioqKioqKioqKi8NCi8vI3ByYWdtYSBoZHJmaWxlICJ3aW5wcm9wWC5jc20iDQojaW5j<EOL>bHVkZSAic3RkX2hlYWQuaW5jIg0KI3By
--- binary data edited out here, but is incomplete ----
Stat Disconnected.
Stat Disconnected.
Stat Disconnected.
Des10101
  • 1
  • 1
  • 1
    Hard to say for sure what is happening without knowing which versions of C++Builder and Indy you are using, or seeing the actual IMAP communications to see if the `APPEND` command is malformatted. But one thing I do notice is your use of the `<<` operator to assign the `Flags` and `SSLVersions` properties WILL NOT work as expected. It will only call the property getters, not the setters. You need to set the `Flags` like this instead: `Message->Flags = TIdMessageFlagsSet() << mfDraft;` or `Message->Flags = Message->Flags << mfDraft;`, and same with `SSLVersions`. – Remy Lebeau Oct 02 '19 at 16:45
  • Hi Remy. Thanks for the code corrections, sadly they don't make a difference. I'm running C++Builder V6 with Indy 10.6.2.0. How do I go about analysing the IMAP communication? I tried analysing with WireShark but wasn't sure what I was looking for. I could see that TLSv1.2 was used and the last comms was a string of Frame Check Sequence errors. – Des10101 Oct 02 '19 at 19:01
  • 1
    Since TLS is involved, Wireshark won't be of any help since the comms are encrypted. But you can attach one of Indy's `TIdLog...` components to the `TIdIMAP4::Intercept` property to log the un-encrypted IMAP commands/responses. Also, you might also consider upgrading your copy of Indy to the current version (10.6.2.5518) to make sure you have all the latest functionality and bug fixes. – Remy Lebeau Oct 02 '19 at 19:04
  • Hi Remy, I'm not sure if the above sections of logs are readable or useful, but there didn't seem to be much difference between a good send and a bad one (apart from length - the bad send just stops midway through the binary transmission). The only other notable difference was to do with values for boundary fields. I'll update Indy next, thanks for your help so far. – Des10101 Oct 02 '19 at 20:12
  • You should have posted that info as an [edit] to your original question instead of in comments. – Remy Lebeau Oct 02 '19 at 20:58
  • Ah, that makes sense. I've edited the log to remove the majority of the binary data but it basically stops transmitting the binary at random points when the disconnect occurs. – Des10101 Oct 02 '19 at 21:07
  • Off-hand, I don't see anything in that log that should be causing a connection failure on either end. Unless maybe the server imposes a max email size on the mailbox, but it is not advertising an `APPENDLIMIT` value in the `CAPABILITY` response. – Remy Lebeau Oct 02 '19 at 21:15
  • 1
    Internally, `TIdIMAP4.AppendMsg()` uses the [`LITERAL+`](https://tools.ietf.org/html/rfc7888) IMAP extension if the server advertises support for it (which Office365 does). That allows `AppendMsg()` to send the whole email directly in the `APPEND` command without wasting a round-trip asking for permission to send the whole email. One thing you could try doing is edit the source code for `AppendMsg()` to force its `LUseNonSyncLiteral` local variable to always be False, and see if that helps the issue. That will at least allow the server to validate the email size up front before accepting it – Remy Lebeau Oct 02 '19 at 21:43
  • @RemyLebeau And as if by magic, it all now works flawlessly (without making further changes). So I guess it must have been a problem on Microsoft's end. Many thanks for all your help. – Des10101 Oct 07 '19 at 15:42

0 Answers0