I have a application, which accepts xml files, upon uploading a invalid xml file it sends a mail with brief information about the file. I also need to include the entire file content Update (attachment), what should be ideal way to do this.
This is what I tried (test code)
InputStream input = new BufferedInputStream(new FileInputStream("C:\\Simon\\csmclientbenz.xml"))
byte[] buffer = new byte[8192]
try {
for (int length = 0; (length = input.read(buffer)) != -1;) {
System.out.write(buffer, 0, length)
}
} finally {
input.close()
}
but currently it only prints part of the file. The xml file can be as large as 6MB. What is it that I am missing? Thanks
Update
Sending mail code
public void sendMail(mailProp,mailData)
{
logger.debug("sendMail method")
Properties properties = System.getProperties()
properties.setProperty("mail.smtp.host", mailProp.host)
Session session = Session.getDefaultInstance(properties)
try{
MimeMessage message = new MimeMessage(session)
message.setFrom(new InternetAddress(mailProp.from))
message.addRecipient(Message.RecipientType.TO,new InternetAddress(mailProp.to))
message.setSubject("Configuration failed to upload for simon@abc.com")
message.setContent(createMessage(mailData), "text/html")
Transport.send(message)
System.out.println("Sent message successfully....")
}catch (MessagingException mex) {
mex.printStackTrace()
}
}
As xml file can be huge upto 6 MB, sending entire file contents in message of email is not a good idea, its better to send as an email attachment. You may refer http://www.roseindia.net/javamail/SendAttachment.shtml to check how to send an attahcment while sending email.
Even better you can compress this xml file before sending it as an attachment to reduce the attachment size using the zip uitlities of java (Ref: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/package-summary.html)