Chinese garbled code in configuration file when sending mail in idea

I wrote a test of sending e-mail yesterday

First write a simple test:

public class TestEmail {
    public static void main(String[] args) {
        try{
            HtmlEmail email = new HtmlEmail();
            //Set the user name and password (authorization password) of the email mailbox
            email.setAuthentication("user name ","Authorization password");
            //Set the sending mail server (SMTP server) domain name
            email.setHostName("smtp.163.com");
            //Set the recipient's mailbox
            email.addTo("Recipient mailbox@qq.com");
            //Set sender's mailbox
            email.setFrom("Sender mailbox@163.com");
            //Subject of mailbox
            email.setSubject("This is an email for testing!");
            //Set the coding method to prevent random code
            email.setCharset("GB2312");
            //Set the content of the message
            email.setMsg("Test the content of the email!");
            //Send mail
             email.send();
        }catch(EmailException e){
            throw new RuntimeException(e);
        }
    }
}

The test shows that it can be sent

Then I want to add the function of sending e-mail to the project I write. This must not work. I must encapsulate it. After encapsulation

Profile:

Tools:

package com.cxj.utils;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;

/**
 * @Author : cxj
 * @create 2021/12/4 20:40
 */
public class EmailUtils {
    /**
     * Send activation email
     * @param to Recipient mailbox
     * @param code Activation code
     * @return
     */
    public boolean sendActivateMail(String to,String code){
        //Get input stream
        InputStream in = EmailUtils.class.getClassLoader().getResourceAsStream("activeEmail.properties");
        Properties prop = new Properties();
        try {
            prop.load(in);
            String name = prop.getProperty("name");
            String password = prop.getProperty("password");
            String host = prop.getProperty("host");
            String from = prop.getProperty("from");
            String subject = prop.getProperty("subject");
            String encode = prop.getProperty("encode");
            String content = prop.getProperty("content");
            //Replace the placeholder with an activation code (, activation code)
            content = MessageFormat.format(content,code);

            //Send mail
            EmailUtils emailUtils = new EmailUtils();

            return emailUtils.sendMail(name,password,host,to,from,subject,content,encode);

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * For sending mail
     * @param name Sender user name
     * @param password Sender password
     * @param host Sender mailbox
     * @param to Recipient mailbox
     * @param from Sender mailbox
     * @param subject Subject of mailbox
     * @param content Contents of mailbox
     * @param encode Encoding method of mailbox
     * @return true Indicates that the sending was successful, and false indicates that the sending failed
     */
    public boolean sendMail(String name,String password,String host,String to,String from,String subject,String content,String encode){
        try{
            HtmlEmail email = new HtmlEmail();
            //Set the user name and password (authorization password) of the email mailbox
            email.setAuthentication(name,password);
            //Set the sending mail server (SMTP server) domain name
            email.setHostName(host);
            //Set the recipient's mailbox
            email.addTo(to);
            //Set sender's mailbox
            email.setFrom(from);
            //Subject of mailbox
            email.setSubject(subject);
            //Set the coding method to prevent random code
            email.setCharset(encode);
            //Set the content of the message
            email.setMsg(content);
            //Send mail
            email.send();
            return true;
        }catch(EmailException e){
            e.printStackTrace();
            return false;
        }
    }
}

Then test:

public class TestEmail {
    public static void main(String[] args) {
        EmailUtils send = new EmailUtils();
        send.sendActivateMail("Recipient mailbox@qq.com","Mailbox subject");
    }
}

Send it like this

After debugging, I found that the subject and content of the email in my configuration file are all Chinese, so it is garbled when it is read out.
Then I went to the Internet and found that as long as this modification was right

However, please note that when I modify the settings here, the Chinese in my configuration file is still there, and then I set it. Check the configuration file to automatically convert the Chinese into ASCII code. After setting, it is found that the Chinese in the configuration file is directly garbled. I first deleted the garbled code, and then wrote it in Chinese. As a result, I directly reported an error during the test:

I tried it myself first:

1. Change the settings back
2. Replace the Chinese of the configuration file with numbers and English
3. Restart idea

As a result, he still can't, but if I write it and don't encapsulate it, I won't report an error and can send it. Do not know why?
Go online to find answers. Basically, they say

My email related services are not enabled
My email account or authorization password is wrong
My recipient's email address is wrong

But I promise, I have written all these correctly, and he still can't. During this period, someone told me that this was because mailbox 163 treated my mail as garbage, but I still don't understand why it can be sent without encapsulation, and the encapsulated ones always report errors.

Later, I used QQ mailbox to send it. As a result, it can be sent without encapsulation and encapsulation. Then I want to try 163 mailbox. Can it be saved
Test 163 again. There is no Chinese in my configuration file. God, he can send it. I don't know why. I haven't changed anything. I just changed to QQ email for several times and came back to test it
Then I tried to change the settings. In fact, I made a great determination to change the settings, because I was afraid that 163 errors would be reported as I did last time. As a result, I changed the settings and tried to send e-mail again. Can I? I also changed the email subject and content in the configuration file into Chinese, which is still OK!

Why do you change the settings twice, but the results are different?

I finally found the answer
When I first modified the settings, there was Chinese in my configuration file. After the modification, the Chinese in the configuration file was directly garbled, and then an error was reported (I don't know why)
The second time I modified the settings, there was no Chinese in my configuration file and it could run normally, and I added Chinese later and it could also run normally

Therefore, if there is Chinese in the configuration file, and there is a problem of garbled code during the test, modify the settings. When "Chinese in the configuration file is converted to ASCII" is checked, there must be no Chinese in the configuration file to avoid problems

Tags: Java Back-end

Posted on Sat, 04 Dec 2021 23:16:27 -0500 by Toxinhead