2017-08-07 74 views
0

我試圖用郵件的郵件的JavaMailSender發送郵件。我第一次實現沒有附件的版本,但現在我需要添加附件。但是,雖然附件添加到MimeMessageHelper(我可以在調試模塊上看到這些部件是加法器),但附件不會與郵件一起發送。郵件主題和內容被正確發送給接收者,但附件丟失。下面是我的代碼:附件的郵件不會發送彈簧郵件

try { 
     MimeMessageHelper messageHelper = new MimeMessageHelper(this.mimeMessage,true, CharEncoding.UTF_8); 

     for(Mail mails : unsentMails) { 
      try { 

       /* 
        Here, we first get the list of receivers and main targets according to their type information 
        Then, we add them to messageHelper 
       */ 
       Set<Attachments> attachments = mails.getFk_attachments(); 

       for(MailReciever item : mails.getRecievers()) { 
        if(item.getType().equals("cc")) { 
          messageHelper.addCc(item.getAddress()); 
         } 
         else { 
          messageHelper.addTo(item.getAddress()); 
         } 
       } 
       for(Attachments file : attachments) { 
        messageHelper.addAttachment(file.getFileName(), 
         new ByteArrayResource(file.getContent()),file.getContentContentType()); 

        try { 
         FileUtils.writeByteArrayToFile(new File("C:\\Users\\fatih.dogmus\\Desktop\\deneme\\"+file.getFileName()),file.getContent()); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       FileSystemResource fileSystemResource = (new FileSystemResource(new File("C:\\Users\\fatih.dogmus\\Desktop\\hebele.txt\\"))); 
       messageHelper.addAttachment(fileSystemResource.getFilename(),fileSystemResource ); 
       messageHelper.setSubject(mails.getSubject()); 
       messageHelper.setFrom(this.userName,this.sendingName); 

       mimeMessage.setContent(mails.getContent(),"text/html"); 

       this.javaMailSender.send(this.mimeMessage); 
       mails.setStatus(EmailStatus.SUCCEEDED); 

       for(MailReciever item : mails.getRecievers()) { 
        item.setStatus(EmailStatus.SUCCEEDED); 
        item.setLastAttemptDate(zonedDateTime); 
       } 

      } 
      catch (MailException mailException) { 
       for(MailReciever item : mails.getRecievers()) { 
        item.incrementAttemptCount(); 
        item.setStatus(EmailStatus.ENQUEUED); 
       } 


       mails.incrementAttemptCount(); 
       mails.setStatus(EmailStatus.ENQUEUED); 


       if(mails.getSendingAttempts() == 3) { 
        mails.setStatus(EmailStatus.FAILED); 
        for(MailReciever item : mails.getRecievers()) { 
         item.setStatus(EmailStatus.FAILED); 
         item.setLastAttemptDate(zonedDateTime); 
        } 

        System.out.println("Failed to send mail. Aborting."); 
       } 
       else { 
        System.out.println(String.format("Attempt count is %d. Will try again", mails.getSendingAttempts())); 
       } 
       mailException.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } finally { 
       mailRepository.save(mails); 
       mailRecieverRepository.save(mails.getRecievers()); 
      } 
     } 
    } 
    catch (MessagingException e) { 
     e.printStackTrace(); 
    } 

我從郵件本身,接收器和附件等數據庫中獲得所需的數據。附件以clob(又名byte [])存儲在數據庫中。我也嘗試從我的本地系統添加一個文件,但這也不起作用。我也嘗試將從數據庫中讀取的文件寫入到我係統中的文件中,這似乎也能正常工作,因此數據庫系統按預期工作。保存和檢索看起來不是問題。下面是.yml文件

mail: 
     host: smtp.gmail.com 
     port: 587 
     name: ****** 
     username: ****** 
     password: ********   
     protocol: smtp 
     tls: true 
     properties.mail.smtp: 
      auth: true 
      starttls.enable: true 
      ssl.trust: smtp.gmail.com 

名稱字段郵件郵件配置只是一個從其他設置郵件名稱字段不是郵件本身的領域。

謝謝。

回答

0

好吧,我解決了這個問題。問題是我直接設置了mimeMessage的內容,因此它覆蓋了addAttachment和其他內容明智的配置。