• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

Sending Output of a Java Program as a .doc

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
gaugeta
Asked:
gaugeta
  • 13
  • 13
  • 4
1 Solution
 
for_yanCommented:
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
 
for_yanCommented:
0
 
gaugetaAuthor Commented:
@for_yan:Could you give some sample file for poi doc generation.
Please help...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
for_yanCommented:


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
 
gaugetaAuthor Commented:
@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
 
gaugetaAuthor Commented:
@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
 
for_yanCommented:


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
 
for_yanCommented:
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
 
gaugetaAuthor Commented:
@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
 
for_yanCommented:
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
 
gaugetaAuthor Commented:
@for_yan:Thanks a lot will try this and get back to you if i face any issues.
Thanks again.
0
 
gaugetaAuthor Commented:
@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
 
for_yanCommented:
Yes, I forgot to remove body

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

Just adds line feed - not imprtant for HTML
0
 
gaugetaAuthor Commented:
@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
 
gaugetaAuthor Commented:
@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
 
Gurvinder Pal SinghCommented:
are you behind the firewall?, or may be mailhub.company.com is not the correct SMTP server to connect
0
 
gaugetaAuthor Commented:
@gurvinder372:I guess i am.I use putty to program on unix file system.
Please help...
0
 
for_yanCommented:
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
 
for_yanCommented:
putyy has no connection to it; you need tpo specify smtp server
0
 
gaugetaAuthor Commented:
@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
 
Gurvinder Pal SinghCommented:
As @for_yan has mentioned above, you need to use the SMTP server to which you have the access to, and then try
0
 
Gurvinder Pal SinghCommented:
You would use 127.0.0.1 only if you are running SMTP on your own machine.
0
 
gaugetaAuthor Commented:
@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
 
for_yanCommented:
You find out those folks who manage your email what is the name of your smtp server
0
 
for_yanCommented:
You find out those folks who manage your email what is the name of your smtp server
0
 
Gurvinder Pal SinghCommented:
If you working for a company, then you admin or help desk should be giving this info to you
0
 
for_yanCommented:
There is no connection between putty and your smtp servere
0
 
for_yanCommented:

you can try
smtp.gmail.com

but it may noot work - nbtter to ask your folks who manage your email
0
 
gaugetaAuthor Commented:
@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
 
gaugetaAuthor Commented:
@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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 13
  • 13
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now