Solved

Sending Output of a Java Program as a .doc

Posted on 2011-09-19
30
294 Views
Last Modified: 2012-05-12
Hi Experts...
I have a java program whose output i want to capture as a document and send the contents of this document as the body of a mail.
How do i achieve this.
Please help...
0
Comment
Question by:gaugeta
  • 13
  • 13
  • 4
30 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36564462
You capture it as a text and then create  Word document or maybe even Excel file with  Apache POI

http://poi.apache.org/

and you should be able to format it - maybe not as flexible as in native applications but still
POI should allow it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36564465
0
 

Author Comment

by:gaugeta
ID: 36564605
@for_yan:Could you give some sample file for poi doc generation.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36564642


Check some simple code here:
http://www.mail-archive.com/tika-user@lucene.apache.org/msg00082.html

well it looks like if you want to create something sophisticated - it owuld hardly be a good thing

And why did you decide to to go away from your originla idea of creating PDF with iText - it looks like that is much more
popular thing

I think creating Excel with POI is also more used option.

Word with POI  is the least used option

Are your reports more or less uniform?
Perhpas editing some template will be easier.
0
 

Author Comment

by:gaugeta
ID: 36564717
@for_yan:Yes i was initially working on itext for pdf but was not allowed to do later.
The required format is .doc
Basically all i want in the .doc file is representing some values in form of tables and some coloured text format.
Please suggest any appropriate solution.
Thanks for the reply...
0
 

Author Comment

by:gaugeta
ID: 36564728
@for_yan:Or may be i can do all this stuff in itext and convert the pdf to .doc if its possible.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36564782


go here and read attentively
http://stackoverflow.com/questions/203174/whats-a-good-java-api-for-creating-word-documents
- there are many suggestions there

Just realize that it is not something  simple - there is definitely nothing which will do it  in two lines of code.
It is also obvious that there is nothing which has full Word functionality - luckily, you will not need full functionality,
but each option may or may not have abilities to the level which you need.
There are  some real possibilities like  to try to create RTF (which will be opened in fact by word for users) with iText
if not this, then try with POI - it is known to have some problems but depending on the complexity
of your stuff iit may work.

Well, iText and PDF was tested by many people and is definitely reasonable option
(also not something you can do in two lines, but at least many people achieved result),
but if you have to do it with Word...

With POI and Word it looks like it is much more painful road - people did create
some simple files like here:
http://www.mail-archive.com/tika-user@lucene.apache.org/msg00082.html
but there is a feeling that with a little bit more sophisticated contents there are problems.

There are also Java-Com bridges which allow to use some
parts of windows code from java - this also may be an option.

So I think that after some exploartion, if you just have not too special tables
it is possible to achieve.

All of it is rather hard work, and no absolute gurantee of 100% success, sorry...
Mention to your bosses that it is not something straightforward, maybe they would
in the end be happy with text file or for instance with HTML report which
your users will double-click and see very decent tables rendered by the browser - taht would be rather simple to do

Just Word is the  requirement, prehaps the most difficult in implementation
 




0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36564791
If you are sending reports by email - really think about having HTML formatted email -
most email programs now are of course rendering HTML formatted emails and you can have
the table right in the body of the email and such tables may look better than word -
think about it...
0
 

Author Comment

by:gaugeta
ID: 36564880
@for_yan:Thanks for the reply.
Yes my only requirement is produce some daily statictics in form of tables and some patterns occuring in data in coloured formats.
So how do i achieve the above suggestion based  on this requirement.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36564917
You use JavaMail and use method like that for sending email:

public void sendEmail(String htmlString, Vector OAEmailAddresses,
                      Vector emailAddressesCC, String userAddress, String subject)
   {


 String adrFrom = userAddress;
      String signature = "";
     String sepp = System.getProperty("line.separator");


       String body = bodyText;

   InternetAddress [] intAdd;
    InternetAddress [] intAddcc;


  intAdd = new InternetAddress[OAEmailAddresses.size()];

    intAddcc = new InternetAddress[emailAddressesCC.size()];



       try {
  for(int j=0; j<OAEmailAddresses.size(); j++)
    intAdd[j] = new InternetAddress((String)OAEmailAddresses.elementAt(j));

    for(int j=0; j<emailAddressesCC.size(); j++)
    intAddcc[j] = new InternetAddress((String)emailAddressesCC.elementAt(j));



    Properties props = new Properties();

	
	props.put("mail.smtp.host", "mailhub.company.com");
	Session session = Session.getDefaultInstance(props, null);
	    Message msg = new MimeMessage(session);
	    msg.setFrom(new InternetAddress(adrFrom));
 

	    msg.setRecipients(Message.RecipientType.TO, intAdd);
            msg.setRecipients(Message.RecipientType.CC, intAddcc);
             msg.setSubject(subject );


        msg.setSentDate(new java.util.Date());

	    msg.setText(htmlString);
           msg.setHeader("Content-Type","text/html");  // this is important header

	    Transport.send(msg);
           }
       catch (Exception e) {System.out.println("Error sending e-mail : " + e.toString()); e.printStackTrace(); }
     System.out.println("E-mail message sent!");


}

Open in new window


