java - 发送带有附件的电子邮件

标签 java gmail email-attachments

我有一个发送邮件的功能,但现在我需要附加一个文档,但我不知道该怎么做,或者如果我可以这样做,这是代码

public void enviar(){
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", true); // added this line
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.user", "xxxx@gmail.com");
    props.put("mail.smtp.password", "xxxx");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", true);

    Session session = Session.getInstance(props,null);
    MimeMessage message = new MimeMessage(session);
    // Create the email addresses involved
    try {
        InternetAddress from = new InternetAddress("xxxx@gmail.com");
        message.setSubject("Informe fichadas");
        message.setFrom(from);
        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("xxxx@gmail.com"));

        // Create a multi-part to combine the parts
        Multipart multipart = new MimeMultipart("alternative");

        // Create your text message part
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("some text to send");

        // Add the text part to the multipart
        multipart.addBodyPart(messageBodyPart);

        // Create the html part
        messageBodyPart = new MimeBodyPart();
        String htmlMessage = "Informe fichadas";
        messageBodyPart.setContent(htmlMessage, "text/html");
        multipart.addBodyPart(messageBodyPart);

        // Associate multi-part with message
        message.setContent(multipart);

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.gmail.com", "user", "pass");
        System.out.println("Transport: "+transport.toString());
        transport.sendMessage(message, message.getAllRecipients());


    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我如何附加文件?文件位于此路径 (C:\hola.txt)

最佳答案

添加

         // Part two as attachment
         messageBodyPart = new MimeBodyPart();
         String filename = "c:\hola.txt";
         DataSource source = new FileDataSource(filename);
         messageBodyPart.setDataHandler(new DataHandler(source));
         messageBodyPart.setFileName(filename);
         multipart.addBodyPart(messageBodyPart);

正上方

 // Associate multi-part with message
         message.setContent(multipart);

来源:http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm

关于java - 发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31013775/

相关文章:

c# - 超出存储分配。服务器响应为 : 5. 3.4 消息大小超过固定的最大消息大小

php - 在 Laravel 5.4 中通过电子邮件发送 PDF 文档作为附件

java - 如何编写 Java 函数来返回 Unicode 点的标准名称?

java - MainSoft Grasshopper,.Net 到 Java

java - Kotlin + Maven 运行打包jar 找不到主类

facebook - 登录请求失败的正确 HTTP 状态代码是什么? (或者为什么 gmail/fb 响应新页面)

python - 如何使用 Python 以 Gmail 作为提供商发送电子邮件?

android - 在 Android 中发送带附件的电子邮件。适用于 Gmail 但不适用于 Outlook

java - 如何根据android/java/eclipse中textView中的文本更改imageView

php - 在 PHPMailer 中添加多个附件