Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
messageBodyPart.setFileName("Whatever you'd like") should do the trick.
-Stu
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!