第一个JavaMail程序
package cn.itcast.javamail2;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo1 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Properties props = new Properties();
//props.put("mail.smtp.auth", "false"); //认证
//props.put("mail.smtp.localhost", "localhost");
props.setProperty("mail.smtp.auth", "true"); //认证
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true); //加入的调试信息,有什么问题打印出来
Message msg = new MimeMessage(session); //制造卫星
msg.setText("JavaMail第一个程序!");
msg.setFrom(new InternetAddress("itjavawfc@sina.com"));
//这个步骤里面:1.创建Transport对象,2.连接服务器 3.发送Message 4.关闭连接
Transport transport = session.getTransport(); //制造火箭
transport.connect("smtp.sina.com", 25, "itjavawfc", "wfc868690");//连接服务器(主机,端口,用户名,密码)
transport.sendMessage(msg,
new Address[]{new InternetAddress("itjavawfc@sohu.com")}); //发送msg
//transport.send(msg,new Address[]{new InternetAddress("itcast_test@sohu.com")});
transport.close();
}
}
实验前的准备:
准备实验环境:
1提前在sina和sohu上开始一个免费账号
2获取smtp和pop3服务器的名称
3在outlook中配置sina账户
需要加载mailapi.jar到工程里去,这样才能调用JavaMail的类.
在工程里面创建lib文件-->复制mailapi.jar到lib文件中-->右击mailapi.jar-->buildpath到工程中去即可。
需要的工具下载地址:http://download.csdn.net/download/itjavawfc/7395459
运行程序即可:
如果入到这样的错误,
javax.mail.AuthenticationFailedException
解决:
必须保证:
新浪邮箱很好开通的,邮箱设置→账户→POP/SMTP设置,勾选“开启 POP3服务器: pop.sina.com,SMTP服务器: smtp.sina.com”,就可以了!
本实验结果:在搜狐邮箱里收到了自己从新浪邮箱发来的邮件。
OK!
第二个JavaMail程序
可以与第一个程序比较一下,特别是代码方面.此程序可以实现群发的功能
package cn.itcast.javamail2;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Demo2 {
public static void main(String[] args) throws Exception{
//创建Session对象,一盘情况下,一二句必须的。
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true"); //认证,你要使用你的邮箱去发送邮件,你的你的邮箱所在的服务器必须得到认证
props.setProperty("mail.transport.protocol", "smtp");//服务器的协议
props.setProperty("mail.host", "smtp.sina.com"); //提供服务器的名称和端口,采用了默认的服务器
Session session=Session.getInstance(props, //策略模式的运用
new Authenticator(){ //验证对象返回器,这个类覆盖了父类的方法,返回对象的用户名和密码
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("itjavawfc","wfc868690");
}
}
);
session.setDebug(true); //加入的调试信息,有什么问题打印出来
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("itjavawfc@sina.com"));
msg.setSubject("中文主题"); //主题
msg.setRecipients(RecipientType.TO, InternetAddress.parse("itjavawfc@sohu.com,335441537@qq.com"));
//我们Transport发送的时候并没有指定发送的目的地,我们在msg中可以先给出地址,这样可以实现群发
msg.setContent("<span style='color:red'>中文哦,汪方晨的第二个JavaMail程序</span>", "text/html;charset=utf-8"); //设置msg的类型
Transport.send(msg);
}
}