JavaMail API 發送電子郵件帶有附件

下面是一個例子從你的機器上選擇附件發送電子郵件。放置到 /home/manisha/ 在本地機器上的 file.txt文件。在這裏,我們使用JangoSMPT服務器通過該電子郵件被髮送到我們的目標電子郵件地址。 

要發送電子郵件,其中包含一個內嵌的圖像,遵循的步驟是:

  • 獲得一個會話 Session

  • 創建一個默認  MimeMessage 對象,並設置發件人,收件人,主題(From, To, Subject)在消息中。

  • 實際的消息如下:

    messageBodyPart.setText("This is message body");

  • 創建一個MimeMultipart的對象。添加上述messageBodyPart與實際的消息在其設置,這個多重對象。

  • 接下來創建一個DataHandler的如下添加附件:

    messageBodyPart = new MimeBodyPart(); String filename = "/home/manisha/file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart);

  • 接下來設置多重如下消息中:

    message.setContent(multipart);

  • 發送使用傳輸對象的消息。

創建Java類

創建一個Java類文件SendAttachmentInEmail,其內容如下:

package com.yiibai; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class SendAttachmentInEmail { public static void main(String[] args) { // Recipient's email ID needs to be mentioned. String to = "destinationemail@gmail.com"; // Sender's email ID needs to be mentioned String from = "fromemail@gmail.com"; final String username = "manishaspatil";//change accordingly final String password = "******";//change accordingly // Assuming you are sending email through relay.jangosmtp.net String host = "relay.jangosmtp.net"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "25"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Now set the actual message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "/home/manisha/file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException e) { throw new RuntimeException(e); } } }

由於我們使用的是由主機提供商JangoSMTP提供的SMTP服務器,我們需要驗證用戶名和密碼。javax.mail.PasswordAuthentication類用於驗證的密碼。

編譯並運行

現在,我們班是準備好了,讓我們編譯上面的類。我已經保存了類SendAttachmentInEmail.java到目錄:/home/manisha/JavaMailAPIExercise.我們需要的jar文件:javax.mail.jar和activation.jar文件在classpath。執行下面的命令從命令提示符編譯類(兩個jar被放置在/home/manisha/ 目錄下): 

javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendAttachmentInEmail.java

現在,這個類被編譯,執行下面的命令來運行:

java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: SendAttachmentInEmail

驗證輸出

你應該看到下面的消息命令控制檯上:

Sent message successfully....

因爲我通過JangoSMTP發送郵件到我的Gmail地址,下面的郵件會在我的Gmail帳戶的收件箱中接收:

JavaMail