Fetching size of message using IMAP command - c#

I am fetching the size of message using following IMAP command.
"$ FETCH UID RFC822.SIZE\r\n"
For some messages the command works properly and returns the message size
"* 3 FETCH (RFC822.SIZE 2376)\r\n$ OK Success\r\n"
But for some emails its not fetching the size of message. It returns only Success message but not the size
Here is the response for some messages.
"$ OK Success\r\n"
Is there any alternative way for fetching the size of message?

Stepping out on a limb... what you have in mind is x uid fetch 1234 rfc822.size in order to learn the size of the message with UID 1234. However, what you send is y fetch 1234 rfc822.size, and if there are fewer than 1234 messages in the mailbox that command will not work.
Am I guessing correctly?

If you get no size information back, then it means that the message doesn't exist.

Related

C#: How to keep track of the UID of messages between folders?

I'm moving messages from the inbox folder to a new folder. After a while I need to move some of these messages(not all) to a new folder. The problem with this is that everytime I move a message the uid changes. How can I do this without moving the wrong message?
If your server supports UIDPLUS (nearly all do), and you yourself are moving or copying the messages, the server will give you the new UID when you copy or move it in the COPYUID response. You'll need to record this.
An example from the IMAP MOVE RFC6851:
C: a UID MOVE 42:69 foo
S: * OK [COPYUID 432432 42:69 1202:1229]
S: * 22 EXPUNGE
S: (more expunges)
S: a OK Done
The COPYUID response includes the UIDVALIDITY of the destination mail box, the UID set of the source messages, and the UID set of the destination messages.
If some other process is doing the moving, there's no way to guarantee the same message, and you'd have to track them based on their content (eg, the Message-ID header, the date time, a hash, etc.)
You're "moving" also involves an INSERT into DB to keep track of UIDs? If yes, watch for autogenerated PKs.

GetMessage() take excessive long time MailKit

When I call
var result = MailKit.GetMessage(uid)
Then the call takes around 4 seconds to return. Is there any better/faster way to get the mime message from the server??
Currently doing the following:
1. Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope | MessageSummaryItems.Flags)
2. Loop the fetch result to find a messageId match
3. Send GetMessage() with the UID found previously.
I can see that FETCH in most cases takes around 2 secs, all in all my process will take something like 6 seconds, which is kinda "long" before I can present the email in the client.
Is there a faster way to get a mime message from a mail server using MailKit, when all I know about the email is the MessageID??
You could cache all of the message summaries so that you don't have to ask the server for them each time...
You could also try:
var uids = folder.Search (SearchQuery.HeaderContains ("Message-Id", messageId));
if (uids.Count > 0)
message = folder.GetMessage (uids[0]);

Sending OSC message bundles from C# and receiving in Processing got weird address pattern and errors

