有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java附件不随spring mail一起发送

我正在尝试用spring mail的JavaMailSender发送邮件。我首先实现了没有附件的版本,但现在我需要添加附件。但是,尽管附件被添加到了MimeMessageHelper(我可以在debug mod上看到这些部分是adder),但附件并不是通过邮件发送的。邮件主题和内容已正确发送给收件人,但缺少附件。下面是我的代码:

    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(也称为字节[])存储在数据库中。我还尝试从本地系统添加一个文件,但这也不起作用。我还尝试将从数据库读取的文件写入我的系统中的一个文件,这似乎也能正常工作,所以数据库系统按预期工作。保存和检索看起来不是问题。下面是带有的邮件配置。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 field只是一个字段,用于设置邮件本身以外的其他邮件的name field

谢谢


共 (1) 个答案

  1. # 1 楼答案

    我也遇到了同样的问题,正如法提赫所说,你必须把文本放到助手中,而不是放到mimessage中

    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");