需要下载的JAR包:
JavaMail:http://www.oracle.com/technetwork/java/javamail/index.html
JAF:http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html
加入的包有:mail.jar activation.jar
你要准备一个邮箱账户,就用网易的吧,然后打开SMTP功能
关于网易的一些端口信息参考下面:
每个公司都是不一样的,使用SSL的和不实用的也不一样
使用的开发和编译环境是JDK1.4,如果你使用的是JDK1.5环境,那么你的环境需要修改一些东西,找到 javaee.jar 删除里面的 mial 包,否则会有冲突
否则会报错:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream at javax.mail.Session.loadProvidersFromStream(Session.java:928) at javax.mail.Session.access$000(Session.java:174) at javax.mail.Session$1.load(Session.java:870) at javax.mail.Session.loadResource(Session.java:1084) at javax.mail.Session.loadProviders(Session.java:889) at javax.mail.Session.<init>(Session.java:210) at javax.mail.Session.getInstance(Session.java:249) at com.mail.TextMail.main(TextMail.java:22)
下面我直接贴代码了
package com.mail; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; // 如果你使用JDK1.4以上运行 会出现 Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream // 需要你找到 javaee.jar 删掉 里面的 mail 包 public class TextMail { public static void main(String[] args) throws Exception { // 配置文件对象 Properties props = new Properties(); // 邮箱服务地址 props.put( "mail.smtp.host ", "smtp.163.com "); // 是否进行验证 props.put("mail.smtp.auth", "true"); // 创建一个会话 Session session = Session.getInstance(props); // 打开调试,会打印与邮箱服务器回话的内容 session.setDebug(true); Message message = new MimeMessage(session); // 如果发送人没有写对,那么会出现 javamail 550 Invalid User // 如果发送人写的和使用的帐号不一致,则会出现 553 Mail from must equal authorized user InternetAddress from = new InternetAddress("[email protected]"); from.setPersonal(MimeUtility.encodeText("java小强<[email protected]>")); message.setFrom(from); InternetAddress to = new InternetAddress("[email protected]"); message.setRecipient(Message.RecipientType.TO, to); message.setSubject(MimeUtility.encodeText("强哥邀请,谁敢不从!")); message.setText("强哥邀请你访问我的博客:http://cuisuqiang.iteye.com/!"); message.setSentDate(new Date()); Transport transport = session.getTransport("smtp"); // 具体你使用邮箱的smtp地址和端口,应该到邮箱里面查看,如果使用了SSL,网易的端口应该是 465/994 transport.connect("smtp.163.com", 25, "test20120711120200", "test123456"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("发送完毕"); } }
需要注意的有
InternetAddress from = new InternetAddress("[email protected]");
和
transport.connect("smtp.163.com", 25, "test20120711120200", "test123456");
这两个邮箱的地址必须相同,否则会出现
Exception in thread "main" javax.mail.MessagingException: 553 Mail from must equal authorized user at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388) at com.mail.TextMail.main(TextMail.java:38)
你上网查都是说用户验证不对,其实一般大家都是按照别人代码拷贝的,然后修改了用户和密码,怎么会不对!
我这个账户和密码都是可以用的,对不起网易了!
由于上面我设置了打印调试,所以你在控制台会看到如下内容:
DEBUG: setDebug: JavaMail version 1.3.1 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.163.com", port 25 220 163.com Anti-spam GT for Coremail System (163com[20111010]) DEBUG SMTP: connected to host "smtp.163.com", port: 25 EHLO cuisuqiang 250-mail 250-PIPELINING 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN PLAIN 250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrgdNesUCa0xDrUUUUj 250-STARTTLS 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN" DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrgdNesUCa0xDrUUUUj" DEBUG SMTP: Found extension "STARTTLS", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Attempt to authenticate AUTH LOGIN 334 dXNlcm5hbWU6 dGVzdDIwMTIwNzExMTIwMjAw 334 UGFzc3dvcmQ6 dGVzdDEyMzQ1Ng== 235 Authentication successful DEBUG SMTP: use8bit false MAIL FROM:<[email protected]> 250 Mail OK RCPT TO:<[email protected]> 250 Mail OK DEBUG SMTP: Verified Addresses DEBUG SMTP: [email protected] DATA 354 End data with <CR><LF>.<CR><LF> Message-ID: <14518194.1341995865564.JavaMail.Administrator@cuisuqiang> Date: Wed, 11 Jul 2012 16:37:45 +0800 (CST) From: "=?GBK?Q?java=D0=A1=C7=BF<[email protected]>?=" <[email protected]> To: [email protected] Subject: =?GBK?B?x7+459H7x+ujrMutuNKyu7TTo6E=?= Mime-Version: 1.0 Content-Type: text/plain; charset=GBK Content-Transfer-Encoding: quoted-printable =C7=BF=B8=E7=D1=FB=C7=EB=C4=E3=B7=C3=CE=CA=CE=D2=B5=C4=B2=A9=BF=CD=A3=BAhtt= p://cuisuqiang.iteye.com/=A3=A1 . 250 Mail OK queued as smtp2,DNGowEB52UhcO_1PZmMMCA--.937S2 1341995868 QUIT
这些都是程序与邮件服务商的一些交互信息!
最后:
由于近期大量小网站在未经同意情况下使用文章,现将我的博客地址公布如下,请您到ITEYE网站看原创,谢谢!