I am relatively new to programming, so please excuse me if the question is stupid. I am now working on a project which involves Kinect. I am using C# to extract the live joint information (such as position and orientation) and then sending the data into Processing using OSC message -Udp protocal. I sent OSC message bundle from C# the problem is I don't know how to dispatch the message to what I want in Processing. Or possibly, I sent the data in a wrong format in C#. I would be very appreciate if someone can tell me what could possible gone wrong in the code and caused the errors.
I send the joint position from C# by using these code:
if (joint0.JointType == JointType.ElbowRight)
{
// distance in meter
String temp = "ElbowRight " + joint0.Position.X * 1000 + " " + joint0.Position.Y * 1000 + " " + joint0.Position.Z * 1000;
Console.WriteLine(temp);
OscElement message = new OscElement("/joint/" + joint0.JointType.ToString(), joint0.Position.X * 1000, joint0.Position.Y * 1000, joint0.Position.Z, joint0.TrackingState.ToString());
bundle.AddElement(message);
}
OscSender.Send(bundle); // send message bundle
The part,"/joint/", is the address pattern of the message. The following data are the arguments of the message. According to http://opensoundcontrol.org/spec-1_0 a OSC type tag string should be added following to the address pattern, which is an OSC-string beginning with the character ',' (comma) followed by a sequence of characters corresponding exactly to the sequence of OSC Arguments in the given message. However when I tried this, a format exception was caused and the error was reported as: Invalid character (\44). What I did was simply added ",s" to the OSC message:
OscElement message = new OscElement("/joint/" + ",s" + joint0.JointType.ToString(), joint0.Position.X * 1000, joint0.Position.Y * 1000, joint0.Position.Z, joint0.TrackingState.ToString());
How do I suppose to add the type tag? Can this be the reason that caused the following errors?
In my Processing code, I tried to get the joint position value by using these code:
if(theOscMessage.checkAddrPattern("/joint")==true) {
String firstValue = theOscMessage.get(0).stringValue();
float xosc = theOscMessage.get(1).floatValue(); // get the second osc argument
float yosc = theOscMessage.get(2).floatValue(); // get the second osc argument
float zosc = theOscMessage.get(3).floatValue(); // get the second osc argument
String thirdValue = theOscMessage.get(4).stringValue(); // get the third osc argument
println("### values: "+xosc+", "+xosc+", "+zosc);
return;
}
However I got this error:
[2013/6/16 20:20:53] ERROR # UdpServer.run() ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException
Than I tied receiving the messaging using a example given in Processing, which displays the address pattern and type tag of the massage:
println("addrpattern\t"+theOscMessage.addrPattern());
println("typetag\t"+theOscMessage.typetag());
It printed out this:
addrpattern pundle
typetag u???N?N?$xlt???
I dont understand what's wrong with the code. Souldn't the address pattern be "joint"? or at least "bundle"? What is the pundle...
P.s. I am using Visual C# 2010 Express and Processing 2.0b9 64bit on a Win7 os computer.
Thank you so much for the help!
Update:
Although I still can't figure out how to solve this problem, I found a way to receive messages in Processing. Instead of using OSC bundle, I am sending Osc messages with different address pattern. then use message plug (e.g. oscP5.plug(this,”leftFoot”,”/joint/AnkleLeft”);) in the draw method. Then create a method called leftFoot
public void leftFoot(float fx, float fy, float fz, String state) {
println("Received: "+fx+", "+fy+", "+fz+", "+state);
}
Then you can see the data being print out. p.s. in C# the OSC message was send using:
OscElement message = new OscElement("/joint" + "/" + joint0.JointType.ToString(), joint0.Position.X * 1000, joint0.Position.Y * 1000, joint0.Position.Z, joint0.TrackingState.ToString());
OscSender.Send(message);
Hmmm... not sure exactly, but you could just use OSCeleton. It comes with a Processing example - I've used it before, and it works fine.
(the example may also help you understand how to use the OSC address patterns correctly... )
https://github.com/Sensebloom/OSCeleton
https://github.com/Sensebloom/OSCeleton-examples
OSC requires padding to next 32-bit boundary, meaning that you need to add zeros to pad the message, until its length is an even multiple of four bytes.
You need to do the same for the typetag. Pad it with zeroes even if you only send one typetag, so ,s00 instead of just ,s

Messages getting overwritten on IBM MQ

