@@ -1213,16 +1213,23 @@ public System.IO.MemoryStream EndUidMessageStream(IAsyncResult result)
12131213 /// <param name="messageOrdinal">The ordinal position of the message to be fetched.</param>
12141214 /// <returns>The message's data as a string.</returns>
12151215 /// <example><see cref="Fetch.MessageObject"/></example>
1216- public string MessageString ( int messageOrdinal )
1217- {
1216+ public string MessageString ( int messageOrdinal ) {
12181217 this . ParentMailbox . SourceClient . SelectMailbox ( this . ParentMailbox . Name ) ;
1219- this . ParentMailbox . SourceClient . OnMessageRetrieving ( new ActiveUp . Net . Mail . MessageRetrievingEventArgs ( messageOrdinal ) ) ;
1218+ this . ParentMailbox . SourceClient . OnMessageRetrieving ( new ActiveUp . Net . Mail . MessageRetrievingEventArgs ( messageOrdinal ) ) ;
12201219 string response = this . ParentMailbox . SourceClient . Command ( "fetch " + messageOrdinal . ToString ( ) + " rfc822" , getFetchOptions ( ) ) ;
12211220 ActiveUp . Net . Mail . Logger . AddEntry ( response ) ;
1222- string message = response . Substring ( response . IndexOf ( "}" ) + 3 , response . LastIndexOf ( ")" ) - response . IndexOf ( "}" ) - 3 ) ;
1223- this . ParentMailbox . SourceClient . OnMessageRetrieved ( new ActiveUp . Net . Mail . MessageRetrievedEventArgs ( System . Text . Encoding . UTF8 . GetBytes ( message ) , messageOrdinal ) ) ;
1221+
1222+ int messageSize = Convert . ToInt32 ( response . Substring ( response . IndexOf ( "{" ) + 1 , response . IndexOf ( "}" ) - response . IndexOf ( "{" ) - 1 ) ) ;
1223+ int messageStart = response . IndexOf ( "}" ) + 3 ;
1224+ string message = response . Substring ( messageStart , messageSize ) ;
1225+ // Some (older) MS Exchange versions return a smaller message size, use old version as fallback if last character is not new line or the next line starts not with FLAGS
1226+ if ( message . Substring ( message . Length - 2 , 2 ) != "\r \n " || ! response . Substring ( messageStart + messageSize ) . Trim ( ) . ToUpper ( ) . StartsWith ( "FLAGS" ) ) {
1227+ message = response . Substring ( messageStart , response . LastIndexOf ( ")" ) - messageStart ) ;
1228+ }
1229+
1230+ this . ParentMailbox . SourceClient . OnMessageRetrieved ( new ActiveUp . Net . Mail . MessageRetrievedEventArgs ( System . Text . Encoding . UTF8 . GetBytes ( message ) , messageOrdinal ) ) ;
12241231 return message ;
1225- }
1232+ }
12261233
12271234 private delegate string DelegateMessageString ( int messageOrdinal ) ;
12281235 private DelegateMessageString _delegateMessageString ;
@@ -1403,17 +1410,26 @@ public System.IO.MemoryStream EndUidMessageStreamPeek(IAsyncResult result)
14031410 /// <param name="messageOrdinal">The ordinal position of the message to be fetched.</param>
14041411 /// <returns>The message's data as a string.</returns>
14051412 /// <example><see cref="Fetch.MessageString"/></example>
1406- public string MessageStringPeek ( int messageOrdinal )
1407- {
1408- this . ParentMailbox . SourceClient . SelectMailbox ( this . ParentMailbox . Name ) ;
1409- this . ParentMailbox . SourceClient . OnMessageRetrieving ( new ActiveUp . Net . Mail . MessageRetrievingEventArgs ( messageOrdinal ) ) ;
1410- string response = "" ;
1411- if ( this . ParentMailbox . SourceClient . ServerCapabilities . IndexOf ( "IMAP4rev1" ) != - 1 ) response = this . ParentMailbox . SourceClient . Command ( "fetch " + messageOrdinal . ToString ( ) + " body[mime]" , getFetchOptions ( ) ) ;
1412- else response = this . ParentMailbox . SourceClient . Command ( "fetch " + messageOrdinal . ToString ( ) + " rfc822.peek" , getFetchOptions ( ) ) ;
1413- string message = response . Substring ( response . IndexOf ( "}" ) + 3 , response . LastIndexOf ( ")" ) - response . IndexOf ( "}" ) - 3 ) ;
1414- this . ParentMailbox . SourceClient . OnMessageRetrieved ( new ActiveUp . Net . Mail . MessageRetrievedEventArgs ( System . Text . Encoding . UTF8 . GetBytes ( message ) , messageOrdinal ) ) ;
1415- return message ;
1416- }
1413+ public string MessageStringPeek ( int messageOrdinal ) {
1414+ this . ParentMailbox . SourceClient . SelectMailbox ( this . ParentMailbox . Name ) ;
1415+ this . ParentMailbox . SourceClient . OnMessageRetrieving ( new ActiveUp . Net . Mail . MessageRetrievingEventArgs ( messageOrdinal ) ) ;
1416+ string response = "" ;
1417+ if ( this . ParentMailbox . SourceClient . ServerCapabilities . IndexOf ( "IMAP4rev1" ) != - 1 )
1418+ response = this . ParentMailbox . SourceClient . Command ( "fetch " + messageOrdinal . ToString ( ) + " body[mime]" , getFetchOptions ( ) ) ;
1419+ else
1420+ response = this . ParentMailbox . SourceClient . Command ( "fetch " + messageOrdinal . ToString ( ) + " rfc822.peek" , getFetchOptions ( ) ) ;
1421+
1422+ int messageSize = Convert . ToInt32 ( response . Substring ( response . IndexOf ( "{" ) + 1 , response . IndexOf ( "}" ) - response . IndexOf ( "{" ) - 1 ) ) ;
1423+ int messageStart = response . IndexOf ( "}" ) + 3 ;
1424+ string message = response . Substring ( messageStart , messageSize ) ;
1425+ // Some (older) MS Exchange versions return a smaller message size, use old version as fallback if last character is not new line or the next line starts not with FLAGS
1426+ if ( message . Substring ( message . Length - 2 , 2 ) != "\r \n " || ! response . Substring ( messageStart + messageSize ) . Trim ( ) . ToUpper ( ) . StartsWith ( "FLAGS" ) ) {
1427+ message = response . Substring ( messageStart , response . LastIndexOf ( ")" ) - messageStart ) ;
1428+ }
1429+
1430+ this . ParentMailbox . SourceClient . OnMessageRetrieved ( new ActiveUp . Net . Mail . MessageRetrievedEventArgs ( System . Text . Encoding . UTF8 . GetBytes ( message ) , messageOrdinal ) ) ;
1431+ return message ;
1432+ }
14171433
14181434 private delegate string DelegateMessageStringPeek ( int messageOrdinal ) ;
14191435 private DelegateMessageStringPeek _delegateMessageStringPeek ;
0 commit comments