gpt4 book ai didi

java - 我的 SMTP 发件人抛出错误 421

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:52 36 4
gpt4 key购买 nike

我的代码工作原理如下:

import java.io.*;
import java.net.*;
import java.util.*;

public class SMTPClientDemo {
protected int port = 25;
protected String hostname = "localhost";
protected String from = "";
protected String to = "";
protected String subject = "";
protected String body = "";
protected Socket socket;
protected BufferedReader br;
protected PrintWriter pw;

public SMTPClientDemo() throws Exception
{
// TODO code application logic here
try
{
getInput();
sendEmail();
}
catch (Exception e)
{
System.out.println ("Error sending message - " + e);
}
}
public static void main(String[] args) throws Exception {
// TODO code application logic here
// Start the SMTP client, so it can send messages
SMTPClientDemo client = new SMTPClientDemo();
}
// Check the SMTP response code for an error message
protected int readResponseCode() throws Exception
{
String line = br.readLine();
System.out.println("< "+line);
line = line.substring(0,line.indexOf(" "));
return Integer.parseInt(line);
}

// Write a protocol message both to the network socket and to the screen
protected void writeMsg(String msg) throws Exception
{
pw.println(msg);
pw.flush();
System.out.println("> "+msg);
}

// Close all readers, streams and sockets
protected void closeConnection() throws Exception
{
pw.flush();
pw.close();
br.close();
socket.close();
}

// Send the QUIT protocol message, and terminate connection
protected void sendQuit() throws Exception
{
System.out.println("Sending QUIT");
writeMsg("QUIT");
readResponseCode();
System.out.println("Closing Connection");
closeConnection();
}

// Send an email message via SMTP, adhering to the protocol known as RFC 2821
protected void sendEmail() throws Exception
{
System.out.println("Sending message now: Debug below");
System.out.println("-------------------------------------");
System.out.println("Opening Socket");
socket = new Socket(this.hostname,this.port);
System.out.println("Creating Reader & Writer");
br = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new
OutputStreamWriter(socket.getOutputStream()));
System.out.println("Reading first line");
int code = readResponseCode();
if(code != 220) {
socket.close();
throw new Exception("Invalid SMTP Server");
}
System.out.println("Sending helo command");
writeMsg("HELO "+InetAddress.getLocalHost().getHostName());
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid SMTP Server");
}
System.out.println("Sending mail from command");
writeMsg("MAIL FROM:<"+this.from+">");
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid from address");
}
System.out.println("Sending rcpt to command");
writeMsg("RCPT TO:<"+this.to+">");
code = readResponseCode();
if(code != 250)
{
sendQuit();
throw new Exception("Invalid to address");
}
System.out.println("Sending data command");
writeMsg("DATA");
code = readResponseCode();
if(code != 354)
{
sendQuit();
throw new Exception("Data entry not accepted");
}
System.out.println("Sending message");
writeMsg("Subject: "+this.subject);
writeMsg("To: "+this.to);
writeMsg("From: "+this.from);
writeMsg("");
writeMsg(body);
code = readResponseCode();
sendQuit();
if(code != 250)
throw new Exception("Message may not have been sent correctly");
else
System.out.println("Message sent");
}

// Obtain input from the user
protected void getInput() throws Exception
{
// Read input from user console
String data=null;
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

// Request hostname for SMTP server
System.out.print("Please enter SMTP server hostname: ");
data = br.readLine();
if (data == null || data.equals("")) hostname="localhost";
else
hostname=data;
// Request the sender's email address
System.out.print("Please enter FROM email address: ");
data = br.readLine();
from = data;
// Request the recipient's email address
System.out.print("Please enter TO email address :");
data = br.readLine();
if(!(data == null || data.equals("")))
to=data;
System.out.print("Please enter subject: ");
data = br.readLine();
subject=data;
System.out.println("Please enter plain-text message ('.' character on a blank line signals end of message):");
StringBuilder buffer = new StringBuilder();
// Read until user enters a . on a blank line
String line = br.readLine();
while(line != null)
{
// Check for a '.', and only a '.', on a line
if(line.equalsIgnoreCase("."))
{
break;
}
buffer.append(line);
buffer.append("\n");
line = br.readLine();
}
buffer.append(".\n");
body = buffer.toString();
}

}

编译顺利,当我尝试运行它时,结果是:

 Please enter SMTP server hostname: mail.telkomsel.com
Please enter FROM email address: andikacj@aim.com
Please enter TO email address :kamikaze273@gmail.com
Please enter subject: coba
Please enter plain-text message ('.' character on a blank line signals end of message):
coba14
.
Sending message now: Debug below
-------------------------------------
Opening Socket
Creating Reader & Writer
Reading first line
< 220 ESMTP mail.telkomsel.com
Sending helo command
> HELO Cruzer-PC
< 250 mailpapp1.telkomsel.co.id
Sending mail from command
> MAIL FROM:<andikacj@aim.com>
< 250 2.1.0 Ok
Sending rcpt to command
> RCPT TO:<kamikaze273@gmail.com>
< 250 2.1.5 Ok
Sending data command
> DATA
< 354 end data with <CR><LF>.<CR><LF>
Sending message
> Subject: coba
> To: kamikaze273@gmail.com
> From: andikacj@aim.com
>
> coba14
.

< 421 syntax error (wait for server response)
Sending QUIT
> QUIT
< null
Error sending message - java.lang.NullPointerException

我可以保证的是,421 意味着问题可能出在服务器上,但是是哪台服务器?

任何帮助将不胜感激,谢谢

最佳答案

该错误是由您正在通信的服务器 mail.telkomsel.com 发送的。

为了防止垃圾邮件,大多数服务器不会接受发件人地址位于其域之外并且未经适当身份验证的电子邮件。错误代码 421 实际上意味着服务不可用。这是对垃圾邮件发送者说走开!的一种方式。

关于java - 我的 SMTP 发件人抛出错误 421,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20537757/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com