I am sending 3 messages
Message1 - correlation id:5000
empty message (no body/message)-correlation id:5001
Message2 - correlation id:5002
My outbound queue processes like this
Message1 - correlation id:5000
Message1-correlation id:5001 => same previous message ovewritten on the empty message.*
Message2 - correlation id:5002
The second line above should not have had Message1, instead just empty. Any thoughts?
My get method
mqGetMsgOpts = new MQGetMessageOptions();
if (mqQueue != null)
{
//Get options for the messsage
mqGetMsgOpts.Options = MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_WAIT | MQC.MQOO_INQUIRE;
mqGetMsgOpts.MatchOptions = MQC.MQMO_NONE;
mqGetMsgOpts.WaitInterval = 5000; // 5 seconds limit for waiting
}
if (mqMsg.MessageLength > 0 && mqMsg.DataLength > 0)
{
messageData = mqMsg.ReadString(mqMsg.MessageLength);
}
If I don't do the length check, i will get stream reader related exception.
My put method
if(mqQueue==null)
mqQueue = mqQMgr.AccessQueue("Queue Name", MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_SHARED | MQC.MQOO_INQUIRE);
mqMsg.WriteString(message);
I have not heard messages getting overwritten in WMQ. I suspect this must be an issue with application. This line of code:
mqGetMsgOpts.Options = MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_WAIT | MQC.MQOO_INQUIRE;
The MQC.MQGMO_BROWSE_FIRST option will make WMQ to return always the first message that satisfies the conditions specified in MQMD structure. I can't make out from your code snippet if this option is modified at a later point to specify MQGMO_BROWSE_NEXT to read the next message in the queue.
Instead of MQC.MQGMO_BROWSE_FIRST you can specify MQGMO_BROWSE_NEXT option to continuously read messages.
Also you have specified MQC.MQOO_INQUIRE which is not valid for GMO options. You need to remove that.
More details browse options are here
I can imagine two possible issues that could cause this.
Your putting application did not send an empty message body for message two.
Your getting application is showing you the message buffer from message one. If there is no message buffer delivered from MQ, your previous message buffer contents will remain.
To determine which has happened to you I suggest you put all the messages, but before running your sample to get them, instead run something like the supplied sample amqsget to rule out the possibility of 1.
Then you can focus on the get buffer in your application. Make sure you are not using it if MQ said the length of the returned message is zero.

Download emails (backup) from gmail programmatically

Does anyone know a way to execute a bulk dump of every email of a gmail account and write the emails to a file?
I'm looking to write a program that would let users back up there gmail (probably via imap) and back it up to either individual files or as a pst (I know pst will probably be much harder)
some time ago I wrote a blog post about exactly same topic. See HOWTO: Download emails from a GMail account in C# for details.
Code uses our Rebex Mail component:
using Rebex.Mail;
using Rebex.Net;
...
// create the POP3 client
Pop3 client = new Pop3();
try
{
// Connect securely using explicit SSL.
// Use the third argument to specify additional SSL parameters.
Console.WriteLine("Connecting to the POP3 server...");
client.Connect("pop.gmail.com", 995, null, Pop3Security.Implicit);
// login and password
client.Login(email, password);
// get the number of messages
Console.WriteLine("{0} messages found.", client.GetMessageCount());
// -----------------
// list messages
// -----------------
// list all messages
ListPop3MessagesFast(client); // unique IDs and size only
//ListPop3MessagesFullHeaders(client); // full headers
}
finally
{
// leave the server alone
client.Disconnect();
}
public static void ListPop3MessagesFast(Pop3 client)
{
Console.WriteLine("Fetching message list...");
// let's download only what we can get fast
Pop3MessageCollection messages =
client.GetMessageList(Pop3ListFields.Fast);
// display basic info about each message
Console.WriteLine("UID | Sequence number | Length");
foreach (Pop3MessageInfo messageInfo in messages)
{
// display header info
Console.WriteLine
(
"{0} | {1} | {2} ",
messageInfo.UniqueId,
messageInfo.SequenceNumber,
messageInfo.Length
);
// or download the whole message
MailMessage mailMessage = client.GetMailMessage(messageInfo.SequenceNumber);
}
}
Gmail provides POP access. So just use any library that allows you to communicate using POP and you're golden.
Edit: I just noticed that you mentioned IMAP; I recommend you use POP instead for bulk dumps. IMAP is too chatty for what you want to do.
If you must use IMAP, here's a library for you.
You can use fetchmail from a Unix environment to create an mbox file.
http://lifehacker.com/software/gmail/geek-to-live--back-up-gmail-with-fetchmail-235207.php
There is an open-source Python program compiled to Windows (using py2exe) at
https://github.com/jay0lee/got-your-back/wiki
But Mac users would need to compile it (which I haven't completely figured out due to a py2exe error).
Either way, you also need a way to execute the program automatically in a schedule.

Categories