receiving and handling emails in java - java

I am working on application that reads input from email and do action according to it.
My application should listen for any new emails and parse it to get attachment file and data inside the body.
My question is: How can I do that in java? how to do that listener in java?
I have an email that I will use to parse received from, but what I want how to listen to any new email and parse data from it.

You probably want to take a look at JAMES. This offers all the functionality you require.

Related

Telegram client for android: how to get last message?

I want to built a custom Telegram client for android with a function to save messages into file. As a base project, I'm using official source code: https://github.com/DrKLO/Telegram
And I can't find a way to actually get a message. In Documentation for Telegram mentioned method getChatHistory https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1get_chat_history.html
But I don't see any usage of the API in their code. Instead, they are used custom classes somehow similar to API. And the general class for any actions with messages is MessageObject. I can get the text content of this object by using this
message.messageText.toString();
where message is object of class MessageObject, but how I can create this object?
The supposed workflow is: when interested chat is opened, besides already existed common buttons, custom button for save message is appearing, after tapping this button, begins a cycle in which we create object of class MessageObject start with id of last message in chat, we extrating a text from this message, and saves it a file.

Send long sms via Opensmpp

I use opensmpp library for sending sms, but I cannot find documentation or example of sending long messages. I want to send long messages by splitting and using UDH (because it's sms gate requirement). Where can I find documentation or example for Opensmpp java library?
Good to hear it worked! As a short summary, this would be the way to add additional header information for multipart/concatenated SMS delivery using OpenSmpp:
SubmitSM smRequest = new SubmitSM();
smRequest.setEsmClass((byte)Data.SM_UDH_GSM);
The above code was taken from this article about multi-part SMS that includes sample code and extended technical details.

Sending same emails to many addresses

Hi
I have to send am mail(like an notification) to all the persons who fulfill some criteria.
The mail id will be taken from the database and sent the mail
Where should I look for the implementation in JAVA so that I can sent the a mail to many person.
Thanks
Have a look at the javax.mail Package
Links:
http://java.sun.com/products/javamail/javadocs/javax/mail/Message.html#addRecipient%28javax.mail.Message.RecipientType,%20javax.mail.Address%29
And as Message.RecipientType you should use Message.RecipientType.BCC to not showing the every address to every recipient
Google Keywords: Java Mail BCC
This is how to send email using Java: http://www.javabeat.net/tips/33-sending-mail-from-java.html
To send email to many addresses, just send the same email with different 'to' address
There are several ways to go about this. Assuming you know how to query the database to get the mail and recipients and how to actually send (a single) mail - it's really up to preferences.
Personally I prefer to simply put all recipients in "the BCC-field" and then actually send the mail to a dummy address or my own. That way none of the recipients will be disclosed to the others. If that is not an issue - just put 'em all in the "To-field".
(if in fact querying database and sending mail is the real issue - I'm sure there are quite a few references on this site)
The Apache Commons Email library in an excellent Java API for sending Email. You can use the approach taken by #mbanzon to send the list by adding Bcc fields.

Search email inbox using javax.mail

I was trying to see if there was a way to search an email inbox from javax.mail. Say I wanted to send a query and have the it return emails to us. Can we parse returned HTML and extract data. Also, if the above is possible how would I "translate" those messages returned by that server to POP3 messages? E.g. we have extracted:
Subject: Foo
Body: Bar
but to open same message using POP3 I need to know it's POP3 uid, or number. I don't think we'll be able to get UID but perhaps we can figure out the number.
I guess the question is:
Can I send a query to email server (such as Hotmail or Yahoo) and get returned emails?
Unfortunately, the POP3 protocol doesn't support that. It is not like SQL or so. You need to mirror the complete mailbox yourself in some kind of a datastore (SQL database?) and execute the search on that. You can eventually keep/cache the data so that you don't need to retrieve the whole inbox everytime, but only the unread items.

How to track an email in Java?

How I can track an email?
I' m using java on the server side for sending emails. I want to track whether it is delivered , opened, etc... How I can do that ?
This is not a Java specific issue.
You can create an HTML email, and embed an invisible gif which will report back to your server. Some software like Outlook and some web mail programs will block this for untrusted emails.
You can request a return receipt. Many mail programs ignore this entirely, and the ones which don't usually ask the user if they want to send it.
Example:
email.AddHeaderField("Disposition-Notification-To","<g.revolution#stackoverflow.com>")
There is no way to ensure that you always get the delivery or open-message notification.
Mailservers may accept the mail and drop it afterwards.
users may read the mail but dismiss the notification.
"Webbugs" (aka images in the html source of the mail that include a special token that allows the mail to be recognized) don't work in most email programs.
As a matter of fact it's very unlikely that you can see that someone got the message.
What you could do is to keep the message on your server and only send a link. If the user clicks that you can be pretty sure that he got the message. But thankfully many users would not click on such links because it's used in fraud and spam.
I suppose you're sending it through SMTP. Whenever your mail is sent to your SMTP server, your java program has no control of it:
1) If you want to know if your mail has been delivered, you should contact your SMTP server somehow (if the SMTP server is outside your control then forget that) and see if your mail has been sent.
2) You can't know if a mail has been opened by its receiver. The maximum you can do is set a flag that the mail requires acknowledgement, but that depends if the user explicitly wants to send that acknowledgment. Other possibility is set some link to your site within the mail that should be clicked to see the content. You will be able to track if the user clicked that link.
what you can do is -
you have to embed an invisible image in the body in which the src will call the page in server and log that event, you can only do it in HTML encoding.
example -
<img src="ImageServer.aspx?imageID=track.jpg& custID=134ghxx34343ai& campID=32434"/>
and then in ImageServer.aspx there will a handling code which will log that event or save it to database.
example -
private void Page_Load(object sender, System.EventArgs e)
{
// content type should be resolved programmatically
Response.ContentType = "image/jpeg";
if (!IsPostBack) Track();
Response.WriteFile(GetImageFileByID());
}
In case you send HTML email, you could add a 1x1 pixel image with some tracking parameters, that calls back to your webserver. It's not 100% reliable since some email clients (and users) block images in emails.
You can add something like
<span style="display:none">Tracking no: ${TRACK_NO}</span>
to the body of the email. This should always be in the response unless the email client strips it out.

Categories