简体   繁体   English

附件不与 spring 邮件一起发送

[英]Attachment's are not sent with spring mail

i'm trying to send mails with spring mail's JavaMailSender.我正在尝试使用 spring 邮件的 JavaMailSender 发送邮件。 i first implemented the version without attachments but now i need to add attachments.我首先实现了没有附件的版本,但现在我需要添加附件。 but, although the attachments are added to MimeMessageHelper (i can see on debug mod that the parts are adder), the attachments are not sent with mail.但是,尽管附件已添加到 MimeMessageHelper(我可以在调试模块上看到部件是加法器),但附件并未随邮件一起发送。 mails subject and content are correctly sent to receivers but attachments are missing.邮件主题和内容正确发送给收件人,但附件丢失。 below is my code:下面是我的代码:

    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();
    }

i get the required data from database like mail itself, receivers and attachment.我从数据库中获取所需的数据,如邮件本身、接收器和附件。 attachments are stored in databse with clob (aka as byte[]).附件使用clob(又名byte[])存储在数据库中。 i also try to add a file from my local system but that doesn't work either.我也尝试从我的本地系统添加一个文件,但这也不起作用。 i also try writing the file that is read from database to a file in my system and that seems to work as well so database system is working as intended.我还尝试将从数据库读取的文件写入我系统中的文件,这似乎也能正常工作,因此数据库系统正在按预期工作。 saving and retrieving doesn't look like the problem.保存和检索看起来不是问题。 below is mail mail configuration with .yml file下面是带有 .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 is just a field to set the name field on the mail from other than mails itself. name 字段只是一个字段,用于在邮件本身以外的邮件上设置 name 字段。

Thank you.谢谢。

I got the same problem, as Fatih said, you have to put the text into the helper and not into the mimeMessage.我遇到了同样的问题,正如 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");

Ok i solved the problem.好的,我解决了这个问题。 The problem was i was setting the content of the mimeMessage directly so it overrode the addAttachment and other content wise configs.问题是我直接设置了 mimeMessage 的内容,因此它覆盖了 addAttachment 和其他内容明智的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM