Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

attachment with message....

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i am having problem with sending mails with attachment and message....

i'm able to send mail with message and no attachment and also with attachment and no message...but not both at the same time...

here's the code...

import java.util.Properties;

import javax.mail.Session;
import javax.mail.Message;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;

public class EmailNotification {

public EmailNotification() {


public static void sendMail(String to,String from, String subject, String message, String attachmentFileName){
Properties props = new Properties();

String host = "<host name>";


Session session = Session.getDefaultInstance(props, null);

Message msg = new MimeMessage(session);

try {
msg.setFrom(new InternetAddress(from));

InternetAddress[] address =null ;
msg.setRecipients(Message.RecipientType.TO, address);

BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();


DataSource source = new FileDataSource(attachmentFileName);
messageBodyPart.setDataHandler(new DataHandler(source));
String fileApper = attachmentFileName.substring(attachmentFileName.lastIndexOf("\\")+1);


catch(AddressException ae){

catch(MessagingException me) {


Posts: 27211
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you downloaded JavaMail then you have a "demo" directory that contains example code. You will find an example of what you want to do in that directory, I think. You need separate parts for your body text and each of your attachments.
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic