简体   繁体   English

使用javamail将字符串作为附件发送会得到重复的内容

[英]Sending string as an attachment using javamail gets duplicated content

I'm struggling to send a csv file over javamail.我正在努力通过javamail发送一个csv文件。

Since the content is small, I constructed manually the data as CSV format, stored in memory and passed as ByteArrayDataSource to MimeMessageHelper .由于内容很小,因此我将数据手动构造为CSV格式,存储在内存中,并作为ByteArrayDataSource传递给MimeMessageHelper However, the received file has strangely double the content.但是,接收到的文件的内容却奇怪地翻了一番。

双重内容

The code is pretty standard:该代码是非常标准的:

// mail sender
try {
            MimeMessage mail = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mail, true, "UTF-8");
            helper.setSubject(mailObj.getMailSubject());
            helper.setFrom(mailObj.getMailFrom());
            helper.setTo(mailObj.getMailTo());
            Mail.Attachment attachment = mailObj.getAttachment();
            if (attachment != null) helper.addAttachment(attachment.getFilename(),
                                                         attachment.getDataSource());
            javaMailSender.send(helper.getMimeMessage());
        } catch (Exception e) {
            log.error("Cannot send email " + mailObj.toString(), e);
        }
DataSource createDataSource(Data originalData) throws IOException {
        try (StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out) {
            @Override
            public void println() {
                write(LINE_SEP);
            }
        }) {
            pw.write('\uFEFF'); // Write BOM
            pw.println(HEADERS);
            for (AppointmentBookingEvent details : appointments) {
                // Concatenate the data with PrintWriter.println(...);
            }
            return new ByteArrayDataSource(out.toString().getBytes(StandardCharsets.UTF_8), "text/csv");
        }
    }

I noticed that the method DataSource.getInputStream() is called twice by the sender's internal functions.我注意到发送者的内部函数两次调用了DataSource.getInputStream()方法。 Was that by any chance the cause ?这是偶然的原因吗?

I'm struggling to send a csv file over javamail.我正在努力通过javamail发送一个csv文件。

Since the content is small, I constructed manually the data as CSV format, stored in memory and passed as ByteArrayDataSource to MimeMessageHelper .由于内容很小,因此我将数据手动构造为CSV格式,存储在内存中,并作为ByteArrayDataSource传递给MimeMessageHelper However, the received file has strangely double the content.但是,接收到的文件的内容却奇怪地翻了一番。

双重内容

The code is pretty standard:该代码是非常标准的:

// mail sender
try {
            MimeMessage mail = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mail, true, "UTF-8");
            helper.setSubject(mailObj.getMailSubject());
            helper.setFrom(mailObj.getMailFrom());
            helper.setTo(mailObj.getMailTo());
            Mail.Attachment attachment = mailObj.getAttachment();
            if (attachment != null) helper.addAttachment(attachment.getFilename(),
                                                         attachment.getDataSource());
            javaMailSender.send(helper.getMimeMessage());
        } catch (Exception e) {
            log.error("Cannot send email " + mailObj.toString(), e);
        }
DataSource createDataSource(Data originalData) throws IOException {
        try (StringWriter out = new StringWriter(); PrintWriter pw = new PrintWriter(out) {
            @Override
            public void println() {
                write(LINE_SEP);
            }
        }) {
            pw.write('\uFEFF'); // Write BOM
            pw.println(HEADERS);
            for (AppointmentBookingEvent details : appointments) {
                // Concatenate the data with PrintWriter.println(...);
            }
            return new ByteArrayDataSource(out.toString().getBytes(StandardCharsets.UTF_8), "text/csv");
        }
    }

I noticed that the method DataSource.getInputStream() is called twice by the sender's internal functions.我注意到发送者的内部函数两次调用了DataSource.getInputStream()方法。 Was that by any chance the cause ?这是偶然的原因吗?

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

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