Java Mail API - Encoding problems

前端 未结 9 1310
野趣味
野趣味 2020-12-31 11:14

I\'m using Java Mail API and I\'m trying to send an email through Gmail\'s SMTP. How my program works: java.util.Scanner class is used to get user input - I\'m asking user f

相关标签:
9条回答
  • 2020-12-31 11:26

    The following worked for me:

    MimeMessage message = ...
    message.setSubject(subject, "UTF-8");
    message.setContent(body, "text/plain; charset=UTF-8");
    

    Where subject and body are regular String objects with no special treatment (code and user interface use UTF-8).

    0 讨论(0)
  • 2020-12-31 11:27

    You should use setText(String text, String charset) or setText(String text, String charset, String subtype) to set the text body with a specific encoding.

    MimeUtility.encodeText() is not meant for body text, but only for encoded text in headers (and then only for headers set with setHeader or addHeader).

    0 讨论(0)
  • 2020-12-31 11:38

    1- Consider you want to send an email with this string in the body:

    "Olá João!"
    

    2 - As the code is running in the GAE server, this string is interpreted with the default ASCII encoding. To send this email with the correct accented characters, define the String as:

    String body = "Ol\u00e1 Jo\u00e3o!";
    

    The special characters are manually defined with its UTF-8 codes. Search the codes you need in the table http://www.utf8-chartable.de/

    3- Convert the string encoding to UTF-8. All the codes manually typed will be now correctly interpreted:

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);
    String encodedSubject = new String (subject.getBytes("UTF-8"),"UTF-8");
    String encodedBody = new String (body.getBytes("UTF-8"),"UTF-8");
    message.setSubject(encodedSubject, "UTF-8");
    message.setText(encodedBody, "UTF-8");
    
    0 讨论(0)
  • 2020-12-31 11:47

    Basically, my code works just fine, as its supposed to. It was the cmd, that could not handle non-ascii letters. I used a bat file to access a jar. I think I'm just going to make a little GUI then... Thanks everyone for answering.

    0 讨论(0)
  • 2020-12-31 11:47
    JavaMailSenderImpl emailSender = new JavaMailSenderImpl();
    mailSender.setHost("...");
    
    MimeMessage message = emailSender.createMimeMessage();
    message.setSubject("...", "UTF-8");
    message.setText("...", "UTF-8");
    
    MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
    helper.setFrom(from);
    helper.setTo(to);
    
    emailSender.send(message);
    
    0 讨论(0)
  • 2020-12-31 11:48
        MimeMessage message = new MimeMessage(session);
        message.setSubject(subject, "UTF-8");
        message.setText(body, "UTF-8");
    

    So one has to set the character encoding for both, body and subject.


    Addendum because of comment of @bartac

    For the corresponding MimeBodyPart do a setHeader("Content-Type", "text/plain; charset=UTF-8").

    0 讨论(0)
提交回复
热议问题