-2

I have used springboot with JPA. my Codes are

Connection property in application.xml:

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.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect 

Pojo

class User{
@Id
    @Column(name = "username", unique = true, nullable = false, length = 64)
    private String username;
@Column(name = "password")
    private String password;
@Column(name = "email", length = 100)
    private String email;
//getter and setter
//to string()
}

mysql table property

CREATE TABLE `User` (
  `username` varchar(64) CHARACTER SET utf8 NOT NULL,
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

When am saving arabic characters it save successfully in database.

But when am fetching am getting ???? like this. when i debug and stopped at that point i see it get Arabic fonts correctly on that object.

debug screen

Email sending code

private void initSession() {
    Properties props = new Properties();
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "587");
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.debug", "true");
    

    session = Session.getInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
}
public String forgetPassword(String accounts, String e_address) {
    EmailMessage emailMessage = new EmailMessage();
    emailMessage.setSubject("Forget Password");
    emailMessage.setBody(MessageTemplate.forgetPassword(accounts));
    emailMessage.setTo_address(e_address);
    try {
        sendEmail(emailMessage);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    return "Successfully send";
}
private void sendmail(EmailMessage emailmessage) throws AddressException, MessagingException, IOException {
    
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(username, false));
    
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailmessage.getTo_address()));
            msg.setSubject(emailmessage.getSubject());
            msg.setContent(emailmessage.getBody(), "text/html");
            msg.setSentDate(new Date());
    
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(emailmessage.getBody(), "text/html");
    
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
    
            msg.setContent(multipart);
            // sends the e-mail
            Transport.send(msg);
    
        }

Service code

public String getAllAccount(String email) {
        List<User> users = myUserRepository.findByEmail(email);
        System.out.println(users.toString());
        return users.toString();
    }

Controller code

     @PostMapping("/forgetPassword")
    public ResponseEntity forgetPassword(@RequestBody GetEmail getEmail) {
        ResponseEntity responseEntity;
        Map<String, String> result = new HashMap<>();
        if (!PatternMatch.isValidEmail(getEmail.getEmail())) {
            result.put("result", UserConstant.UNSUPPORTED_EMAIL + "");
        } else {
            String accounts = service.getAllAccount(getEmail.getEmail());
            System.out.println("accounts " + accounts);
            if (accounts.equals("")) {
                result.put("result", UserConstant.EMAIL_NOT_EXISTS + "");
            } else {
                new Thread(() -> {
                    final EmailSender sender = EmailSender.getInstance();
                    sender.forgetPassword(accounts, getEmail.getEmail());
                }) {
                }.start();
                result.put("result", UserConstant.EMAIL_SENT_SUCCESSFULLY + "");
            }
        }

what am doing wrong please help me.

Deepak Maurya
  • 67
  • 1
  • 9
  • How do you fetch the String? Since it looks good in debug mode. – Milgo Aug 12 '20 at 08:15
  • Not sure if it is related, but based on your input I found this - https://stackoverflow.com/questions/2996475/what-character-encoding-should-i-use-for-a-web-page-containing-mostly-arabic-tex#:~:text=All%20Arabic%20characters%20can%20be,a%20more%20space%20efficient%20option. – dbl Aug 12 '20 at 08:18
  • @Milgo add a breakpoint on line where you want to check in runtime value of object and run project in debug mode. move your cursor on that object you will see there. – Deepak Maurya Aug 12 '20 at 08:33
  • Sorry, I was unclear. I meant how do you fetch your String to get ????. Since the value is correct in debug mode. – Milgo Aug 12 '20 at 08:39
  • I am printing it to controller using system.out.println(). and actually i am writing this object in mail code. in console and in mail i get ???? like this. although while saving am printing and getting correct value in console. – Deepak Maurya Aug 12 '20 at 08:47
  • Did you set your IDE's encoding to UTF-8? And please add your "mail code". – Milgo Aug 12 '20 at 09:48
  • The problem is how you display the Arabic characters. Usually when you see a question mark (i.e. `?`) rather than the actual character, it is because the Font being used cannot display the actual characters. In your case, the Font being used cannot display Arabic characters. What platform are you on and how are you displaying the characters? Maybe you are on Windows and trying to display the characters in a Command Prompt window? – Abra Aug 12 '20 at 11:32
  • @Abra for checking purpose i am printing it on netbeans console during execute. and i see the character when am inserting it in database. – Deepak Maurya Aug 12 '20 at 11:46
  • @Milgo I wrote just hard code System.out.println("مينا"); to print arabic name in service and i got ???? like this in email and console. – Deepak Maurya Aug 12 '20 at 18:34

1 Answers1

2

Please check if your console is using the charset UTF-8 and set the charset of your e-mail, e.g. with:

msg.setContent(emailmessage.getBody(), "text/html;charset=utf-8");
Milgo
  • 2,617
  • 4
  • 22
  • 37