有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Writer发送带有韩文文本的电子邮件

我的应用程序以编程方式发送电子邮件。当正文是英文文本时,它起作用,但当正文是韩文时,它就变成了垃圾。例如发送'테스트' 结果为“??”

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

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();
        }
    }

共 (1) 个答案

  1. # 1 楼答案

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

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