使用 Writer 发送带有韩语文本的电子邮件输出垃圾

Using Writer to send an email with Korean text outputs garbage

我的应用程序以编程方式发送电子邮件。当正文是英文文本时它有效,但当正文是韩文时它会以垃圾形式出现。例如,发送“테스트”会导致“???”。

这是我用来发送电子邮件的代码:

AuthenticatingSMTPClient client = new AuthenticatingSMTPClient();
    try {
        client.connect(hostname, port);
        client.ehlo("localhost");
        if (client.execTLS()) {
            client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN, login, password);

            client.setSender(from);

            client.addRecipient(to);

            Writer writer = client.sendMessageData();

            if (writer != null) {
                SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, subject);
                writer.write(header.toString());
                writer.write("테스트);
                writer.close();
                if (!client.completePendingCommand()) {
                    throw new Exception("Failure to sendLocation the email " + client.getReply() + client.getReplyString());
                }
            } else {
                throw new Exception("Failure to sendLocation the email " + client.getReply() + client.getReplyString());
            }
        } else {
            throw new Exception("STARTTLS was not accepted " + client.getReply() + client.getReplyString());
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (client != null) {
            client.logout();
            client.disconnect();
        }
    }

Specify the character encoding 创建客户端时,例如

AuthenticatingSMTPClient client =
    new AuthenticatingSMTPClient(SMTPSClient.DEFAULT_PROTOCOL, "UTF-8");