0

I have an application where i have to generate a file either it could be an txt file. All i want is to send some reports over mail either by mail or attachment in mail.

please help me how to do this?

The data format is something like a tabular format.

//create a multi part
            Multipart mp = new Multipart();

            //data for the content of the file
            String fileData = "<html>just a simple test</html>";
            String messageData = "Mail Attachment Demo";

            //create the file
            SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"text/html","file.html",fileData.getBytes());

            TextBodyPart tbp = new TextBodyPart(mp,messageData);

            //add the file to the multipart
            mp.addBodyPart(tbp);
            mp.addBodyPart(sap);

            //create a message in the sent items folder
            Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

            Message message = new Message(folders[0]);

            //add recipients to the message and send
            try {
                 Address toAdd = new Address("nilanchala_p@spanservices.com","Nilanchala");
                 Address toAdds[] = new Address[1];
                 toAdds[0] = toAdd;
                 message.addRecipients(Message.RecipientType.TO,toAdds);
                 message.setContent(mp);

                 Transport.send(message);
            } catch (Exception e) {
                 Dialog.inform(e.toString());
            }
Nilanchala
  • 5,891
  • 8
  • 42
  • 72
  • what is the error? Exception? – Sherif elKhatib Aug 01 '11 at 10:08
  • no error or exception but the mail it goes as an attachment. and while opening attachment it doesn't appears like "just a simple test" as expected. also how can i create a table format. Please tell me where i can change in my code. – Nilanchala Aug 01 '11 at 10:14
  • as you said to integrate as a part of html. how can i do it. please bare my questions even if it is stupid. – Nilanchala Aug 01 '11 at 10:16

1 Answers1

3

The best way is to send the table using plain text with html formatting

<table border="1">
    <tr>
        <td>row 1, cell 1</td>
        <td>row 1, cell 2</td>
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
        </tr>
</table> 

No need for a file... Just format and send

Ok try this:

//create a multi part
            Multipart mp = new Multipart();

            //data for the content of the file
            String fileData = "<html>just a simple test</html>";

            //create the file
            TextBodyPart tbp = new TextBodyPart(mp,fileData );

            //add the file to the multipart
            mp.addBodyPart(tbp);

            //create a message in the sent items folder
            Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

            Message message = new Message(folders[0]);

            //add recipients to the message and send
            try {
                 Address toAdd = new Address("nilanchala_p@spanservices.com","Nilanchala");
                 Address toAdds[] = new Address[1];
                 toAdds[0] = toAdd;
                 message.addRecipients(Message.RecipientType.TO,toAdds);
                 message.setContent(mp);

                 Transport.send(message);
            } catch (Exception e) {
                 Dialog.inform(e.toString());
            }
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106