Java使用附件中的文件发送电子邮件

时间:2014-09-03 14:29:04

标签: java javamail

我想使用java发送附件中的文件。 我有两个类,一个用于指定文件位置,另一个用作实用程序类来发送电子邮件 因此,当我执行第一个课程时,它不会发送电子邮件。

头等舱:

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();
    }
  }
}

第二节课:

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);
    }
}

没有错误,但文件未发送。 请帮忙,谢谢

2 个答案:

答案 0 :(得分:0)

您的代码看起来很好。事实上,我使用几乎完全相同的代码来发送带附件的邮件。您应该查看是否需要向传输添加身份验证并使用host,port,auth id和auth pass进行连接。另外,检查阻止带附件的邮件的任何防火墙(非常常见的问题)。

如果你看这篇文章Stack Overflow post on sending multiple attachments

你会看到几乎完成了同样的事情,它给出了一个如何通过身份验证发送消息的例子。

答案 1 :(得分:0)

你的代码错了。如果您从某处复制它,原件也是错误的,或者您复制错误。这就是你想要的:

    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);