2016-02-09 22 views
0

我有一個函數可以用java中的附件發送郵件。它在我上傳附件時起作用。但是,問題是如果我必須發送沒有附件的郵件,它說錯誤,當我發送郵件,我沒有上傳任何附件。在Java中發送郵件和附件時出錯

這裏是我的代碼:

Properties props = new Properties(); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.auth", "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 { 
     String html = text; 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse(email)); 
     message.setSubject(subject); 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setContent(html, "text/html"); 
     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart); 
     messageBodyPart = new MimeBodyPart(); 
     String filename = "C:/Users/gro/Desktop/"+attachment; 
     DataSource source = new FileDataSource(filename); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(filename); 
     multipart.addBodyPart(messageBodyPart); 
     message.setContent(multipart); 

     Transport.send(message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     throw new RuntimeException(e); 
    } 

任何想法如何,我可以解決這個問題?在發送消息IOException異常;:

我得到這個錯誤:

org.apache.jasper.JasperException:了java.lang.RuntimeException:javax.mail.MessagingException的 嵌套的例外是: java.io.FileNotFoundException:C:\用戶\ GRO \桌面(訪問被拒絕)

+0

你會得到什麼錯誤? – SLaks

+0

org.apache.jasper.JasperException:java.lang.RuntimeException:javax.mail.MessagingException:發送消息時發生IOException; 嵌套的異常是: \t java.io.FileNotFoundException:C:\ Users \ gro \ Desktop(訪問被拒絕) – Aalm

+0

錯誤的哪部分你不明白?你爲什麼期望你的代碼工作? – SLaks

回答

0

它只是無法找到文件..

我不知道你在哪裏獲取attachment字段集。

但是,包裹的是附加的文件信息的代碼片段,這樣的事情應該下面爲你工作:

if(attachment != null && attachment.length() > 0) {  
    messageBodyPart = new MimeBodyPart(); 
    String filename = "C:/Users/gro/Desktop/"+attachment; 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    multipart.addBodyPart(messageBodyPart); 
} 
+0

試圖..不工作 – Aalm

0

我不太明白您的實現作爲文件名設置內代碼沒有任何用戶交互。我建議你改變身體一點點地得到這樣的:

if(filename!=null) { 
    messageBodyPart.setFileName(filename); 
    multipart.addBodyPart(messageBodyPart); 

} 
0

讓我看看,如果我明白了,你得到這個當您嘗試發送的電子郵件沒有依附?
所以變量「attachment」爲空?
所以你試圖加載文件「C:/ Users/gro/Desktop/null」?
而你得到一個FileNotFoundException呢? -.-

+0

是的,當我嘗試發送沒有附件的電子郵件我得到這個錯誤..有一些時候,我需要發送電子郵件沒有附件.. – Aalm

0

沒有看到「附件」是什麼,這將是很難弄清楚你在做什麼。正如其他人已經提到的那樣,你需要檢查「attachment」的值,如果它存在(非null或File.exists()),那麼應該執行用於將文件附加到電子郵件的代碼塊。

+0

即使我檢查文件是否存在,我仍然得到錯誤 – Aalm

+0

「附件」的價值是什麼? – ANooBee

0

你可以請硬編碼的文件路徑(包括文件名),並嘗試操作,我認爲你的'attachement'被設置爲空或一些不存在的目錄。