今天用到commons-email-1.2.jar来发邮件
先导入commons-email-1.2.jar包,类如下:
import org.apache.commons.mail.SimpleEmail;
public class SendEmail {
public static void main(String args[]) {
System.out.println("Begin Send email");
// TODO Auto-generated method stub
try {
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.xxx.com.cn");
email.addTo("[email protected]", "xxx");
email.setFrom("[email protected]", "testMe");
email.setSubject("hello");
email.setMsg("hello");
email.send();
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是却一直抛异常:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : mail.xxx.com.cn:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at SendEmail.main(SendEmail.java:18)
Caused by: javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:768)
at javax.mail.Session.getTransport(Session.java:708)
at javax.mail.Session.getTransport(Session.java:651)
at javax.mail.Session.getTransport(Session.java:631)
at javax.mail.Session.getTransport(Session.java:686)
at javax.mail.Transport.send0(Transport.java:166)
at javax.mail.Transport.send(Transport.java:98)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
... 2 more
首先是jar包的问题,因为不是只下载commons-mail的jar包,如果是jdk1.5或者1.5以下的版本,还需要下载sun的mail.jar和jaf的jar包,如果是jdk6的话只需要mail.jar就可以了。具体下载地址去sun网站自己找找就行了,
然后就是要开启邮箱的smtp服务,具体的进入邮箱进行设置就可以了。一般都很简单。
我用的是jdk1.6,所以只用导入mail.jar就行,导入mail.jar后问题又来了,
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
at javax.mail.internet.MimeUtility.<clinit>(MimeUtility.java:1161)
at javax.mail.internet.InternetAddress.setPersonal(InternetAddress.java:216)
at org.apache.commons.mail.Email.createInternetAddress(Email.java:589)
at org.apache.commons.mail.Email.addTo(Email.java:727)
at org.apache.commons.mail.Email.addTo(Email.java:710)
at SendEmail.main(SendEmail.java:14)
不要看到NoClassFound就以为是缺少包,如果是包的冲突,也会有这种错误的。
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
当出现以上错误时,恭喜您已经离接收邮件不远了,否则请您解决好所有的异常后再来看这个帖子。
javax.mail和javax.activation这两个包已经在javaEE5当中属于基础包了,就是JDK中自带了已经,但是里面的方法与现在外面的mail.jar和activation.jar有一些出入,所以初学者在直接copy别人代码的时候往往会出现上面的错误。
废话不多说下面是解决方法
进到
C:\myeclipse6.0\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710\data\libraryset\EE_5
这个路径里,可以看到javaee.jar,用rar把这个文件打开,然后进到javax文件夹里,删除mail和activation两个文件夹(删掉就OK,不过要注意备份一下)
删掉之后运行上面的代码,经行简单的修改以后就可以实现接收邮件的功能了!
以下是163, yahoo和gmail的例子,以供参考
163和yahoo的类似,只需要改smtp服务器就行
SimpleEmail email = new SimpleEmail();
try {
email.setHostName("smtp.163.com"); //Yahoo的服务器地址是:smtp.mail.yahoo.com
email.addTo("发送到的地址", "John Doe");
email.setFrom("发送的邮件地址", "Mr Smith");
email.setAuthentication("用户名", "密码");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gmail跟其他的邮箱的不同之处在于它的默认端口不是普通的25,而是465,所以需要在程序中动态设置一下,设置的具体方法就是:
email.setSSL(Boolean.TRUE); // 设定是否使用SSL
email.setSslSmtpPort("465"); // 设定SSL端口
SimpleEmail email = new SimpleEmail();
try {
email.setHostName("smtp.gmail.com");
email.setSSL(Boolean.TRUE); // 设定是否使用SSL
email.setSslSmtpPort("465"); // 设定SSL端口
email.addTo("发送到的地址", "John Doe");
email.setFrom("发送的邮件地址", "Mr Smith");
email.setAuthentication("用户名", "密码");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果gmail出了这种错误基本上就是没设置端口的原因:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at SendEmail.main(SendEmail.java:24)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 21sm7388726ywh.46
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
... 2 more
希望对大家能有所帮助