以下是一个Java SMTP发送邮件的示例代码,其中包括将标题和内容进行Base64编码的过程,没有使用第三方库。\n\njava\nimport java.io.*;\nimport java.net.Socket;\nimport java.nio.charset.StandardCharsets;\nimport java.util.Base64;\n\npublic class SMTPSender {\n\n private static final String SMTP_SERVER = "smtp.example.com";\n private static final int SMTP_PORT = 25;\n private static final String SENDER_EMAIL = "sender@example.com";\n private static final String SENDER_PASSWORD = "password";\n private static final String RECIPIENT_EMAIL = "recipient@example.com";\n private static final String SUBJECT = "Hello, World!";\n private static final String CONTENT = "This is the email content.";\n\n public static void main(String[] args) {\n try {\n Socket socket = new Socket(SMTP_SERVER, SMTP_PORT);\n PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8), true);\n BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));\n\n // Read the welcome message from the server\n String response = in.readLine();\n System.out.println(response);\n\n // Send HELO command\n out.println("HELO " + SMTP_SERVER);\n response = in.readLine();\n System.out.println(response);\n\n // Send AUTH LOGIN command\n out.println("AUTH LOGIN");\n response = in.readLine();\n System.out.println(response);\n\n // Send base64-encoded username\n String encodedUsername = Base64.getEncoder().encodeToString(SENDER_EMAIL.getBytes(StandardCharsets.UTF_8));\n out.println(encodedUsername);\n response = in.readLine();\n System.out.println(response);\n\n // Send base64-encoded password\n String encodedPassword = Base64.getEncoder().encodeToString(SENDER_PASSWORD.getBytes(StandardCharsets.UTF_8));\n out.println(encodedPassword);\n response = in.readLine();\n System.out.println(response);\n\n // Send MAIL FROM command\n out.println("MAIL FROM: <" + SENDER_EMAIL + ">");\n response = in.readLine();\n System.out.println(response);\n\n // Send RCPT TO command\n out.println("RCPT TO: <" + RECIPIENT_EMAIL + ">");\n response = in.readLine();\n System.out.println(response);\n\n // Send DATA command\n out.println("DATA");\n response = in.readLine();\n System.out.println(response);\n\n // Send email headers\n out.println("From: " + SENDER_EMAIL);\n out.println("To: " + RECIPIENT_EMAIL);\n out.println("Subject: =?UTF-8?B?" + Base64.getEncoder().encodeToString(SUBJECT.getBytes(StandardCharsets.UTF_8)) + "=?");\n out.println("Content-Type: text/plain; charset=utf-8");\n out.println();\n\n // Send email content\n out.println(CONTENT);\n\n // Send end of email data command\n out.println(".");\n response = in.readLine();\n System.out.println(response);\n\n // Send QUIT command\n out.println("QUIT");\n response = in.readLine();\n System.out.println(response);\n\n // Close the connection\n in.close();\n out.close();\n socket.close();\n } catch (IOException e) {\n e.printStackTrace();\n }\n }\n}\n\n\n这个示例代码中的SMTP服务器、端口号、发件人邮箱、密码、收件人邮箱、邮件主题和内容等信息需要根据实际情况进行修改。关键的部分是将邮件主题和内容进行Base64编码,然后在发送时使用Base64编码的内容,以确保正确处理包含非ASCII字符的邮件主题和内容。

Java SMTP发送邮件示例:无需第三方库实现Base64编码邮件主题和内容

原文地址: https://www.cveoy.top/t/topic/pJKU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录