2013-04-20 103 views
28

我想用java和g郵件發送電子郵件我已經將我的文件存儲在雲上以及我想作爲附件發送給我的郵件的存儲文件中。使用Java發送郵件附件

它應該將該文件添加到此郵件中,而不是這些文件的鏈接。

我如何發送這樣的附件?

+0

您必須能夠從雲端拉下來的文件在你的代碼。之後,只需附加它們 – 2013-04-20 06:41:13

+0

示例 http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm – 2016-04-26 11:54:38

+0

使用此API:https://sourceforge.net/projects/easymail4j/ – 2016-12-27 17:17:59

回答

43

工作的代碼,我用Java郵件1.4.7罐子

import java.util.Properties; 
import javax.activation.*; 
import javax.mail.*; 

public class MailProjectClass { 

public static void main(String[] args) { 

    final String username = "[email protected]"; 
    final String password = "your.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("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Testing Subject"); 
     message.setText("PFA"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 
     String file = "path of file to be attached"; 
     String fileName = "attachmentName"; 
     DataSource source = new FileDataSource(file); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(fileName); 
     multipart.addBodyPart(messageBodyPart); 

     message.setContent(multipart); 

     System.out.println("Sending"); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+0

它引發連接超時異常 – 2013-09-05 08:58:33

+0

我有「530 5.7.0必須首先發出一個STARTTLS命令。pa5sm839428pdb.28 - gsmtp」@NINCOMPOOP – 2015-01-07 07:28:34

+0

嘗試添加:'props.put(「mail.smtp.EnableSSL.enable」 ,「true」);' – NINCOMPOOP 2015-01-09 09:17:43

0

使用Spring框架,您可以添加多個附件:

package com.mkyong.common; 


import javax.mail.MessagingException; 
import javax.mail.internet.MimeMessage; 

import org.springframework.core.io.FileSystemResource; 
import org.springframework.mail.MailParseException; 
import org.springframework.mail.SimpleMailMessage; 
import org.springframework.mail.javamail.JavaMailSender; 
import org.springframework.mail.javamail.MimeMessageHelper; 

public class MailMail 
{ 
    private JavaMailSender mailSender; 
    private SimpleMailMessage simpleMailMessage; 

    public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { 
     this.simpleMailMessage = simpleMailMessage; 
    } 

    public void setMailSender(JavaMailSender mailSender) { 
     this.mailSender = mailSender; 
    } 

    public void sendMail(String dear, String content) { 

     MimeMessage message = mailSender.createMimeMessage(); 

     try{ 
     MimeMessageHelper helper = new MimeMessageHelper(message, true); 

     helper.setFrom(simpleMailMessage.getFrom()); 
     helper.setTo(simpleMailMessage.getTo()); 
     helper.setSubject(simpleMailMessage.getSubject()); 
     helper.setText(String.format(
      simpleMailMessage.getText(), dear, content)); 

     FileSystemResource file = new FileSystemResource("/home/abdennour/Documents/cv.pdf"); 
     helper.addAttachment(file.getFilename(), file); 

     }catch (MessagingException e) { 
     throw new MailParseException(e); 
     } 
     mailSender.send(message); 
     } 
} 

要知道如何配置你的項目來處理此代碼,完整閱讀this tutorial

11

對於ü不知道的原因,當我發送電子郵件到我的Gmail地址時,接受的答案部分起作用。我有附件,但沒有電子郵件的文字。

如果你想附着和文本嘗試此基礎上公認的答案:

Properties props = new java.util.Properties(); 
    props.put("mail.smtp.host", "yourHost"); 
    props.put("mail.smtp.port", "yourHostPort"); 
    props.put("mail.smtp.auth", "true");    
    props.put("mail.smtp.starttls.enable", "true"); 


    // Session session = Session.getDefaultInstance(props, null); 
    Session session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("user", "password"); 
       } 
       }); 


    Message msg = new MimeMessage(session); 
    try { 
     msg.setFrom(new InternetAddress(mailFrom)); 
     msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); 
     msg.setSubject("your subject"); 

     Multipart multipart = new MimeMultipart(); 

     MimeBodyPart textBodyPart = new MimeBodyPart(); 
     textBodyPart.setText("your text"); 

     MimeBodyPart attachmentBodyPart= new MimeBodyPart(); 
     DataSource source = new FileDataSource(attachementPath); // ex : "C:\\test.pdf" 
     attachmentBodyPart.setDataHandler(new DataHandler(source)); 
     attachmentBodyPart.setFileName(fileName); // ex : "test.pdf" 

     multipart.addBodyPart(textBodyPart); // add the text part 
     multipart.addBodyPart(attachmentBodyPart); // add the attachement part 

     msg.setContent(multipart); 


     Transport.send(msg); 
    } catch (MessagingException e) { 
     LOGGER.log(Level.SEVERE,"Error while sending email",e); 
    } 
+1

此修改適用於我。我正確地獲取了附件,但未收到任何電子郵件正文文本。使用完整的代碼@amdev,我收到了正文和附件的電子郵件。謝謝! – snowmanjack 2017-04-19 19:00:22

+0

贊同上面的評論,這應該是被接受的答案! – roz 2017-11-30 23:01:37