want to read .ics file in mail box - calendar scheduling - java

I want to read the mail inbox and gather scheduled events. I could do it by reading .ics file the attachment. Outlook can do calendar scheduling and add another attachment to that. In that special case, I could not read .ics file as an attachment. But there is a Part as multipart/alternative. I could not get it as a readable one.
public EmailContentDTO WriteAndReadCalendarFiles(Message message) {
try {
LOGGER.info("Start function WriteAndReadCalendarFiles");
// create calendar entry
Map<String, String> calendarEntry = null;
// get as multipart object
Object content = emailFolder.getMessage(message.getMessageNumber()).getContent();
Multipart multipart = (Multipart) content;
Address[] recipients = message.getAllRecipients();
LOGGER.info("multipart.getCount() : " + multipart.getCount());
// get multipart one by one
for (int k = 0; k < multipart.getCount(); k++) {
// write ICS file
Part part = multipart.getBodyPart(k);
// LOGGER.info("Attachment properties: Disposition:" + part.getDisposition()+", Description: "+ part.getDescription()+" Content-Type: "+part.getContentType() + "File name:" + part.getFileName());
if ((part.getFileName() != null && part.getFileName().endsWith(".ics")) || part.getContentType().contains("text/calendar;")) {
// LOGGER.debug("File name:" + part.getFileName());
String disposition = part.getDisposition();
if (disposition == null) disposition = "attachment";
String fileUploadDir = environment.getRequiredProperty("icsFileUploadDirectory");
//make directory
File uploadDirectory = new File(fileUploadDir);
if (!uploadDirectory.exists()) {
uploadDirectory.mkdirs();
}
String fileName = "invite.ics";
if (disposition.contains("attachment")) {
LOGGER.info("start write ics file");
writeICSFile("invite.ics", fileUploadDir, part.getInputStream());
}
// read ICS file
FileInputStream fileInputStream = new FileInputStream(fileUploadDir + fileName);
// create calendar object
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(fileInputStream);
// read calender object and retrieve data
for (Iterator i = calendar.getComponents().iterator(); i.hasNext(); ) {
Component component = (Component) i.next();
if (component.getName().equalsIgnoreCase("VEVENT")) {
calendarEntry = new HashMap<>();
for (Iterator j = component.getProperties().iterator(); j.hasNext(); ) {
net.fortuna.ical4j.model.Property property = (Property) j.next();
calendarEntry.put(property.getName(), property.getValue());
//get time zome
if (property.getName().equals("DTSTART")) {
Parameter tzid = property.getParameter("TZID");
if (tzid != null) {
calendarEntry.put("TZID", tzid.getValue());
}
}
}
// get ATTENDEES
ArrayList<String> attendees = new ArrayList<>();
//format address to tags email format and add to the list
for (Address address : recipients) {
String nonFormatAddress = address.toString();
if (nonFormatAddress.contains("<")) {
Pattern pattern = Pattern.compile("<(.*?)>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(nonFormatAddress);
if (matcher.find()) {
attendees.add(matcher.group(1));
}
} else {
attendees.add(address.toString());
}
}
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date startTime = dateFormat.parse(calendarEntry.get("DTSTART").split("z")[0]);
Date endTime = dateFormat.parse(calendarEntry.get("DTEND").split("z")[0]);
if (calendarEntry.containsKey("TZID")) {
if (calendarEntry.get("TZID").toLowerCase().contains("sri lanka")) {
startTime = dateGenerator.changeHoursOfDate(startTime, -330);
endTime = dateGenerator.changeHoursOfDate(endTime, -330);
}
}
// Create EmailContentDTO object
EmailContentDTO emailContentDTO = new EmailContentDTO(calendarEntry.get("SUMMARY"),
calendarEntry.get("ORGANIZER").split(":")[1],
calendarEntry.get("UID"), attendees,
(calendarEntry.get("LOCATION") != null) ? calendarEntry.get("LOCATION") : "",
(calendarEntry.get("DESCRIPTION") != null) ? calendarEntry.get("DESCRIPTION").split(":")[0] : "",
startTime, endTime, Integer.parseInt(calendarEntry.get("SEQUENCE")));
return emailContentDTO;
}
}
}
}
return null;
} catch (Exception e) {
LOGGER.error("Function WriteAndReadCalendarFiles : " + e.getMessage(), e);
e.printStackTrace();
return null;
}
}

Related

slow json parsing while fetching data

I am using WordPress rest API to fetch data and I am only fetching 10 posts at a time using volley.
API Link
When I request API in the browser it gives me a result under 6-7 seconds...
But my app takes more than 15 sec to show that 10 posts.
Sometimes it takes more than 1 minute to show the data.
I even added a parameter to get only 2 posts per page. But the response time is the same.
I am searching and modifying code from 2 days but can't get rid of it.
Here is the code:-
private void jsonrequest(int pageNo) {
JSON_URL = "https://jamuitoday.com/wp-json/wp/v2/posts?_embed&page=" + pageNo;
JsonArrayRequest request = new JsonArrayRequest(JSON_URL, response -> {
JSONObject jsonObject;
for (int i = 0; i < response.length(); i++) {
try {
isLoading = true;
isNewsLoaded = false;
jsonObject = response.getJSONObject(i);
News news = new News();
//Get News Link
String link = jsonObject.getString("link");
news.setLink(link);
//Set Date
String date = jsonObject.getString("date");
String d = date.substring(0, 10);
int time = Integer.parseInt(date.substring(11, 13));
if (time > 12) {
String ti = date.substring(13, 16);
String dt = d + " " + (time - 12) + ti + " PM";
news.setdate(dt);
} else {
String ti = date.substring(11, 16);
String dt = d + " " + ti + " AM";
news.setdate(dt);
}
//Set Title
JSONObject title = jsonObject.getJSONObject("title");
String titleren = title.getString("rendered");
news.setName(titleren);
//Set Description
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
JSONObject desc = jsonObject.getJSONObject("content");
String descSub = desc.getString("rendered");
news.setDescription(String.valueOf(Html.fromHtml((descSub))));
news.setAdImgUrl(descSub);
// Set Image
JSONObject img = new JSONObject(jsonObject.getString("_embedded"));
JSONArray ttt = img.getJSONArray("wp:featuredmedia");
String s = "";
for (int t = 0; t < ttt.length(); t++) {
JSONObject obj = ttt.getJSONObject(t);
s = obj.getString("source_url");
}
news.setImage_url(s);
listNews.add(news);
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(listNews);
isLoading = false;
isNewsLoaded = true;
pageNumber += 1;
if (recyclerView.getLayoutManager() != null) {
recyclerView.getLayoutManager().scrollToPosition(currentListPosition);
}
if (shimmerFrameLayout.isShimmerVisible()) {
shimmerFrameLayout.stopShimmer();
shimmerFrameLayout.setVisibility(View.GONE);
}
if (swipyRefreshLayout.isRefreshing()) {
swipyRefreshLayout.setRefreshing(false);
}
}, error -> {
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(request);
}
Any hack that can speed it up?
I'll implement SQLite to store data and show at app startup. So, the user can't feel the load time.

how to create new directory everyday having its name as current date

Date dir = new java.util.Date(System.currentTimeMillis());
String baseDir = "/home/gaurav/usr/logs/ESBegin/";
String newDir = createDateBasedDirectory(baseDir, dir);
Logger logger = Logger.getLogger("MyLog1");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler(newDir+"/data.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info(stringifiedJson);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this will create a folder of todays date but i want to create new folder for everyday and store the log file in new folder....means everyday's folder must have that day's log file
i have following method to create a folder
public static String createDateBasedDirectory(String baseDirectory, Date argDate) {
String newDir = null;
if (baseDirectory != null && argDate != null) {
try {
String format = "yyyy-MM-dd";
DateFormat dateFormatter = new SimpleDateFormat(format);
String date = dateFormatter.format(argDate);
File f = new File(baseDirectory);
File files[] = f.listFiles();
String dir = null;
int baseDirLength = baseDirectory.length();
int checkingLength = baseDirLength + format.length() + 3;
int maxSeqNo = 0;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
dir = files[i].toString();
if (dir.length() == checkingLength) {
String existingDirDate = dir.substring(baseDirLength, baseDirLength + 10);
if (date.equalsIgnoreCase(existingDirDate)) {
int dirSeqNo =
Integer.parseInt(dir.substring(baseDirLength + 11, baseDirLength + 10 + 3));
if (dirSeqNo > maxSeqNo) {
maxSeqNo = dirSeqNo;
}
}
}
}
}
String currSeq = "" + (maxSeqNo + 1);
if (currSeq.length() == 1) {
currSeq = "0" + currSeq;
}
newDir = baseDirectory + date;
new File(newDir).mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
return newDir;
}
what should i change if in want to create a new folder everyday
You can use the functionality of your logging framework to do so. For example use Log4J's RollingFileAppender.
You can use the fileName parameter to create new directories. From the documentation:
fileName: The name of the file to write to. If the file, or any of its parent directories, do not exist, they will be created.
Check if the directory for the current day exists, then create it if it doesn't exist or simply return it if it does exist.
/*
* to make sure everyone knows what's going on
* name this method like getOrCreateDailyLogDirectory
*/
public static String createDateBasedDirectory(String baseDirectory, Date argDate) {
String newDir = null;
if (baseDirectory != null && argDate != null) {
try {
String format = "yyyy-MM-dd";
DateFormat dateFormatter = new SimpleDateFormat(format);
String date = dateFormatter.format(argDate);
// check if the directory exists:
String todaysLogDir = baseDirectory + "\\" + date; // create the path as String
// then create a Path (java.nio, alternatives possible)
Path todaysDirectoryPath = Paths.get(todaysLogDir);
// and check if this Path exists
if (Files.exists(todaysDirectoryPath) {
// if present, just return it in order to write (into) a log file there
return todaysDirectoryPath.toUri().toString();
} else {
// create it the way you want and return the path as String
...
}
...
} catch (Exception e) {
e.printStackTrace();
}
}
return newDir;
}
Doing it this (or a similar) way always returns the directory for the current day and creates it before once a day on first log attempt.

Java Mail API: Convert Message to String?

I am using the following code to successfully retrieve messages from my Gmail account.
// Import Statements
public class ConfirmEmail {
WebDriver driver;
Folder inbox;
String gmailID = "xxxxxxxxxxx#gmail.com";
String gmailPass = "xxxxxxxx";
String storeMessage;
public ConfirmEmail()
{
}
public void MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, gmailID,
gmailPass);
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = 0; i < msgs.length; i++)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
}
With this code I am successfully able to print messages to console. Works flawlessly 100% of the time.
However, I need to store the "bodyPart" (i.e, the actual message or body of message) in a String so I could search the String using Regex. I need to extract links begining with http.
How can I convert the message to a string?
Thanks
I'm not quite sure what you are asking (because you said you already print out your Messages ... so when you print them, why can't you store them in a String?)
if you realy just want the bodyPart stored in a String variable:
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
String content = bp.getContent().toString();

javamail cannot read multipart/mixed mails

I am unable to retrieve the content of the body of "multipart/MIXED" mails ...
This is what I use to reading mails ....
private String read(Message message) throws MessagingException, IOException {
String result = message.getContentType().toString() + " Unable to read";
if (message instanceof MimeMessage) {
MimeMessage m = (MimeMessage) message;
Object contentObject = m.getContent();
if (contentObject instanceof Multipart) {
BodyPart clearTextPart = null;
BodyPart htmlTextPart = null;
Multipart content = (Multipart) contentObject;
int count = content.getCount();
for (int i = 0; i < count; i++) {
BodyPart part = content.getBodyPart(i);
if (part.isMimeType("text/plain")) {
clearTextPart = part;
break;
} else if (part.isMimeType("text/html")) {
htmlTextPart = part;
}
}
if (clearTextPart != null) {
result = (String) "<html><body>"
+ clearTextPart.getContent() + "</body></html>";
} else if (htmlTextPart != null) {
String html = (String) htmlTextPart.getContent();
result = android.text.Html.fromHtml(html).toString();
}
} else if (contentObject instanceof String) {
String html = (String) contentObject;
result = html;
} else {
result = "not found";
}
}
return result;
}
As you can see the problem is , the mail wont even pass the condition (message instanceof MimeMessage) ...
The output for "multipart/MIXED" mail is :
multipart/MIXED; boundary=20cf306..... Unable to read
public static String processBody( > Part p < , String operacao)
} else if (p.isMimeType("multipart/*")) {
*** MimeMessage m = (MimeMessage) p; ***
Multipart mp = (Multipart) m.getContent();
for (int i = 0; i < mp.getCount(); i++) {
t = processBody(mp.getBodyPart(i), operacao);
if (t != null)
return t;
}
}
You haven't provided enough detail. Certainly you can trace the flow of your program through that code. What exactly is happening? If it's failing one of the tests, what are the actual values you're seeing and what do you expect?
What version of JavaMail are you using?
Are you reading the message from a mail server? What does the debug output show?

how to send attachment using sendgrid mail api

I need to send a mail with attachment in java using sendgrid api. I am mentioning files like [filename.xls]=bytearray. I am getting this byte array using jxl api. I am able to send the mail ,but xls is not having the values.
Code :
sendgridUrl+"?api_user="+apiUser + "&api_key=" + apiPassword + ""
+ to + "&subject=" + (subject.replaceAll(" ", "%20")) + "&fromname="
+ senderPersonal + "&files[" + fileName + "]=" + value2
+ "&html=" + mess‌​age + "&from=" + AppConfig.getProperty("senderemail");
private static bool SendMailUsingSendgrid(EmailSendRequestDTO request)
{
try
{
string htmlContent = string.Empty;
string plainTextContent = string.Empty;
// Set apikey
var apiKey = ConfigReader.Read(CommonEnums.ConfigKeys.SendgridApiKey);
// Initialize sendgrid client.
var client = new SendGridClient(apiKey);
// From address
var from = new EmailAddress(request.FromAddress, request.FromDisplayName);
// Subject
var subject = request.Subject;
// Send mail to multiple recepients.
var recepientList = new List<EmailAddress>();
if (!string.IsNullOrEmpty(request.ToAddresses))
{
foreach (var toAddress in request.ToAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
recepientList.Add(new EmailAddress(toAddress));
}
}
// If content in html format
if (request.IsHtmlFormat)
{
htmlContent = request.Body;
}
else
{
plainTextContent = request.Body;
}
// Creat object to send mail message.
var mailMessage = MailHelper.CreateSingleEmailToMultipleRecipients(from, recepientList, subject, plainTextContent, htmlContent);
// add multiple cc.
var ccList = new List<EmailAddress>();
if (!string.IsNullOrEmpty(request.CCAddresses))
{
foreach (var ccAddress in request.CCAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
ccList.Add(new EmailAddress(ccAddress));
}
}
// add multiple bcc.
var bccList = new List<EmailAddress>();
if (!string.IsNullOrEmpty(request.BCCAddresses))
{
foreach (var bccAddress in request.BCCAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
bccList.Add(new EmailAddress(bccAddress));
}
}
// Attachments
if (!string.IsNullOrEmpty(request.AttachmentFile))
{
foreach (var attachmentFile in request.AttachmentFile.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
SendGrid.Helpers.Mail.Attachment messageAttachment = new SendGrid.Helpers.Mail.Attachment();
mailMessage.Attachments.Add(messageAttachment);
}
}
var response = client.SendEmailAsync(mailMessage).Result;
return true;
}
catch (Exception)
{
return false;
}
}

Categories

Resources