【问题标题】:Java send email with file in attachmentJava发送带有附件文件的电子邮件
【发布时间】:2014-09-04 02:56:39
【问题描述】:

我想使用 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);
    }
}

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

【问题讨论】:

标签: java jakarta-mail


【解决方案1】:

您的代码错误。如果您从某个地方复制它,那么原件也是错误的,或者您复制它是错误的。这就是你想要的:

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

【讨论】:

    【解决方案2】:

    您的代码看起来不错。事实上,我使用几乎完全相同的代码来发送带有附件的消息。您可能应该查看是否需要向传输添加身份验证并使用主机、端口、身份验证 ID 和身份验证通行证进行连接。此外,检查是否有任何防火墙阻止带有附件的消息(非常常见的问题)。

    如果你看这篇帖子Stack Overflow post on sending multiple attachments

    您将看到几乎相同的事情已经完成,并且它提供了一个关于如何发送带有身份验证的消息的示例。

    【讨论】:

    • 在第二类中指定要发送的文件时它可以工作,但我的问题是如何从第一类中获取文件。
    • 在头等舱获取文件是什么意思?
    • SendFile 类用作实用程序类,Test 类用于通过调用 SendFile 类的方法发送文件。
    • 我删除了Test类的第一个括号,现在它发送没有文件的电子邮件。
    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 2015-09-09
    • 2017-09-06
    • 2018-07-24
    • 2015-08-28
    相关资源
    最近更新 更多