Java发送带有附件的文件的电子邮件 [英] Java send email with file in attachment

查看:89
本文介绍了Java发送带有附件的文件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java发送附件文件. 我有两个类,一个类指定了文件位置,第二个类用作实用程序类来发送电子邮件 因此,当我执行第一堂课时,它不会发送电子邮件.

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.

头等舱:

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

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

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

推荐答案

您的代码错误.如果您是从某个地方复制的,则原件也是错误的,或者您复制的是错误的.这就是您想要的:

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

这篇关于Java发送带有附件的文件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