2013-04-20 7 views
28

私はjavaとgメールを使用して電子メールを送信しようとしています。私は自分のファイルをクラウドに保存しました。Javaを使用してメール添付を送信する

ファイルをこのメールに追加し、そのファイルのリンクは追加しないでください。

どのように添付ファイルを送信できますか?に対処するためのプロジェクトを構成する方法を知るために

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

:コードを作業する

+0

あなたはあなたのコード内のクラウドからファイルをプルダウンすることができなければなりません。その後、ちょうど –

+0

サンプル http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm –

+0

それらを添付して、このAPIを使用します。https://sourceforge.net/projects/easymail4j/ –

答えて

43

が、私はJavaのメール1.4.7のjar春のフレームワークを使用して

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

それは接続タイムアウト例外 –

+0

を上げ、私が持っています"5.7.0 STARTTLSコマンドを最初に発行する必要があります。pa5sm839428pdb.28 - gsmtp" @NINCOMPOOP –

+0

これを追加してください: 'props.put(" mail.smtp.EnableSSL.enable " 、 "true"); ' – NINCOMPOOP

0

を使用している、あなたは多くの添付ファイルを追加することができますこのコードは、完全な読書this tutorialです。

-2

まずあなたがabosoluteパス

を使用してメールにこのファイルを添付ADFフレームワーク(任意のJavaベースのフレームワークのために同じコードワーク)

http://www.awasthiashish.com/2013/06/download-file-from-url-using-oracle-adf.html

でそれを行う方法を参照 クラウドからファイルをダウンロードする必要が

http://www.awasthiashish.com/2013/04/gmail-integration-with-oracle-adf-using.html

Uについてはアシシュ

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

+0

は上記のコメントに同意します。 – roz

関連する問題