今天在写一个邮件程序:
申请了两个邮箱号:
然后开始写程序:代码如下:
package com.oristand.mail;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo {
/**
* @param args
* @throws MessagingException
*/
@SuppressWarnings("static-access")
public static void main(String[] args) throws MessagingException {
// TODO Auto-generated method stub
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Message message = new MimeMessage(session);
message.setText("你好,我是来自高歌用Java程序写的一封邮件");
message.setFrom(new InternetAddress("[email protected]"));
Transport transport = session.getTransport();
transport.connect("smtp.sohu.com",25,"ge_gao_sex", "123456");
transport.sendMessage(message, new Address[]{new InternetAddress("[email protected]")});
transport.close();
}
}
刚开始的时候一直报下面一个错误:
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.sohu.com", port 25, isSSL false
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.sohu.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1545)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:291)
at com.oristand.mail.Demo.main(Demo.java:36)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1511)
... 3 more
上网一搜,原来是麦咖啡把我自己写的发送邮件的程序,当成蠕虫了,不让链接邮件服务器。
把麦咖啡挺了以后:登录[email protected],看到一封邮件:
以下信息来自位于 smtp.sohu.com 上的邮件系统: 非常抱歉,您的邮件不能被发送到某些或全部收件人。附件为邮件原文和错误报告。 请和管理员联系,以获得更多的帮助。 请将错误报告作为附件一并发给管理员. 您可以删除自己的信件. 搜狐邮件系统 <[email protected]>: host mxnew-a.126.com[220.181.15.134] said: 553 Requested action not taken: no smtp MX only,mx4,IsmowKDrlQGYqphKwKi6Ew--.40756S2 1251519128 (in reply to MAIL FROM command) 并且有一个名为No subject.eml的附件,下载下来一看,内容是:
Received: from gaoge (unknown [123.103.67.35])
by smtp.sohu.com (Postfix) with ESMTP id 5852C98EBE8
for <[email protected]>; Sat, 29 Aug 2009 12:12:14 +0800 (CST)
From: [email protected]
Message-ID: <12282550.0.1251519140093.JavaMail.lucky@gaoge>
MIME-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
Date: Sat, 29 Aug 2009 12:12:14 +0800 (CST)
To: undisclosed-recipients:;
xOO6w6OsztLKx8C019S437jo08NKYXZhs8zQ8tC0tcTSu7fi08q8/g==
但是向其他的邮件发邮件,比如说新浪,搜狐都是可以正常发邮件的!