Using DispositionNotification with Javamail - jakarta-mail

I am working with a set of specialized email servers that are configured to return Message Disposition Notifications (MDNs) upon successful receipt of the message. I have developed a Javamail client that is used to send messages to one of these servers and retrieve returned MDNs from the user's POP3 INBOX folder.
How do I go about getting at the disposition notification body part from the returned MimeMessage I retrieve from the user's inbox? I've found the com.sun.mail.dsn.DispositionNotification class but haven't seen an example of how to correctly create an instance of a DispositionNotification using the constructor:
DispositionNotification(InputStream)
Should I be able get the InputStream from a MimeMessage and use it to create the DispositionNotification, like so?
Message[] msgs = getInboxMessages();
DispositionNotification dn = new DispositionNotification(msgs[0].getInputStream();
Or is there some other way this should be done?

The getContent method on such a message should return a MultipartReport object (a special subclass of the usual MimeMultipart object), from which you can access the parts of the report. The MultipartReport.getReport() method will return either a DeliveryStatus or DispositionNotification object, depending on the type of the report.

Related

Unable to retrieve attachments from a signed mail having ContentType as "application/pkcs7-mime; name=smime.p7m"

I am trying to read a digitally signed mail from java code using multipart and mime messaging and fetch the attachments (xml, pdf, txt etc.,) and message details.
My code is working fine for mails having Content-Type as : multipart/signed; protocol="application/x-pkcs7-signature";
But For few mails having Content-Type as : application/pkcs7-mime; smime-type=signed-data; name=smime.p7m it is not fetching the attachments and message details. Can anyone explain what is the difference between both of them and how to resolve it.
I recently came across this issue myself, and although this question is three month old, I leave an answer with my findings, just in case.
Both kinds of messages are instances of S/MIME signed messages as specified in RFC2633 (https://www.rfc-editor.org/rfc/rfc2633).
The multipart/signed; protocol="application/x-pkcs7-signature" indicates a clear-signed message (section 3.4.3.3 of the RFC), meaning you can read the original message content without having S/MIME capabilities in your client code. Hence no problem with these.
The application/pkcs7-mime; smime-type=signed-data; name=smime.p7m indicates an S/MIME signedData email (section 3.4.2) Your client code needs S/MIME capability in order to read the original message (even if you don’t care about the signature).
Easiest way (worked for me) is to use bouncycastle's SMIMESigned class (from the S/MIME API, https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15on), like this:
byte[] content = <the signed data's content as byte[]>;
ByteArrayDataSource dataSource = new ByteArrayDataSource(content,"multipart/signed");
SMIMESigned signedData = new SMIMESigned(new MimeMultipart(dataSource));
MimeBodyPart bodyPart = signedData.getContent();
<you can process the body part as normal from here>

How to update a Azure AD user?

I have successfully created a new user in Azure AD following Create User reference.
Now I want to update that very same user following Update User reference. To keep things simple This is the JSon content I'm sending:
{
"userType": "T"
}
The documentation tells me to send a PATCH request, but I always receive an HTTP Error 400 (Bad request). If I try sending a POST request I receive an HTTP Error 405 (Method not allowed).
The destination URL is https://graph.microsoft.com/v1.0/users/user-id.
The user-id is the one returned with the user details.
What am I missing?
Update 1
Those are the permissions set to the application:
This is exactly how you update user (PATCH).
However the userType property cannot be T, from the docs you refer:
That property can only have one of the two distinct values: Member or Guest.
Of course, the user-id in path should the id property as returned by the get users operation.
I am pretty sure that if you use a normal REST client will be able to see the whole error message, which will be more meaningful.
If you still have issue - paste the exact error message.

Why is Gmail returning a UID outside the range specified by UID FETCH command?

When I run a fetch command UID FETCH 170930:170930 BODY[] I get the response 88924 FETCH (UID 170920 FLAGS (\Seen)). I wasn't expecting to retrieve a message with a UID out of the range specified. Is this normal IMAP behaviour? The 170930 UID came from watching the folder with an IdleManager only moments earlier, so I have no reason to believe that a message with that UID doesn't exist on the server.
The fetch request I've posted here is a guess based on the Java code I'm using to execute it. At the very least it should still be requesting only messages within that range:
Argument args = new Argument();
args.writeString(Long.toString(start) + ":" + Long.toString(end));
args.writeString("BODY[]");
Response[] r = protocol.command("UID FETCH", args);
Response response = r[r.length - 1];
An IMAP server is required to send you FETCH responses in certain cases. Sometimes the server is required to wait with responses, but it's never required not to send you any.
If you send a command that requires two response, and someone else does something that requires one response to you, then you get three responses. That something might be to change the flag on a message (requires FETCH ... FLAGS ... to you, although there's no promptness requirement) or send you some mail (requires EXISTS to you).

Getting a users mailbox current history Id

I'd like to start syncing a users mailbox going forward so I need the most recent historyId of the users mailbox. There doesn't seem to be a way to get this with one API call.
The gmail.users.history.list endpoint contains a historyId which seems to be what I need, from the docs:
historyId unsigned long The ID of the mailbox's current history record.
However to get a valid response from this endpoint you must provide a startHistoryId as a parameter.
The only alternative I see is to make a request to list the users messages, get the most recent history id from that, then make a request to gmail.users.history.list providing that historyid to get the most recent one.
Other ideas?
Did you check out https://developers.google.com/gmail/api/guides/sync ?
Depending on what your use-case is, to avoid races between your current state and when you start to forward sync, you'll need to provide an appropriate historyId. If there were a "get current history ID" then anything between your previous state and when you got those results would be lost. If you don't have any particular existing state (e.g. only want to get updates and don't care about anything before that) then you can use any historyId returned (e.g. on a message or thread) as you mention.
Small example for C# users (mentioned in comments by #EricDeFriez).
Nuget package Google.Apis.Gmail.v1 must be installed. See also quickstart for .NET developers.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var req = service.Users.GetProfile("me");
req.Fields = "historyId";
var res = req.Execute();
Console.WriteLine("HistoryId: " + res.HistoryId);
This answer is related to the Java Gmail API Client Library using a service account.
I found that the gmail.users.getprofile() will not work as the object that it returns is of type Class Gmail.Users.GetProfile which does not have an interface to getting a historyId.
com.google.api.services.gmail.model.Profile actually has a getHistoryId() function, but calling service.users().getProfile() will return a Class Gmail.Users.GetProfileobject instead.
To get around this, I use the history.list() function which will always return the latest historyId as part of its response.
Gmail service = createGmailService(userId); //Authenticate
BigInteger startHistoryId = BigInteger.valueOf(historyId);
ListHistoryResponse response = service.users().history().list("me")
.setStartHistoryId(startHistoryId).setMaxResults(Long.valueOf(1)).execute();
I set the max number of results to be 1 to limit the unnecessary data that I get returned back and I will receive a payload that looks like:
{"history":[{"id":"XXX","messages":[{"id":"XXX","threadId":"XXX"}]}],"historyId":"123456","nextPageToken":"XXX"}
The historyId (123456) will be the current historyId of the user. You can grab that historyId using response.getHistoryId()
You can also see that the latest historyId is given in the response if you use the API tester for Users.history: list
https://developers.google.com/gmail/api/v1/reference/users/history/list

Can I use one servlet to receive xmpp messages, and another to send them in App Engine?

Does the required servlet URL mapping (/_ah/xmpp/message/chat/) for the xmpp service restrict me to using only that servlet for both sending and receiving xmpp messages?
My application receives messages, adds them to an 'inQueue' for processing, and once processed, the results are added to an 'outQueue'. Ideally I'd like the worker servlet assigned to the outQueue to send the response.
I am using push queues and the servlets are obviously invoked by an HTTP POST request...
When I try to use the xmpp service to send the message from my outQueue worker ( which is obviously not mapped to /_ah/xmpp/message/chat/ ), it expectedly does nothing.
In the servlet mapped to /_ah/xmpp/message/chat/:
//Generate a Memcache key
String key = generateMemcacheKey(message);
//Serialize the message as xml using getStanza (for now, just put in the senderJID as string)
String senderJIDString = message.getFromJid().getId();
//Cache the message in Memcache
cache.put(key, senderJIDString);
//Enqueue a task for the message
private Queue queue = QueueFactory.getQueue("inQueue");
queue.add(TaskOptions.Builder.withUrl("/xmppParser").param("memcacheKey", key));
In xmppParser servlet doPost method:
//Extract message from memcache
String key = req.getParameter("memcacheKey");
String recipientJIDString = (String)cache.get(key);
//Todo Detect language
//Todo Parse Message into an Imperative accordingly (For now, just reply hello)
//Todo Handle Session status
//Put Imperative into memcache (For now, just put recipientID in)
cache.put(key, recipientJIDString);
//Enqueue appropriately
Queue queue = QueueFactory.getQueue("outQueue");
queue.add(TaskOptions.Builder.withUrl("/responseServlet").param("memcacheKey", key
));
Now I would like this responseServlet to send the reply:
//Get a handler on the memcache
MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
//Extract the key to the response from the request
String key = req.getParameter("memcacheKey");
//Extract the message from the memcache ( For now it's just the JID of the sender)
String recipientJIDString = (String)cache.get(key);
//Parse it into a message (For now just make a simple "I hear you" message)
JID recipientJID = new JID(recipientJIDString);
Message response = new MessageBuilder()
.withMessageType(MessageType.NORMAL)
.withRecipientJids(recipientJID)
.withBody("I hear you")
.build();
//Send the message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
xmpp.sendMessage(response);
The servlet mappings are all kosher, and the xmpp service is unable to send the message. It raises a null pointer exception, but the memcache viewer confirms the recipientJID is kosher as well... I'm guessing only the servlet mapped to /_ah/xmpp/message/chat/ is able to send messages, or am I wrong?
I am forced to test on the live site itself as XMPP does not seem to be supported by the development server, adding the possibility of my having overlooked some production environment configuration...
Much obliged!
ram

Resources