• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Attaching a file using JavaMail, without displaying the full file path

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to attach a fiel to an email using JavaMail. This all works fine, but the file displays it's full file path name in the sent email.
the only way i have been able to get around this is if i attach a file that exists in user.dir.
unfortunately this is not practical, becasue i am trying to to use this in a web application, and the fie tht is created needs to be in a directroy exclusive to each web client.
can anyone help ?

// Get system properties
Properties props = System.getProperties();
System.out.println("sendEmailWithAttachment - 1");

// Setup mail server
props.put("mail.smtp.host", smtpServer);
System.out.println("sendEmailWithAttachment - 2");

// Get session
//Session session = Session.getInstance(props, null);
Session session = Session.getDefaultInstance(props, null);
System.out.println("sendEmailWithAttachment - 3");

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom( new InternetAddress(msgFrom) );
message.addRecipient( Message.RecipientType.TO, new InternetAddress(msgTo) );
message.setSubject(msgSubject);
System.out.println("sendEmailWithAttachment - 4");

// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
System.out.println("sendEmailWithAttachment - 5");

//fill message
messageBodyPart.setText(msgBody);
System.out.println("sendEmailWithAttachment - 6");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
System.out.println("sendEmailWithAttachment - 7");

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentName);
messageBodyPart.setDataHandler( new DataHandler(source) );
messageBodyPart.setFileName(attachmentName);
//messageBodyPart.setFileName("dt.txt");
multipart.addBodyPart(messageBodyPart);
System.out.println("sendEmailWithAttachment - 8");

// Put parts in message
message.setContent(multipart);

message.setSentDate(new Date());
System.out.println("sendEmailWithAttachment - 9");

// Send the message
Transport.send( message );


[ June 13, 2002: Message edited by: dean tomlinson ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
messageBodyPart.setFileName("Whatever you'd like") should do the trick.
-Stu
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic