简体   繁体   中英

MimeMessageHelper not able to send two attachments of the same file using JavaMail?

I as titled suggests, I am not able to sent two individual attachments of same file, on the other hand, there's no problem with sending two or more different attachments (different files). I'm receiving newMessage from Android device (using retrofit), content of attachments are sent as byte[].

Here is code:

public boolean sendNewMessage(Message newMessage, int idAccount) {

    boolean messageSent = true;

    Account acc = accountRepository.findById(idAccount).get();

    boolean isAuthenticationRequired = acc.isAuthentication();


    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        //Properties props = mailSender.getJavaMailProperties();
    Properties props = new Properties();
        //SimpleMailMessage message = new SimpleMailMessage();

    mailSender.setHost(acc.getSmtpAddress());
    mailSender.setPort(acc.getSmtpPort()); //465 ...

    mailSender.setUsername(acc.getUsername());
    mailSender.setPassword(acc.getPassword());

    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.ssl.trust", acc.getSmtpAddress());

    if (isAuthenticationRequired) {
        props.put("mail.smtp.auth", "true");
    } else {

    }

    mailSender.setJavaMailProperties(props);

    MimeMessage mimeMessage = mailSender.createMimeMessage();
    boolean hasAttachments = false;
    if (newMessage.getAttachments().size() > 0) hasAttachments = true;
    MimeMessageHelper helper = null;

    try {

        helper = new MimeMessageHelper(mimeMessage, hasAttachments);

        if (hasAttachments){

            for (Attachment att : newMessage.getAttachments()){

                helper.addAttachment(att.getName(), new ByteArrayDataSource(att.getData(), createMimeType(att)));//I've also tried to attache current date_time to att.getName(), but it didn't work
            }
        }

        helper.setFrom(newMessage.getFrom());
        helper.setTo(newMessage.getTo().stream().toArray(String[]::new));
        helper.setCc(newMessage.getCc().stream().toArray(String[]::new));
        helper.setBcc(newMessage.getBcc().stream().toArray(String[]::new));

        helper.setSubject(newMessage.getSubject());
        helper.setText(newMessage.getContent());

        mailSender.send(mimeMessage);

        newMessage.setDate_time(LocalDateTime.now());
        newMessage.setAccount(acc);
        addNewMessage(newMessage); //adds to database

    } catch (MessagingException e) {
        e.printStackTrace();
        messageSent = false;

    } catch (Exception e) {
        e.printStackTrace();
        messageSent = false;
    }

    return messageSent;

}


private String createMimeType(Attachment att){

    return URLConnection.guessContentTypeFromName(att.getName()+"."+att.getMime_type());
}

Java Mail debug is logging multiple files upload and multiple content parts but on receiving side there is only one probably because of same attachment name (cant find proof but javadoc of addAttachment method suggests that some internal working is at place). I would suggest to change attachment name ie add versioning:

helper.addAttachment("temp.csv", new ByteArrayDataSource(new FileInputStream("temp.csv").readAllBytes(), "text/plain"));
helper.addAttachment("temp1.csv", new ByteArrayDataSource(new FileInputStream("temp.csv").readAllBytes(), "text/plain"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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