Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

dean tomlinson
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("", 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) );
System.out.println("sendEmailWithAttachment - 4");

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

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

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

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

// Put parts in message

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

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

[ June 13, 2002: Message edited by: dean tomlinson ]
Stu Glassman
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic