1

i have a classA where i have file object and another classB to send email with attachment. What i try to do is, get the file object from the classA in classB and send it by email as attachment.

When i execute my code, it sends the file as attachment but in the email, the file has no name.

ClassA:

import java.io.File;
import javax.mail.MessagingException;

public class A{
    public void A(){
       System.out.println("Sending the file...");
       File file = new File("c:\\temp\\FileA.txt");
    }   
}

ClassB:

public class B {
    public static void B(File file) throws MessagingException {
        String host = "smtp.gmail.com";
        String Password = "***";
        String from = "***@gmail.com";
        String toAddress = "***@gmail.com";

        //Here i don t want to use this file
        //String filename = "C:/file.txt";

        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, toAddress);
        message.setSubject("Attachment TEST ");

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("coucou the file is here");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(file);

        messageBodyPart.setDataHandler(new DataHandler(source));
        //messageBodyPart.setFileName(file);

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        try {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();
        } catch (SendFailedException sfe) {
            System.out.println(sfe);
        }
    }
    public static void main(String args[]) throws MessagingException{
        B file = new B();
    }
}

What should i do to get the exact file name in my email? Thank you

tkw83
  • 185
  • 1
  • 5
samco cosma
  • 39
  • 1
  • 3
  • 11
  • what's wrong with file.getName() ? btw a better approach is in the chosen answer of this other SO question http://stackoverflow.com/questions/3177616/how-to-attach-multiple-files-to-an-email-using-javamail – BigMike Aug 27 '14 at 14:42

3 Answers3

0

I dont understand how your code ran when you have defined a method which has the name of a class (inside Class B).Constructor do not have return type

Problems with your code: -

1)The file variable is a local variable in a constructor for Class A .It should be declared as instance field

import java.io.File;
import javax.mail.MessagingException;

public class A{

    File file;
    public void A() throws IOException{

     System.out.println("Sending the file...");
     file = new File("c:\\temp\\FileA.txt");
    }   
}

2)You have defined a method which has the name of a class (inside Class B).Constructor do not have return type

3)Setting the name of attachment has been commented out.Uncomment it and pass a String instead of File Object .

messageBodyPart.setFileName(file.getName());

Final code for Class B(I haven't compiled it in IDE though :)

public class B {

    public B(A aa) throws MessagingException {

        File file = aa.file;

        String host = "smtp.gmail.com";
        String Password = "***";
        String from = "***@gmail.com";
        String toAddress = "***@gmail.com";

        //Here i don t want to use this file
        //String filename = "C:/file.txt";

        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.setRecipients(Message.RecipientType.TO, toAddress);

        message.setSubject("Attachment TEST ");

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("coucou the file is here");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(file);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(file.getName());

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        try {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();

        } catch (SendFailedException sfe) {

            System.out.println(sfe);
        }
    }
    public static void main(String args[]) throws MessagingException{

        A aaa = new A();
        B file = new B(aaa);
    }
}
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
0

Change this line:

//messageBodyPart.setFileName(file);

for this:

messageBodyPart.setFileName(file.getName());

I also don't entirely understand the structure of your code...

Hope it helps you

troig
  • 7,072
  • 4
  • 37
  • 63
0

First, use the MimeBodyPart.attachFile method, it's much simpler:

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("coucou the file is here");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(file);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);

Second, to put your main method in A:

public class A {
    public static void main(String[] args) throws Exception {
         // whatever
    }
    // other methods, if needed...
}
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40