0

I am saving correctly Arabic names in my database. but when am fetching to send them email I am getting like ???(unsupporting)

My codes: Application.properties

server.port=8080
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?useUnicode=yes&characterEncoding=UTF-8
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

Repository

public interface MyUserRepository extends JpaRepository<User,String>{

    List<User> findByEmail(String email);

}

Service

public List<User> getAllAccount(String email) {
         return myUserRepository.findByEmail(email);
}

model

@Entity
@Table(name = "User")
public class User implements java.io.Serializable {
@Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    private Integer Id;
    @Column(name = "username", unique = true, nullable = false, length = 64)
    private String username;
    private String name;
    @Column(name = "email", length = 100)
    private String email;
//getters & setters
}

on controller when am trying to get name then its give me ???? whereas its correctly saved in db(readable)

for (int i = 0; i < size; i++) {
            String user = users.get(i).getUsername();
            System.out.println("USER " + user); // printing ???
}

What am doing wrong or missing that giving me this problem. please help me.

Deepak Maurya
  • 67
  • 1
  • 9
  • Have you already tried sending an email? This may be a problem only with the Windows console and `System.out.println`, assuming you are using Windows. See here: https://stackoverflow.com/questions/28065818/how-to-display-arabic-text-in-cmd – jdaz Jun 04 '20 at 02:17
  • ok but actually i was getting it while i was writing same object in email. all am doing is just adding object in a static template something like "Dear "+username //I am gettng ??? – Deepak Maurya Jun 04 '20 at 06:37

1 Answers1

2

It is UTF encoding support issue in the email template.

You can set encoding support in your method for sending your email like this:

Session session = Session.getDefaultInstance(props);
session.setDebug(false);
MimeMessage msg = new MimeMessage(session);
msg.setContent(body, "text/html; charset=utf-8");

I've implemented this same logic in my recent project forex exchange CurrencyFreaks this logic solved my problem. Good Luck!