简体   繁体   English

Java发送带有附件的电子邮件

[英]Java send email with file in attachment

i want to send a file in attachment using java. 我想使用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. 您可能应该查看是否需要向传输添加身份验证并使用主机,端口,身份验证ID和身份验证通行证进行连接。 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 如果您看这篇文章, Stack Overflow关于发送多个附件的文章

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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