1.示例1
public class Demo1 {
/**
* 邮件发送 方式一
* 步骤:1.创建卫星:Message
* 2.创建火箭:Transport
* 3.火箭发射卫星:Transport.sendMessage();
* @param args
*/
public static void main(String[] args) throws Exception{
/**
* 创建卫星
*/
Properties props = new Properties();
//认证方式:判断用户名和密码
props.setProperty("mail.smtp.auth", "true");
//传输协议smtp
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
//控制台可以看见发射过程
session.setDebug(true);
Message msg = new MimeMessage(session);
//创建发送方,和信件的内容
msg.setFrom(new InternetAddress(("qd_qiaodong@sina.com")));
msg.setText("hello world");
/**
* 创建火箭
*/
Transport tp = session.getTransport();
//连接新浪服务器
tp.connect("smtp.sina.cn", 25, "qd_qiaodong@sina.com","qiao18713515228d");
/**
* 发送卫星
*/
tp.sendMessage(msg, new Address[]{new InternetAddress("573717339@qq.com")});
tp.close();
}
}
2.示例二
public class Demo2 {
/**
* @param 邮件发送方式二
*/
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");//认证方式Login
props.setProperty("mail.transport.protocol", "smtp");//传输协议
props.setProperty("mail.host", "smtp.sina.cn");//发送端要连接的服务器
//每次都会创建一个新的session,直接认证用户名和密码
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("qd_qiaodong@sina.com", "qiao18713515228d");
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
//信件的内容
msg.setFrom(new InternetAddress("qd_qiaodong@sina.com"));
msg.setSubject("mail test");
msg.setContent("<span style='color:red'>hello world !</span> ","text/html");
/**
* 发送方式:正常发送。
* 还可以暗送,抄送
*/
msg.setRecipient(RecipientType.TO, new InternetAddress("573717339@qq.com"));
/**
* 创建火箭并发送
*/
Transport transport = session.getTransport();
Transport.send(msg);
transport.close();
}
}
3.复杂邮件的发送
public class Demo3 {
/**
* 步骤:1.构建邮件信息
* --创建出message
* --设置发件人,主题,收件人
* 2.构建邮件的复杂体
* --附件
* --设置附件所需的资源信息
* --正文(包括文本和图片信息)
* --构建正文的关联体系
* --纯文本
* --图片
* 3.生成复杂文件 msg.saveChanges();
* 4.发送邮件
*/
public static void main(String[] args) throws Exception{
//构建邮件信息
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");//认证方式Login
props.setProperty("mail.transport.protocol", "smtp");//传输协议
props.setProperty("mail.host", "smtp.sina.cn");//发送端要连接的服务器
//每次都会创建一个新的session,直接认证用户名和密码
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("qd_qiaodong@sina.com", "qiao18713515228d");
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
//发件人
msg.setFrom(new InternetAddress("\""+MimeUtility.encodeText("乔栋新浪")+"\" <qd_qiaodong@sina.com>"));
msg.setSubject("hello mail test");
//对方接受邮件后,回复发件人时,可以在这指定
msg.setReplyTo(new Address[]{new InternetAddress("qd_qiaodong@sina.com")});
//设置邮件收件人(可以设置多个)
msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("乔栋QQ")+" <573717339@qq.com>,"+MimeUtility.encodeText("乔栋QQ2")+" <573717339@qq.com>"));
/**
* 构建邮件的复杂体
* --两个附件
* --一个正文(它也是复杂体)
*/
MimeMultipart msgmultipart = new MimeMultipart("mixed");
msg.setContent(msgmultipart);
//附件。。
MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
//正文内容。。
MimeBodyPart content = new MimeBodyPart();
//将这三部分加入复杂体当中
msgmultipart.addBodyPart(attch1);
msgmultipart.addBodyPart(attch2);
msgmultipart.addBodyPart(content);
//第一个附件
DataSource ds1 = new FileDataSource("test.txt");
DataHandler dh1 = new DataHandler(ds1);
attch1.setDataHandler(dh1);
attch1.setFileName("test.txt");//设置文件名
//第一个附件
DataSource ds2 = new FileDataSource("测试.txt");
DataHandler dh2 = new DataHandler(ds2);
attch2.setDataHandler(dh2);
//attch2.setFileName("");//设置文件名,不能设置中文名
//可以设置中文文件名
attch2.setFileName(MimeUtility.encodeText("测试.txt"));
/**
* 设置正文
* --包括文字
* --图片
*/
MimeMultipart bodymultipart = new MimeMultipart("related");
content.setContent(bodymultipart);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bodymultipart.addBodyPart(htmlPart);
bodymultipart.addBodyPart(gifPart);
//图片
DataSource gifds = new FileDataSource("resource\\logo.gif");
DataHandler gifdh = new DataHandler(gifds);
gifPart.setDataHandler(gifdh);
gifPart.setHeader("Content-Location", "http://www.itcast.cn/logo.gif");
//纯文本
htmlPart.setContent("你好啊","text/html;charset=gbk");
//生成复杂文本
msg.saveChanges();
Transport.send(msg);
/*OutputStream is = new FileOutputStream("");
msg.writeTo(is);
is.close();*/
}
}
4.直接发送outlook生成的邮件
public class Demo4 {
/**
* @param 将现有生成的邮件发送出去
*/
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");//认证方式Login
props.setProperty("mail.transport.protocol", "smtp");//传输协议
props.setProperty("mail.host", "smtp.sina.cn");//发送端要连接的服务器
//每次都会创建一个新的session,直接认证用户名和密码
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("qd_qiaodong@sina.com", "qiao18713515228d");
}
});
session.setDebug(true);
Message msg = new MimeMessage(session,new FileInputStream("outlook生成的邮件"));
//parse会将一串字符串,解析成Address数组
Transport.send(msg,InternetAddress.parse("573717339@qq.com"));
}
}