2011-07-25 27 views
3

に送信します。添付ファイルを送信する必要があります。.doc、.dbまたは.fileです。だから私は次のコードを使用して、メッセージが正常に配信され、特定の添付ファイルが送信されず、受信されませんでした。ファイルを添付ファイルとしてJavaの

私のコードは次のとおりです。私が持っていたものの間違いこのコードで

import java.util.Date; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.mail.Message; 
import javax.mail.Message.RecipientType; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class SendEmail { 

    private String from = "[email protected]"; 
    private String to; 
    private String subject; 
    private String text; 
    String filename = "hardWare_Dtls.file"; 
    String host = "smtp.gmail.com"; 

    public SendEmail(String from, String to, String subject, String text,String filename) { 
     // System.out.println("From Adress inside constr"+from); 
     this.from = from; 
     this.to = to; 
     this.subject = subject; 
     this.text = text; 
     this.filename=filename; 
    } 

    public void send() { 

     Properties props = System.getProperties(); 
     System.out.println("Email Options SendEmail"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", from); 
     props.put("mail.smtp.password", "123456"); 
     props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo 
              // mail 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 

     Session mailSession = Session.getDefaultInstance(props, null); 
     Message simpleMessage = new MimeMessage(mailSession); 

     InternetAddress fromAddress = null; 
     InternetAddress toAddress = null; 
     try { 
      fromAddress = new InternetAddress(from); 
      toAddress = new InternetAddress(to); 

     } catch (AddressException e) { 
      // TODO Auto-generated catch block 
      // e.printStackTrace(); 
      System.out.println("Address Exception" + e.getMessage()); 
     } 

     try { 
      System.out.println("From Address" + fromAddress); 
      simpleMessage.setFrom(fromAddress); 
      simpleMessage.setRecipient(RecipientType.TO, toAddress); 
      System.out.println("To Address" + toAddress); 
      simpleMessage.setSubject(subject); 
      simpleMessage.setText(text); 
      simpleMessage.setSentDate(new Date()); 

      MimeBodyPart attachmentPart = new MimeBodyPart(); 
       FileDataSource fileDataSource = new FileDataSource(filename) { 
        @Override 
        public String getContentType() { 
         return "application/octet-stream"; 
        } 
       }; 
       attachmentPart.setDataHandler(new DataHandler(fileDataSource)); 
       attachmentPart.setFileName(filename); 

       Multipart multipart = new MimeMultipart(); 
       // multipart.addBodyPart(messagePart); 
       multipart.addBodyPart(attachmentPart); 


      // simpleMessage.setText(attachment); 

      Transport transport = mailSession.getTransport("smtps"); 
      transport.connect("smtp.gmail.com", "[email protected]", 
        "123456"); 
      simpleMessage.saveChanges(); 
      transport.sendMessage(simpleMessage, 
        simpleMessage.getAllRecipients()); 
      Transport.send(simpleMessage); 

      transport.close(); 
      // Transport.send(simpleMessage); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      // e.printStackTrace(); 
      System.out.println("Messagine Exception" + e.getMessage()); 
     } 
    } 

} 

。 Javacodeを使用して添付ファイル付きのメッセージを送信するための他のコードがあれば、親切に私を送ってください。前もって感謝します。

は、私は私のメインクラスでこのコードを使用します。

  String from = "[email protected]"; 
     String to = "[email protected]"; 
     String subject = "Sample Text Message"; 
     String message = "Sample Msg with File attachment"; 
     String filename="hardWare_Dtls.file"; 

     SendEmail sendMail = new SendEmail(from, to, subject, message,filename); 
     sendMail.send(); 
+0

受信した未処理のメッセージを見て、その内容を確認しましたか。 –

+0

どこでマルチパートリクエストを送信していますか?あなたは単純なメッセージだけを送ります。 –

+0

マルチパートを含めるにはどうすればいいですか? – Aerrow

答えて

1

あなたほとんどすべての部分を持っていますが、あなたは(だけでなく、メッセージの)MimeMessageを使用し、あなたのマルチパートようにその内容を設定する必要があります。再び

((MimeMessage) simpleMessage).setContent(multipart); 

とテスト:その代わりの

// simpleMessage.setText(attachment); 

は、次の操作を行います。私はあなたが実際にそれを実行しようとしましたが、あなたのコードは、あなたが行をコメントアウトので、コンパイルされないだろうと思います。それは動作するはずです。

+0

私はそれを試みたが、私は例外がある。 – Aerrow

+0

例外はありますか? – Perception

+0

"Messagine Exception" ..ソースが見つからないと思います。 – Aerrow

関連する問題