and form the body of your email  as html string -  something like below - and the whole power of HTML
for formatting your table will be in your hands - and the table will come in the body of your email

                           String htmlString = "";
  htmlString += "<html>" + sep;
htmlString += "<head>" + sep;

htmlString += "<title>Your title</title></head><body>";


htmlString += "<table> <tr> <td> stuff</td><td>antother cell</td> </tr>" +
"<tr> <td> stuff</td><td>antother cell</td> </tr>" +
" <tr> <td> stuff</td><td>antother cell</td> </tr></table></body></html>";

Open in new window


if your users are using Outlook they should see it as soon as they open email

0
 

Author Comment

by:gaugeta
ID: 36565172
@for_yan:Thanks a lot will try this and get back to you if i face any issues.
Thanks again.
0
 

Author Comment

by:gaugeta
ID: 36565244
@for_yan:BTW what is sep in the following statements.
 String htmlString = "";
  htmlString += "<html>" + sep;
htmlString += "<head>" + sep;

and why is the following statement used if its is not used in the sendmail function:-String body = bodyText;
Please help.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565268
Yes, I forgot to remove body

static String sep = System.getProperty("line.separator");

Just adds line feed - not imprtant for HTML
0
 

Author Comment

by:gaugeta
ID: 36565561
@for_yan:I am getting this exception at this line:Message msg = new MimeMessage(session);
Please help...
Exception in thread "main" java.lang.NoClassDefFoundError: javax.activation.DataSource
        at java.lang.J9VMInternals.verifyImpl(Native Method)
        at java.lang.J9VMInternals.verify(J9VMInternals.java:68)
        at java.lang.J9VMInternals.initialize(J9VMInternals.java:129)
        at radioAutomate.sendEmail(Automate.java:835)
        at radioAutomate.main(Automate.java:619)

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36565603
@for_yan:After importing some jars i got the below exception.
Please help...
Error sending e-mail : javax.mail.MessagingException: Unknown SMTP host: mailhub.company.com;
  nested exception is:
        java.net.UnknownHostException: mailhub.company.com
javax.mail.MessagingException: Unknown SMTP host: mailhub.company.com;
  nested exception is:
        java.net.UnknownHostException: mailhub.company.com
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1225)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
        at javax.mail.Service.connect(Service.java:236)
        at javax.mail.Service.connect(Service.java:137)
        at javax.mail.Service.connect(Service.java:86)
        at javax.mail.Transport.send0(Transport.java:150)
        at javax.mail.Transport.send(Transport.java:80)
        at radioAutomate.sendEmail(Automate.java:849)
        at radioAutomate.main(Automate.java:619)

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 40

Expert Comment

by:gurvinder372
ID: 36565616
are you behind the firewall?, or may be mailhub.company.com is not the correct SMTP server to connect
0
 

Author Comment

by:gaugeta
ID: 36565628
@gurvinder372:I guess i am.I use putty to program on unix file system.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565633
mailhub.company.com  was of cousre generic for smtp  server - you should find out what is the name of your
smtp server and use it
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565637
putyy has no connection to it; you need tpo specify smtp server
0
 

Author Comment

by:gaugeta
ID: 36565638
@for_yan & @gurvinder372:When i used the following then iagain i got the below exception.
String host = "127.0.0.1";
props.put("mail.smtp.host", host);
Please help...
Error sending e-mail : javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25;
  nested exception is:
        java.net.ConnectException: A remote host refused an attempted connect operation.
javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25;
  nested exception is:
        java.net.ConnectException: A remote host refused an attempted connect operation.
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
        at javax.mail.Service.connect(Service.java:236)
        at javax.mail.Service.connect(Service.java:137)
        at javax.mail.Service.connect(Service.java:86)
        at javax.mail.Transport.send0(Transport.java:150)
        at javax.mail.Transport.send(Transport.java:80)
        at radioAutomate.sendEmail(Automate.java:849)
        at radioAutomate.main(Automate.java:619)

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36565641
As @for_yan has mentioned above, you need to use the SMTP server to which you have the access to, and then try
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36565646
You would use 127.0.0.1 only if you are running SMTP on your own machine.
0
 

Author Comment

by:gaugeta
ID: 36565649
@or_yan:How do i find out my smtp server.
And i didnt get this comment "putyy has no connection to it; you need tpo specify smtp server "
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565650
You find out those folks who manage your email what is the name of your smtp server
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565651
You find out those folks who manage your email what is the name of your smtp server
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36565654
If you working for a company, then you admin or help desk should be giving this info to you
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565656
There is no connection between putty and your smtp servere
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36565659

you can try
smtp.gmail.com

but it may noot work - nbtter to ask your folks who manage your email
0
 

Author Comment

by:gaugeta
ID: 36565707
@for_yan:I tried smpt.gmail.com it did not work.
Will find out smtp server name and get back if i face any issues.
Thanks for the reply...
0
 

Author Comment

by:gaugeta
ID: 36586310
@for_yan:When i tried to left align the html and the table elements they appear next to each other in a clustered format instead of one below the other.

Before when not applying any alignment code they were by default appering one below the other but with center alignment.

How do i make the appear one below the other but with left alignment as well.
Please help...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now