Hi,
I am trying to querying attachments from a mail message using Imap. In that process, when I first try to use ImapClient.FetchMessage() method I get a bad fetch error. Can you help me in resolving this issue. Please find below sample code.
Code:
public IList<string> QueryAttachmentNames()
{
try
{
var message = this.ImapClient.FetchMessage(this.ImapMessageInfo.SequenceNumber);
var attachments = message.Attachments;
if (attachments != null)
{
return attachments.Select(x => x.Name).ToList();
}
return new List<string>();
}
catch (Exception ex)
{
throw new Exception(“Mail exception”, ex);
}
}</span></pre><pre style="font-family: Consolas; font-size: 13px; background: white;">Error:</pre><pre style="background: white;"><font face="Consolas">003 BAD FETCH not allowed now.</font></pre><pre style="background: white;"><font face="Consolas">Aspose.Email.Imap.ImapException was caught
HResult=-2146233088
Message=003 BAD FETCH not allowed now.
Source=Aspose.Email
StackTrace:
at . . (ImapReplyLineInfo[] , String )
at . . ( , Boolean , String , Int32 )
at . . (Int32 )
at Aspose.Email.Imap.ImapClient.FetchMessage(Int32 sequenceNumber, Boolean ignoreAttachment)
at Aspose.Email.Imap.ImapClient.FetchMessage(Int32 sequenceNumber)
at kCura.Mail.ImapMessage.QueryAttachmentNames() in c:\TFS\Architecture\Core\Main\Projects\Windows\kCura.Core.Mail\ImapMessage.cs:line 90
InnerException:
Code-2:
public IList<string> QueryAttachmentNames()
{
try
{
var message = this.ImapClient.FetchMessage(this.ImapMessageInfo.UniqueId);
var attachments = message.Attachments;
if (attachments != null)
{
return attachments.Select(x => x.Name).ToList();
}
return new List<string>();
}
catch (Exception ex)
{
throw new Exception(“Mail exception”, ex);
}
}</span></pre><pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-color: white; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background-image: initial; background-attachment: initial; background-color: white; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Error:</pre><pre style="background-image: initial; background-attachment: initial; background-color: white; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><font face="Consolas">003 BAD UID FETCH not allowed now.</font></pre><pre style="background-image: initial; background-attachment: initial; background-color: white; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><font face="Consolas">Aspose.Email.Imap.ImapException was caught
HResult=-2146233088
Message=003 BAD UID FETCH not allowed now.
Source=Aspose.Email
StackTrace:
at . . (ImapReplyLineInfo[] , String )
at . . ( , Boolean , String , Int32 )
at . . (String , Int32 )
at . . (String )
at Aspose.Email.Imap.ImapClient.FetchMessage(String uniqueId)
at kCura.Mail.ImapMessage.QueryAttachmentNames() in c:\TFS\Architecture\Core\Main\Projects\Windows\kCura.Core.Mail\ImapMessage.cs:line 90
InnerException:
Hi Chandra,
Thank you for writing to Aspose support team.
We have tested this issue at our end using the latest version of Aspose.Email for .NET 5.2.0 and the following code worked fine. Could you please share with us which server you are fetching message from (i.e. Gmail, Yahoo, etc.)? At our end, we have fetched messages from Exchange Server 2010 using the API’s ImapClient. Please feel free to share additional information with us for replicating the issue at our end.
Code:
ImapClient client = GetAsposeEWSImapClient();
ImapQueryBuilder builder = new ImapQueryBuilder();
builder.InternalDate.Since(DateTime.Today.AddDays(-1));
client.SelectFolder(ImapFolderInfo.InBox);
ImapMessageInfoCollection msgsColl = client.ListMessages(builder.GetQuery());
if (msgsColl.Count > 0)
Console.WriteLine(client.FetchMessage(msgsColl[0].UniqueId).Subject);
```
Hi Muhammad,
thank you for your quick response. I am using imap to connect to a gmail account. Please find below parameters I am using for creating an ImapClient object.
Code:
Host = “[imap.gmail.com](http://imap.gmail.com/)”,
Username = "username@gmail.com",
Password = “password”,
Port = 993,
SecurityOptions = SecurityOptions.SSLExplicit
I am able to perform all queries using the above client setup except for FetchMessage().
Thanks.
Hi Muhammad,
thank you for your quick response. I am using imap to connect to a gmail account. Please find below parameters I am using for creating an ImapClient object.
Code:
Host = “[imap.gmail.com](http://imap.gmail.com/)”,
Username = "username@gmail.com",
Password = “password”,
Port = 993,
SecurityOptions = SecurityOptions.SSLExplicit
I am able to perform all queries using the above client setup except for FetchMessage().
Thanks.
Hi Chandra,
Could you please give a try to the following code sample. It is working fine at my end without using the SSLExplicit mode.
Sample Code:
ImapClient client = new ImapClient(“[imap.gmail.com](http://imap.gmail.com/)”, 993, “username”, “password”);
client.SelectFolder(“Inbox”);
ImapMessageInfoCollection msgsInfo = client.ListMessages();
Console.WriteLine(client.FetchMessage(msgsInfo[0].UniqueId).Subject);
Hello Kashif,
Thank you for your quick response. I tested my code without using SSLExplicit mode and I was able to successfully query attachments. Is there a way to query for attachments in imap using SSLExplicit mode?
Hi Chandra,
While using ImapClient with Gmail, you have to use either SecurityOptions.Auto or SecurityOptions.SSLImplicit as you can read from this article as well that says:
On the “Incoming server (IMAP)” field, enter 993 and mark the box “This server requires an encrypted connection (SSL)”
where:
SSL = SSLImplicit
TLS = SSLExplicit
For Gmail, all our client’s settings are as follow:
SecurityOptions.SSLImplicit,
“**[imap.gmail.com](http://imap.gmail.com/)** ”,
993,
SecurityOptions.SSLExplicit,
“**[smtp.gmail.com](http://smtp.gmail.com/)** ”,
587,
SecurityOptions.SSLImplicit,
“**[pop.gmail.com](http://pop.gmail.com/)** ”,
995,
Please let us know if we can be of any additional help to you in this regard.
Thank you for all your help. My issue is resolved.
Hi Chandra,
Thank you for providing feedback and please write to us for any other query.