• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

problems adding attachements to Javamail with Outlook

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}

}
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!