JavaMail - Stop automatically sending read receipts - java

I'm developing a Mail Client (IMAP/SMTP) with JavaMail. This client talk to an Exchange 2010 Server that automatically sends read receipts when I set the flag SEEN in messages that require them. How can I avoid the server to send these receipts? I tried to remove the Disposition-Notification-To header from messages but i get the following exception:
javax.mail.IllegalWriteException: "IMAPMessage is read-only"
even if i open their folder in READ_WRITE mode. I read that this problem is due to an IMAP protocol limitation. Is there a way to not send read receipts?

You can't do that in your client. Your client doesn't do it, and IMAP offers no way to configure Exchange.
(Also, IMAP offers no way to modify messages. Once stored, a message can be cached forever by any client and not be modified by any other client.)

The right thing to do is to reconfigure the server.
Lacking that, you could make a copy of the message, modify the copy to remove the header, append the copy to the original folder, and delete the original. You'll need to use the IMAPMessage.setPeek method to prevent the SEEN flag from being set on the original message. Use the MimeMessage copy constructor to make the copy. Note that this will be expensive if the message is large.

Related

Send "MOTD" to Minecraft client from custom Netty server

I have a basic Netty server (From the tutorial) (http://netty.io/wiki/user-guide-for-4.x.html), and it recieves the requests from the client, but how would I go about sending a string to the client?
For example, on a normal Minecraft server, you specify the "MOTD" in the configuration file, and when a client pings it from the server list, it will display that string. I need to do the same thing, but from my server code.
If you wish to send the MoTD to the client you will have to figure out what gets sent in terms of protocol and data.
For example in the most basic form the data sent could be 1 byte for action (display motd) and then variable length for a string.
If I had to find out how to send this I would go look at the open-source bukkit repositories or the Minecraft Decompiled Code Repository to find out the way to do it myself.
Update:
Upon looking at the code it seems that also Minecraft uses Netty, so this plays in your advantage in terms of understanding it. Unfortunately the code is unofficially decompiled and thus obfuscated.
Update 2:
I believe the class you should inspect is
net.minecraft.server.PacketStatusOutServerInfo
and the data sent appears to be JSON generated by the ServerPing class.
You can also check out Minecraft Protocol (specifically Ping); a place where modders can find an explanation of protocol & encryption.
This handler shows the sending of the MOTD:
https://github.com/Bukkit/mc-dev/blob/c1627dc9cc7505581993eb0fa15597cb36e94244/net/minecraft/server/LegacyPingHandler.java
It just happens to be that the MOTD handling goes on line number 69: https://github.com/Bukkit/mc-dev/blob/c1627dc9cc7505581993eb0fa15597cb36e94244/net/minecraft/server/LegacyPingHandler.java/#L69
When the channel receives the ping packet, it encodes the response into a ByteBuf and writes it back out of the channel.
Note that there are a few decompilation errors on the file - ignore them and fix it.

Sending mail from java code - the reliable way

It's a pretty common thing to have to send mails from your app and in most cases it's a real pain in the ... you know where.
So what I'm doing is taking Apache Commons Email (which is based on top of the "official" java mail api) and I'm sending out email in the most simple way there is, i.e. without authenticating to a smtp server. I just do a simple MX lookup on the destination hostname, get the MX servers and try to drop my message at the first one (whether the mail gets rejected or not is a completely different issue and I might sometime soon ask a further question about the whole mess up with return-path: vs. from: vs. reply-to: and the way these headers are (not) handled in java). Back to business... So I've just tried to drop my Message at the mail server with the least preference score.
Here is an example: I want to write to recipient#domain.com. The MX Lookup tells me that domain.com is aware of two MX servers and these are e.g. mail1.domain.com with preference 10 and mail2.domain.com with preference 20. The rfc way to do things is to go to the server with the least preference and drop the mail there. So that's what I do.
And finally my problem: What happens if that server is not available in some way or another? It's pretty simple - I go to the other server, but Apache Commons (and I suspect java mail api) doesn't allow me to do just that. The mail.smtp.host variable is rooted inside the props of the session in the message in the email. And I cannot get at it.
So what's the best way to handle this problem? Should I build my Email completely from the top with the new hostname (mail2), or is there some clever way to make this all work in java without much pain?
It sounds if you are trying to implement a partial mail server, not just merely sending an e-mail. Routing, relaying, caching and delivery retrials are operations implemented and offered by all mail servers and not usually done by mail clients.
What you should do is either use an (one!) existing mail server, which is configured for you to allow relaying outbound mails or if you don't have access to such a server (which I doubt), setup and operate your own server. You then configure this server in mail.smtp.host and forget all you've learned about DNS lookups, server priorities and your worries about what to do if none of the MX servers are reachable.

Best way to send an E-Mail from a java Application

I've been searching for the best way to send an e-mail with an attachment by my java application. I want to use this as an users bug report with logger files. The recipient should be my own e-mail address. I'd prefer the use of an e-mail client.
I tried the following:
Send an e-mail with user authentification like this. I don't want to use this, because the user would need to reveal his e-mail account and password. Furthermore, I'd have to set the properties for every e-mail adress, which is impossible.
Send an e-mail directly to my own e-mail address like this in Listing 16.16 (didnt found an english example). The problem is every e-mail server is using POP authentification nowadays, that means the recipient e-mail server won't accept my e-mail.
Using the mailto URL syntax like this. Doesn't work aswell, because the attachment function isn't working properly in every e-mail client. Best solution so far is to brief the user to add the attachment by himself, after I would put it to his desktop. Or upload the data and add a link to the e-mail body.
The last way I've found is this one. As you could assume this won't work either, because the localhost needs to be connected to the internet and capable enough to send an e-mail.
Hopefully I explained my problem well enough. Is there a different way to send bug reports?
The generally accepted way to get around the problems you describe is to keep all the email logic server side, and then have your application call a web service with the appropriate parameters. It's pretty easy to knock a PHP script / servlet up that will do the job and then send the results on via email, put them in a mysql database or so on.
However, if you can't / don't want to / won't keep this server side, I'd recommend using JavaMail to create a MimeMessage, then using writeTo() to write this to an EML file.
You can then do the usual:
Desktop.getDesktop().open(emlFile);
...which will open the EML file with the default application for handing those files, which is almost always the mail client. Still not foolproof, but if you're determined on sending the email from the client directly I think that's as good as you'll get.

The quintessential email listener / mini server in Java or .NET

I'm embarrased of how I'm unaware of SMTP / POP3 / IMAP protocols,
as much as I thought I know HTTP and TCP/IP it apears that I took email as granted, and never had to write any piece of code that will do other than sending an email via an existing SMTP server.
My task is to write an incomming email channel and I would like to hear what is the basic aproach
What I need is the ability to listen to a specific email address, and capture the body, subject and attachment of that email for further processing.
I understand you want to programatically recieve mail...use subethasmtp (much lighter and easier than james etc, works very well.
If you want your server to receive e-mails, it's an SMTP server that you need.
(You'll also need to make sure that the e-mail address is set up to be sent to that server, via the MX entry in the DNS.)
Note that, depending on how you want to install this service, you might not need to write an SMTP server yourself (or even use a library). Existing SMTP servers are often capable of delegating the processing of an e-mail to external applications.
You could use somelike Postfix and configure it to use pipe for that address, to send the e-mail to process to the program of your choice (including one that you develop yourself). I'm fairly sure Exim, Sendmail and other MTAs have similar features.
With this sort of configuration, your application would usually need to be able to read the e-mail from the standard input (and have the ability to split/process headers and body), but that's usually much simpler that writing an MTA/SMTP server.
If you really want tighter integration with the MTA, perhaps this could be a good starting point (I've never tried it): http://james.apache.org/
An SMTP server is usually how you'd refer to an outbound mail server; it sends mail.
POP and IMAP allow you to connect to a mail server, to read the mail that's already been received.
You need the receiving/server side of SMTP, and you might benefit from reading up on MTA; mail transfer agent.
You might also be interested in reading about SMTP proxies; so, sent mail would go through your server - and could be filtered/listened to, I suppose - and then get sent further to it's actual recipient.
Use the JavaMail API

Is there a library for reading /var/spool/mail/ files?

How does one parse the mail files generated on a Linux system using Java? I mean, I want to be able to extract the From, To, Timestamp and Subject of the various emails inside the file. Any suggestions?
javax.mail.internet.MimeMessage.parse(InputStream)
it's protected but you can subclass it to use the method. However, the file format is quite simple, if you just want some headers, why not parse it on your own?
Those files belong to the Mail Transfer Agent and maybe the user's mail client. Other programs should tread very softly or better yet keep out altogether. Or is your program a mail client?
The "clean" way to do this would be to open up an SMTP or IMAP connection to the mail server / MTA and ask it for pieces of mail on behalf of your user, using his credentials that he gives you.
There's a Java mail API for this that knows how to do this well: http://java.sun.com/products/javamail/ .

Categories