JavaMail sending mail attachment from string - encoding UTF-8

前端 未结 8 2053
面向向阳花
面向向阳花 2021-02-03 22:34

My application has to send a textfile, which it first has to generate as a String. The text contains non-ASCII symbols, so i would like it to be UTF-8. I\'ve tried a lot of vari

相关标签:
8条回答
  • 2021-02-03 23:09

    Give this a try:

    String attachment = "Привет";
    DataSource ds = new ByteArrayDataSource(attachment, "text/plain; charset=UTF-8");
    messageBodyPart.setDataHandler(new DataHandler(ds));
    

    UPDATE: (full example)

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.mail.Session;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.mail.util.ByteArrayDataSource;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            String attachment = "Привет";
            DataSource ds = new ByteArrayDataSource(attachment, "text/plain; charset=UTF-8");
            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(ds));
    
            MimeBodyPart bodyPart = new MimeBodyPart();
            bodyPart.setText("Hello this is some text");
    
            MimeMultipart mp = new MimeMultipart("mixed");
            mp.addBodyPart(bodyPart);
            mp.addBodyPart(attachmentPart);
    
            MimeMessage msg = new MimeMessage((Session)null);
            msg.setContent(mp);
    
            msg.writeTo(System.out);
        }
    }
    

    output:

    Message-ID: <1439781957.1.1297366787857.JavaMail.dnault@dnault.local>
    MIME-Version: 1.0
    Content-Type: multipart/mixed; 
        boundary="----=_Part_0_1579321858.1297366787792"
    
    ------=_Part_0_1579321858.1297366787792
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit
    
    Hello this is some text
    ------=_Part_0_1579321858.1297366787792
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: base64
    
    0J/RgNC40LLQtdGC
    ------=_Part_0_1579321858.1297366787792--
    
    0 讨论(0)
  • 2021-02-03 23:14

    If problem is in file name, rather than in body, following code helped in my (hebrew) case:

    MimeBodyPart attachment = new MimeBodyPart();
    attachment.setFileName(MimeUtility.encodeText(filename, "UTF-8", null));
    
    0 讨论(0)
  • 2021-02-03 23:17

    I used to try send file name in url encoded. And it works for gmail

    messageBodyPart.setFileName(UriUtils.encodePath(attachment.getAttachmentName(), "UTF-8"))
    

    full code here:

    if (!CollectionUtils.isEmpty(requestMessage.getAttachments())) {
                MimeBodyPart messageBodyPart;
                String fileName;
                File file;
                for (Attachment attachment : requestMessage.getAttachments()) {
                    messageBodyPart = new MimeBodyPart();
                    fileName = attachment.getAttachmentName();
                    file = new File(fileName);
                    FileUtils.writeByteArrayToFile(file, attachment.getAttachment());
                    messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file)));
                    messageBodyPart.setFileName(UriUtils.encodePath(attachment.getAttachmentName(), "UTF-8"));
                    messageBodyPart.setDisposition(Part.ATTACHMENT);
                    multipart.addBodyPart(messageBodyPart);
                }
            }
    
    0 讨论(0)
  • 2021-02-03 23:20

    Had similar case, following code solved it:

    MimeBodyPart att = new MimeBodyPart();
    att.setFileName(MimeUtility.encodeText(fileName));
    
    0 讨论(0)
  • 2021-02-03 23:20

    This is a sample code that I use to send files (irrespective on encoding or data structure).

    BodyPart fileBodyPart = new MimeBodyPart();
    fileBodyPart.setDataHandler(new DataHandler(fileDataSource));
    fileBodyPart.setFileName(attachment.getName());
    fileBodyPart.setHeader("Content-Type", fileDataSource.getContentType());
    fileBodyPart.setHeader("Content-ID", attachment.getName());
    fileBodyPart.setDisposition(Part.INLINE);
    

    Where fileDataSource is a javax.activation.DataSource (text file will be in here), and fileBodyPart.setDisposition(Part.INLINE); (PART.INLINE means datasource is inlined with the message body, just like HTML emails, PART.ATTACHMENT means datasource is an attachment).

    Hope this helps.

    0 讨论(0)
  • 2021-02-03 23:23

    One more possibility:

    String attachment = "älytöntä";
    MimeBodyPart part = new MimeBodyPart();
    part.setText(attachment, "UTF-8");
    part.setDisposition("attachment");
    part.setFileName("attachment.txt");
    part.setHeader("Content-Transfer-Encoding", "base64");
    part.setHeader("Content-type", "text/plain; charset=utf-8");
    
    0 讨论(0)
提交回复
热议问题