【问题标题】:Attachment's are not sent with spring mail附件不随春季邮件发送
【发布时间】:2018-01-14 18:36:20
【问题描述】:

我正在尝试使用 spring mail 的 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

name 字段只是一个字段,用于在邮件本身以外的邮件上设置 name 字段。

谢谢。

【问题讨论】:

    标签: java spring spring-messaging


    【解决方案1】:

    我遇到了同样的问题,正如 Fatih 所说,您必须将文本放入帮助程序而不是 mimeMessage。

    messageHelper.addAttachment(fileSystemResource.getFilename(),fileSystemResource  );
    messageHelper.setSubject(mails.getSubject());
    messageHelper.setFrom(this.userName,this.sendingName);
    
    // this is correct
    messageHelper.setText(mails.getContent(),true);
    
    // this is wrong, it will overwrite the attachments
    // mimeMessage.setContent(mails.getContent(),"text/html");
    

    【讨论】:

      【解决方案2】:

      好的,我解决了这个问题。问题是我直接设置了 mimeMessage 的内容,所以它覆盖了 addAttachment 和其他内容明智的配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-06
        • 2018-03-18
        • 2011-03-24
        • 1970-01-01
        相关资源
        最近更新 更多