简体   繁体   中英

Java send email with file in attachment

i want to send a file in attachment using java. I have two classes, one where the file location is specified and the second class is used as utility class to send the email So when i execute the first class it does not send the email.

First class:

public class SendFile {
    private static String[] args;

    public static void sendEmail(File filetosend) throws IOException, Exception{

    //public static void main(String[] args) throws IOException {

    final String username = "email0@gmail.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("email0@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("email0@gmail.com"));
        message.setSubject("Attach file Test from Netbeans");
        message.setText("PFA");

        MimeBodyPart messageBodyPart = new MimeBodyPart();

        Multipart multipart = new MimeMultipart();

        messageBodyPart = new MimeBodyPart();

        //String filetosend = ("c:\\file.txt");

        DataSource source = new FileDataSource(filetosend);
        System.out.println("The filetosend is ="+filetosend);

        messageBodyPart.setDataHandler(new DataHandler(source));
        System.out.println("The source is ="+source);

        messageBodyPart.attachFile(filetosend);
        System.out.println("The file name is ="+messageBodyPart.getFileName());

        multipart.addBodyPart(messageBodyPart);
        System.out.println("The message body part is ="+messageBodyPart);

        message.setContent(multipart);
        System.out.println("The message multi part is ="+multipart);


        System.out.println("Sending");

        Transport.send(message);
        System.out.println("The message is ="+message);

        System.out.println("Done");

    } catch (MessagingException e) {
        e.printStackTrace();
    }
  }
}

And the second class:

import java.io.File;
import java.io.IOException;

public class Test {

    public static void main(String[] args) throws Exception {

    }
    File file;
    public void Test() throws IOException, Exception{ 
        System.out.println("Sending the file...");
        File filetosend = new File("c:\\file.txt");
        SendFile.sendEmail(filetosend);
    }
}

There is no error but the file is not sent. Any help please, thank you

Your code looks fine. In fact, I use almost the exact same code to send messages with attachments. You should probably look at whether or not you need to add authentication to the Transport and connect using the host, port, auth id, and auth pass. Also, check for any firewalls blocking messages with attachments (incredibly common issue).

If you look at this post Stack Overflow post on sending multiple attachments

You will see that nearly the same thing is done, and it gives an example on how to send the message with authentication.

Your code is wrong. If you copied it from somewhere, the original is wrong too, or you copied it wrong. This is what you want:

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("email0@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("email0@gmail.com"));
    message.setSubject("Attach file Test from Netbeans");

    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("PFA");

    attachmentBodyPart = new MimeBodyPart();

    System.out.println("The filetosend is ="+filetosend);
    System.out.println("The source is ="+source);

    attachmentBodyPart.attachFile(filetosend);
    System.out.println("The file name is ="+attachmentBodyPart.getFileName());

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

    message.setContent(multipart);
    System.out.println("The message multi part is ="+multipart);

    System.out.println("Sending");

    Transport.send(message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM