Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PDF File corrupted when being sent by email by tomcat... what's wrong?

843834Sep 26 2008 — edited Feb 18 2010
Hi,

I have been a little frustrated trying tomcat to send a pdf file by email after it have been uploaded to the server.

First case:
If i run the same code in windows, it works.

Second case:
If i run the same code manually in java in the tomcat linux server it works.

Third case:
The problem is when the code is running by tomcat in a jsp page.

In every situation the email goes to the sender, but in the last one, the reported size of the pdf file is not the same as the first and second case resulting in a pdf corruption. It cannot be opened.

I am running Tomcat 5.5.17 with jdk 1.5.0.10 in a Redhat Enterprise Linux 4 Server.
This is the code called by a jsp page.
package eor;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMailUsingAuthentication {
private static final String SMTP_AUTH_USER = "user";
private static final String SMTP_AUTH_PWD = "pass";



public void sendEmail() {
String from = "test@example.org";
String to = "testsender@gmail.com";
String subject = "Sendit from linux ";
String bodyText = "This is a important message with attachment";
String filename = "//home//test//HON-SOLMANT28AGO08.pdf";

Properties properties = new Properties();
properties.put("mail.smtp.host", "Testmailhost");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(properties, auth);
session.setDebug(true);

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());

//
// Set the email message text.
//
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(bodyText);

//
// Set the email attachment file
//
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);

message.setContent(multipart);

Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {

@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
I will appreciate your help to solve this situation.

Regards,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 18 2010
Added on Sep 26 2008
13 comments
1,224 views