代码如下:
/Sample code for send mail.import java.net.*;
import java.io.*;public class SmtpClinent
{
public static void main(String[] args) throws Exception
{
InetAddress mailServer=InetAddress.getByName("localhost");
//InetAddress me=InetAddress.getByName("localhost");
Socket mailTo=new Socket(mailServer,25);
BufferedReader in=new BufferedReader(new InputStreamReader(mailTo.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(mailTo.getOutputStream()));
//首先和服务器25端口建立连接,服务器返回220表示连接成功
String initialID = in.readLine();
System.out.println(initialID);
//向服务器发送HELO name,用来表示发件人,服务器返回250
out.println("HELO me");
out.flush();
System.out.println(in.readLine());
//用MAIL FROM [email protected]告诉服务器你要发邮件
out.println("MAIL FROM:<erikchen1985@localhost>");
out.flush();
System.out.println(in.readLine());
//用RCPT TO:[email protected]
out.println("RCPT TO:<[email protected]>");
out.flush();
System.out.println(in.readLine());
//用DATA发送邮件正文
out.println("DATA");
out.flush();
System.out.println(in.readLine());
out.println("Hi Unni.");
out.flush();
out.println("I have sent this message to you through the mail program that I have written. ");
out.flush();
out.println("Mukesh");
out.flush();
out.println(".");
out.flush();
System.out.println(in.readLine());
//发送QUIT断开连接
out.println("QUIT");
out.flush();
System.out.println(in.readLine());
}
}我用的是Easy Smtp Server邮件服务器。
运行是返回的结果是:
220 Welcome to the Easy SMTP Server250 Hello me250 erikchen1985@localhost Address Okay250 [email protected] Address Okay354 Please start mail input.250 Mail queued for delivery.221 Closing connection. Good bye.但我的邮箱并没有收到邮件,请问为什么?
还有,我的SmtpServer设置因该是没有问题的,用OutLook作为客户端是可以成功(OutLook的smtp设置为localhost)。
/Sample code for send mail.import java.net.*;
import java.io.*;public class SmtpClinent
{
public static void main(String[] args) throws Exception
{
InetAddress mailServer=InetAddress.getByName("localhost");
//InetAddress me=InetAddress.getByName("localhost");
Socket mailTo=new Socket(mailServer,25);
BufferedReader in=new BufferedReader(new InputStreamReader(mailTo.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(mailTo.getOutputStream()));
//首先和服务器25端口建立连接,服务器返回220表示连接成功
String initialID = in.readLine();
System.out.println(initialID);
//向服务器发送HELO name,用来表示发件人,服务器返回250
out.println("HELO me");
out.flush();
System.out.println(in.readLine());
//用MAIL FROM [email protected]告诉服务器你要发邮件
out.println("MAIL FROM:<erikchen1985@localhost>");
out.flush();
System.out.println(in.readLine());
//用RCPT TO:[email protected]
out.println("RCPT TO:<[email protected]>");
out.flush();
System.out.println(in.readLine());
//用DATA发送邮件正文
out.println("DATA");
out.flush();
System.out.println(in.readLine());
out.println("Hi Unni.");
out.flush();
out.println("I have sent this message to you through the mail program that I have written. ");
out.flush();
out.println("Mukesh");
out.flush();
out.println(".");
out.flush();
System.out.println(in.readLine());
//发送QUIT断开连接
out.println("QUIT");
out.flush();
System.out.println(in.readLine());
}
}我用的是Easy Smtp Server邮件服务器。
运行是返回的结果是:
220 Welcome to the Easy SMTP Server250 Hello me250 erikchen1985@localhost Address Okay250 [email protected] Address Okay354 Please start mail input.250 Mail queued for delivery.221 Closing connection. Good bye.但我的邮箱并没有收到邮件,请问为什么?
还有,我的SmtpServer设置因该是没有问题的,用OutLook作为客户端是可以成功(OutLook的smtp设置为localhost)。
220 Welcome to the Easy SMTP Server250 Hello me250 erikchen1985@localhost Address Okay250 [email protected] Address Okay354 Start mail input; end with <CRLF>.<CRLF>
在这里停了。要小数点。小数点有特殊的输入方法吗?我是“.”这个,在英文输入下的点号。
out.println("From:kingfish<[email protected]>"); //add
out.println("Hi Unni.");
out.println(
"I have sent this message to you through the mail program that I have written. ");
out.println("Mukesh");
out.println("\r\n."); //add
out.flush();