2014-09-03 42 views
0

我想用java發送附件中的文件。 我有兩個類,其中一個指定文件位置,第二個類用作工具類發送電子郵件 因此,當我執行第一個類時,它不會發送電子郵件。Java用附件中的文件發送電子郵件

第一類:

public class SendFile { 
    private static String[] args; 

    public static void sendEmail(File filetosend) throws IOException, Exception{ 

    //public static void main(String[] args) throws IOException { 

    final String username = "[email protected]"; 
    final String password = "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("Attach file Test from Netbeans"); 
     message.setText("PFA"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 

     //String filetosend = ("c:\\file.txt"); 

     DataSource source = new FileDataSource(filetosend); 
     System.out.println("The filetosend is ="+filetosend); 

     messageBodyPart.setDataHandler(new DataHandler(source)); 
     System.out.println("The source is ="+source); 

     messageBodyPart.attachFile(filetosend); 
     System.out.println("The file name is ="+messageBodyPart.getFileName()); 

     multipart.addBodyPart(messageBodyPart); 
     System.out.println("The message body part is ="+messageBodyPart); 

     message.setContent(multipart); 
     System.out.println("The message multi part is ="+multipart); 


     System.out.println("Sending"); 

     Transport.send(message); 
     System.out.println("The message is ="+message); 

     System.out.println("Done"); 

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

而第二類:

import java.io.File; 
import java.io.IOException; 

public class Test { 

    public static void main(String[] args) throws Exception { 

    } 
    File file; 
    public void Test() throws IOException, Exception{ 
     System.out.println("Sending the file..."); 
     File filetosend = new File("c:\\file.txt"); 
     SendFile.sendEmail(filetosend); 
    } 
} 

沒有錯誤,但沒有發送的文件。 請任何幫助,謝謝

+0

我發現了一個類似的問題http://stackoverflow.com/questions/16117365/sending-mail-attachment-using-java – Jaydee 2014-09-03 14:48:06

+0

的問題不是要發送的文件。我想從另一個班級獲得檔案。 – 2014-09-03 15:27:19

回答

0

您的代碼是錯誤的。如果你從某個地方複製它,原件也是錯的,或者你錯了。這是你想要什麼:

Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress("[email protected]")); 
    message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]")); 
    message.setSubject("Attach file Test from Netbeans"); 

    MimeBodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText("PFA"); 

    attachmentBodyPart = new MimeBodyPart(); 

    System.out.println("The filetosend is ="+filetosend); 
    System.out.println("The source is ="+source); 

    attachmentBodyPart.attachFile(filetosend); 
    System.out.println("The file name is ="+attachmentBodyPart.getFileName()); 

    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 
    multipart.addBodyPart(attachmentBodyPart); 

    message.setContent(multipart); 
    System.out.println("The message multi part is ="+multipart); 

    System.out.println("Sending"); 

    Transport.send(message); 
+0

現在好了,謝謝 – 2014-09-03 19:25:00

0

你的代碼看起來不錯。實際上,我幾乎使用完全相同的代碼來發送帶有附件的消息。您應該查看是否需要向Transport添加身份驗證,並使用主機,端口,身份驗證ID和身份驗證傳遞進行連接。另外,請檢查是否有防火牆阻止帶附件的消息(令人難以置信的常見問題)。

如果你看看這個帖子Stack Overflow post on sending multiple attachments

你會看到,幾乎同樣的事情做,它給出瞭如何與驗證發送消息的例子。

+0

當第二個類中指定要發送的文件時它正在工作,但是我的問題是如何從第一個類中獲取文件。 – 2014-09-03 15:03:30

+0

Whay你的意思是通過獲得頭等檔案? – tmarwen 2014-09-03 15:38:59

+0

SendFile類用作工具類,Test類用於通過SendFile類的調用方法發送文件。 – 2014-09-03 15:53:41