This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes problems adding attachements to Javamail with Outlook Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "problems adding attachements to Javamail with Outlook" Watch "problems adding attachements to Javamail with Outlook" New topic
Author

problems adding attachements to Javamail with Outlook

tgbarry
Greenhorn

Joined: May 10, 2006
Posts: 1
Hi,

I'm using javamail 1.3, and trying to write a util that sends an email with a list of attachments.

When the email goes through, it puts the attachments in the message body (inline). I've tried a number of variations, including the commented out lines. Any assistance would be most welcome.

Cheers,

Tim



public static void sendEmailWithFileAttachment(String p_emailList, String p_fromUser, String p_subject, String p_msg, String[] _attachFileName) {

try {
if (p_emailList == null) return;
StringTokenizer emailAddresses = new StringTokenizer(p_emailList);
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
String smtpServer = System.getProperty("mail.host");
if (smtpServer != null)
props.setProperty("mail.host", smtpServer);
else {
props.setProperty("mail.host", SEMPRA_DEFAULT_SMTP_SERVER);
smtpServer = SEMPRA_DEFAULT_SMTP_SERVER;
}

if (p_fromUser != null) props.setProperty("mail.user", p_fromUser);


Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(p_subject);

Multipart multiPart = new MimeMultipart();

BodyPart messageBodyPart = new MimeBodyPart();
//messageBodyPart.setText(p_msg);
messageBodyPart.setContent(p_msg, "text/plain");
multiPart.addBodyPart(messageBodyPart);

if (_attachFileName != null) {
for (int i = 0; i < _attachFileName.length; i++) {
BodyPart attachmentBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(_attachFileName[i]);

File file = new File(_attachFileName[i]);
if (file.isFile()) {
int length = (int)file.length();
byte[] fileData = new byte[length];
try {
InputStream fis = new FileInputStream(file);
int ii = fis.read(fileData);
attachmentBodyPart.setDataHandler(new DataHandler(fds));
//DataSource source = new ByteArrayDataSource(fis, "application/x-any");
//attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(new File(_attachFileName[i]).getName());
//attachmentBodyPart.setDisposition(Part.ATTACHMENT);
//attachmentBodyPart.setHeader("content-type","application/pdf");
multiPart.addBodyPart(attachmentBodyPart);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
message.setContent(multiPart);

//System.out.println("addresses: "+p_emailList);
while (emailAddresses.hasMoreTokens()) {
String toAddress = emailAddresses.nextToken(LIST_DELIMITER);
//System.out.println("sending email to "+toAddress);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));

}
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch (MessagingException e) {
System.out.println("Failed sending email " + e.getMessage());
}

}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"tgbarry"

Please click on the My Profile link above and change your display name to meet the JavaRanch Naming Policy of using your real first and real last names.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
wood burning stoves
 
subject: problems adding attachements to Javamail with Outlook
 
Similar Threads
Sending file as an attachment
JavaMail API error
How to send mail to more than one recipients
JavaMail installation
How to embed image (relative path) in an HTML e-mail?