如何发送带有附件的邮件,这是与java邮件发件人的字节数组?

时间:2018-01-23 11:13:34

标签: java spring email javamail

我想使用java邮件发件人发送电子邮件。我有字节数组,我应该作为邮件附件发送。我尝试了多种方式,但即将推出的附件不是可读格式。它以字节为单位.Below是我试过的代码。

public void sendMailWithAttachments(EmailContent emailContent,
            byte[] attachment) {
        try{
            MimeMessage mimeMessage = emailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new 
             MimeMessageHelper(mimeMessage, true);

            mimeMessageHelper.setTo(emailContent.getTo());
            mimeMessageHelper.setFrom(emailContent.getFrom());
            mimeMessageHelper.setCc(emailContent.getCc());
            mimeMessageHelper.setSubject(emailContent.getSubject());


                final InputStreamSource attachmentSource = new ByteArrayResource(attachment);
                DataSource dataSource = new ByteArrayDataSource(attachment, "application/pdf");
                mimeMessageHelper.addAttachment("testattachment", dataSource);



            emailSender.send(mimeMessageHelper.getMimeMessage());
            emailContent.setStatus(true);
        }catch(Exception e){    
            e.printstacktrace
        }

0 个答案:

没有答案