Unicode chars and Spring JavaMailSenderImpl, no unicode chars under Linux!

14,494

Solution 1

I am posting the solution.

First make sure about the encoding of the source code (if there is inline text). In eclipse is the first screen if you choose project properties. Warning: if you change it later it will garble your text.

Second It is better to use MimeMailMessage so that you can specify the encoding, like this:

MimeMessage msg = mailSender.createMimeMessage();

        msg.addRecipient(RecipientType.TO, new InternetAddress(adminEmail));
        msg.addFrom(new InternetAddress[] { new InternetAddress(adminEmail) });

        msg.setSubject(subject, "UTF-8");
        msg.setText(message, "UTF-8");  
        mailSender.send(msg);

Third make sure the system property mail.mime.charset is set to UTF-8, either from Java command or by code like this:

System.setProperty("mail.mime.charset", "utf8");

Thanks to everybody that helped me sorting this out.

Solution 2

In my case, I resolved the encoding problem by specifying JavaMailSenderImpl's defaultEncoding:

mailSender = new JavaMailSenderImpl();
...
mailSender.setDefaultEncoding("UTF-8");

I believe you can also set the value in bean configuration:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    ...
    <property name="defaultEncoding" value="UTF-8"/>
</bean>
Share:
14,494

Related videos on Youtube

gotch4
Author by

gotch4

can do java, js, python, scala, kotlin, some C/C++ and others for food and housing

Updated on June 04, 2022

Comments

  • gotch4
    gotch4 over 1 year

    I'm using Spring and JavaMailSenderImpl, a famous spring class to send emails. My emails contain a lot of unicode chars like èéàò or most notably the dreaded € symbol. My classes work fine when the run on windows. The emails sent are with all the chars (plain text, no html). If I install my app on a Linux virtual server, I'll get all ? instead of the special chars. Is it Spring, Java configuration or something else?

    Update

    Basically the architecture is this: there is a Spring Web Application and I use spring JavaMailSenderImpl to get the work done. This is the configuration in servlet-context:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${email.server}" />
        <property name="username" value="${email.server_user}"></property>
        <property name="password" value="${email.server_pass}"></property>
    </bean>
    

    I'm using the same host on windows and linux to send mail (that is not the same machine where the application runs on... It is just a standard mail service provider over SMTP).

    The code I use to send the email is simply:

    SimpleMailMessage msg = new SimpleMailMessage();
                msg.setTo(adminEmail);
                msg.setFrom(adminEmail);
                msg.setSubject(subject);
                msg.setText(message);
                mailSender.send(msg);
    

    Even setting:

    System.setProperty("mail.mime.charset", "utf8");
    

    at application startup doesn't solve the situation. In fact, before I was getting ? instead of €, now I get �...

  • gotch4
    gotch4 over 12 years
    No, I'm not reading from any file. Text is in string constants in the source code, so it's UTF (I think...)...
  • artbristol
    artbristol over 12 years
    Is it a Maven project? You need to specify the source encoding per maven.apache.org/general.html#encoding-warning. There's probably an equivalent for Ant.
  • gotch4
    gotch4 over 12 years
    I've been thinking of setting mail.mime.charset to UTF-8, would that be enough?
  • artbristol
    artbristol over 12 years
    You need to give more information (edit your question) about your application's architecture, also can you post some of the code where you call JavaMailSenderImpl?
  • gotch4
    gotch4 over 12 years
    it says I've got to wait 21 hours... huuuuuu
  • MyBrainHurts
    MyBrainHurts almost 8 years
    That's right, setting the value in bean configuration works as well.
  • Pafnucy
    Pafnucy almost 6 years
    Been working on Windows and had similar problem. Title and content messed up characters. using MimeMessageHelper to set title and content was the cause, even if utf-8 encoding was set there. Switching to plain MimeMessage.setText and .setSubject with UTF-8 specification worked.
  • Zain Elabidine
    Zain Elabidine almost 3 years
    msg.setSubject(subject, "UTF-8"); the second param doesn't exist any more, any new updates to fix this problem with the new version of the API

Related