- 论坛徽章:
- 0
|
用JAVA的socket写了一个邮件发送程序,能顺利执行,但是最后老是返回“554 this mail is rejected by antispam system”,说是被当成垃圾邮件拒绝了,原因应该出在"HELO "+REMOTE_HOST。既然foxmail能否发送那必然有解决之道,大家有弄过这些东西吗?这个应该如何设置呢?多谢,请看代码
- package org.server;
- import java.io.*;
- import java.net.*;
- import sun.misc.*;
- public class MailSender
- {
- private String SMTP_SERVER = "smtp.189.cn";
- private String REMOTE_HOST = "127.0.0.1";
- private int SMTP_PORT = 25;
- private String SENDER = "*@189.cn";//发送者的邮件地址
- private String RECEIVER = "*@163.com";//接受者的邮件地址
- private Socket socket = null;
- private int WAIT_TIME = 60000;
- private InetAddress remoteAdd = null;
- private BufferedReader br = null;
- private PrintWriter pw = null;
- private String name = "name";
- private String pass = "pass";
-
- private void sendMail()
- {
- socket = new Socket();
- System.out.println("开始连接"+SMTP_SERVER+"...");
- long begin = System.currentTimeMillis();
- String subject = "Test";
- String msg = "Symbian OS";
- String content = "Subject:"+subject+"\r\n"+msg;
- try
- {
- socket.setReuseAddress(true);
- remoteAdd = InetAddress.getByName(SMTP_SERVER);
- SocketAddress address = new InetSocketAddress(remoteAdd,SMTP_PORT);
- socket.connect(address,WAIT_TIME);
- long end = System.currentTimeMillis();
- System.out.println("连接"+SMTP_SERVER+"成功...");
- System.out.println("耗时"+(end-begin)+"ms");
- br = getReader(socket);
- pw = getWriter(socket);
- //String REMOTE_HOST = InetAddress.getByName(SMTP_SERVER).toString();
- System.out.println("Client>"+REMOTE_HOST);
- String username = new BASE64Encoder().encode(name.getBytes());
- String password = new BASE64Encoder().encode(pass.getBytes());
- System.out.println("发送验证信息及邮件...");
- sendAndReceive(null,br,pw);
- sendAndReceive("HELO "+REMOTE_HOST,br,pw);
- sendAndReceive("AUTH LOGIN",br,pw);
- sendAndReceive(username,br,pw);
- sendAndReceive(password,br,pw);
- sendAndReceive("MAIL FROM:<"+SENDER+">",br,pw);
- sendAndReceive("RCPT TO:<"+RECEIVER+">",br,pw);
- sendAndReceive("DATA",br,pw);
- pw.println(content);
- System.out.println("Client>"+content);
- sendAndReceive(".",br,pw);
- sendAndReceive("QUIT",br,pw);
- }
- catch(SocketException err)
- {
- err.printStackTrace();
- }
- catch(UnknownHostException err)
- {
- err.printStackTrace();
- }
- catch(IOException err)
- {
- err.printStackTrace();
- }
- finally
- {
- if(br!=null)
- {
- try
- {
- br.close();
- }
- catch(IOException err)
- {
- err.printStackTrace();
- }
- }
- if(pw!=null)
- {
- pw.close();
- }
- if(socket!=null)
- {
- try
- {
- socket.close();
- }
- catch(IOException err)
- {
- err.printStackTrace();
- }
- }
- }
- }
-
- private PrintWriter getWriter(Socket socket)
- {
- OutputStream socketOut = null;
- try
- {
- socketOut = socket.getOutputStream();
- }
- catch(IOException err)
- {
- err.printStackTrace();
- }
- return new PrintWriter(socketOut,true);
- }
-
- private BufferedReader getReader(Socket socket)
- {
- InputStream socketIn = null;
- try
- {
- socketIn = socket.getInputStream();
- }
- catch(IOException err)
- {
- err.printStackTrace();
- }
- return new BufferedReader(new InputStreamReader(socketIn));
- }
- /*发送并接收服务器的响应数据*/
- private void sendAndReceive(String str,BufferedReader br,PrintWriter pw)
- {
- try
- {
- if(str!=null)
- {
- System.out.println("Client>"+str);
- pw.println(str);
- }
- String response;
- if((response=br.readLine())!=null)
- {
- System.out.println("Server>"+response);
- }
- }
- catch(IOException err)
- {
- err.printStackTrace();
- }
- }
-
- public static void main(String args[])
- {
- new MailSender().sendMail();
- }
- }
复制代码 |
|