【问题标题】:Using Writer to send an email with Korean text outputs garbage使用 Writer 发送带有韩文文本的电子邮件输出垃圾
【发布时间】:2019-04-26 03:59:54
【问题描述】:

我的应用程序以编程方式发送电子邮件。当正文是英文文本时它可以工作,但当正文是韩文时,它会作为垃圾出现。例如发送 '테스트' 结果为 '???'。

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

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

【问题讨论】:

    标签: java android writer


    【解决方案1】:

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

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

    【讨论】:

    • 感谢工作!我不得不使用AuthenticatingSMTPClient("TLS", "UTF-8") 编辑:刚刚看到您更新了答案以包含默认协议。
    猜你喜欢
    • 2017-03-02
    • 2016-05-04
    • 2013-10-17
    • 1970-01-01
    • 2012-05-27
    • 2013-04-08
    • 2011-02-04
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多