Hello. In the code I send the letter from two different parts of the code. The first version of the code (sendMail code) sends it with the attachment empty, the file name is Noname. The second version of the code, does not send any attachments already. I can not understand what the problem is.

public JavaMailSenderImpl mailSender() { JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl(); javaMailSenderImpl.setHost("smtp.gmail.com"); javaMailSenderImpl.setPort(567); javaMailSenderImpl.setUsername("************"); javaMailSenderImpl.setPassword("***********"); javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.host", "smtp.gmail.com"); javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.port", "465"); javaMailSenderImpl.getJavaMailProperties().put("mail.debug", "true"); javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.auth", "true"); javaMailSenderImpl.getJavaMailProperties().put("mail.smtp.starttls.enable", "true"); javaMailSenderImpl.getJavaMailProperties().setProperty("mail.smtp.socketFactory.port", "465"); javaMailSenderImpl.getJavaMailProperties().setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); javaMailSenderImpl.getJavaMailProperties().setProperty("mail.smtp.socketFactory.fallback", "false"); return javaMailSenderImpl; } 

The code that sends the attachment:

 public void sendMail(String to, String userName) throws MessagingException { StringTokenizer tokenizer = new StringTokenizer(to + ";", ";,"); MimeMessage message2 = javaMailSenderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message2, true); Formatter formatter = new Formatter(); formatter.format("************* %S в: %s", userName, new Date()); helper.setTo(tokenizer.nextToken()); while (tokenizer.hasMoreTokens()) { helper.addCc(tokenizer.nextToken()); } helper.setFrom("**************"); helper.setSubject(formatter.toString()); javaMailSenderImpl.send(message2); } 

Call:

 utilService.sendMail("*****" , SecurityContextHolder.getContext().getAuthentication().getName().toString()); 

enter image description here

    2 answers 2

    And you, by the way, do not forget to add the attachment itself to the letter?

      mailSender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); message.setFrom("me@mail.com"); message.setTo("you@mail.com"); message.setSubject("my subject"); message.setText("my text <img src='cid:myLogo'>", true); message.addInline("myLogo", new ClassPathResource("img/mylogo.gif")); message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf")); } }); 

    This is an example from the MimeMessageHelper docks that you use, and it immediately shows that the attachment is being added ..

    Anticipating your further question about where the "noname" comes from - try to podebazhit (or enable spring debag-logs) code about here:

     MimeMessageHelper#createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) 

    There the mime-type is just exposed, and there are mentions about attachments. It’s hard for me without a code to guess how the code works further, but I assume that the attachment remains empty and Spring (or even the transport level) replaces null with an empty file named "noname".

    • figured out. if I don’t add the text setText(str) , an empty attachment is created, no matter if I add the attachment or not. those. If I add an attachment, but do not install the text, then comes 2 attachments, one is mine and one is empty Noname - m_br0_89

    Understood. Must be installed text