package cn.itcast;
import java.io.File;
import java.util.Properties;
import javax.mail.Authenticator;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.junit.Test;
public class Demo1 {
//@Test
public void fun8() throws Exception{
// 创建Properties 类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth","true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587
props.put("mail.smtp.port", "587");
// 此处填写你的账号
props.put("mail.user", "19yjuyjgyjg652@qq.com");
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", "annfuwdkyqdrccif");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = "1ghgjgjg652@qq.com";
String password = "annfuwdkyqdrccif";
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// MimeMultipart list = new MimeMultipart();//创建多部分内容
// 创建MimeBodyPart
//MimeBodyPart part1 = new MimeBodyPart();
// 设置收件人的邮箱
InternetAddress to = new InternetAddress("1787iui210@qq.com");
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置邮件的内容体
message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");
// 最后当然就是发送邮件啦
Transport.send(message);
}
@Test//带附件的邮件发送
public void fun9() throws Exception{
// 创建Properties 类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth","true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587
props.put("mail.smtp.port", "587");
// 此处填写你的账号
props.put("mail.user", "1jkhuiyuo652@q q.com");
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", "annfuwdkyqdrccif");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = "xxxxxxxxx@qq.com";
String password = "xxxjmhjxxwwfghgfhif"//不是邮箱的密码,是身份验证码
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
MimeMultipart list = new MimeMultipart();//创建多部分内容
// 创建MimeBodyPart
MimeBodyPart part1 = new MimeBodyPart();
// 设置收件人的邮箱
InternetAddress to = new InternetAddress("1ttty76i7i872210@qq.com");
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置主体部件的内容体
part1.setContent("这是一封测试邮件", "text/html;charset=UTF-8");
// 把主体部件添加到集合中
list.addBodyPart(part1);
// 创建MimeBodyPart
MimeBodyPart part2 = new MimeBodyPart();
part2.attachFile(new File("C:/Users/h/ictures/timg.jpeg"));//设置附件的内容
part2.setFileName(MimeUtility.encodeText("大美女.jpg"));//设置显示的文件名称,其中encodeText用来处理中文乱码问题
list.addBodyPart(part2);
message.setContent(list);//把它设置给邮件作为邮件的内容。
// 最后当然就是发送邮件啦
Transport.send(message);
}
}
import java.io.File;
import java.util.Properties;
import javax.mail.Authenticator;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.junit.Test;
public class Demo1 {
//@Test
public void fun8() throws Exception{
// 创建Properties 类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth","true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587
props.put("mail.smtp.port", "587");
// 此处填写你的账号
props.put("mail.user", "19yjuyjgyjg652@qq.com");
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", "annfuwdkyqdrccif");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = "1ghgjgjg652@qq.com";
String password = "annfuwdkyqdrccif";
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// MimeMultipart list = new MimeMultipart();//创建多部分内容
// 创建MimeBodyPart
//MimeBodyPart part1 = new MimeBodyPart();
// 设置收件人的邮箱
InternetAddress to = new InternetAddress("1787iui210@qq.com");
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置邮件的内容体
message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");
// 最后当然就是发送邮件啦
Transport.send(message);
}
@Test//带附件的邮件发送
public void fun9() throws Exception{
// 创建Properties 类用于记录邮箱的一些属性
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth","true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587
props.put("mail.smtp.port", "587");
// 此处填写你的账号
props.put("mail.user", "1jkhuiyuo652@q q.com");
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", "annfuwdkyqdrccif");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = "xxxxxxxxx@qq.com";
String password = "xxxjmhjxxwwfghgfhif"//不是邮箱的密码,是身份验证码
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
MimeMultipart list = new MimeMultipart();//创建多部分内容
// 创建MimeBodyPart
MimeBodyPart part1 = new MimeBodyPart();
// 设置收件人的邮箱
InternetAddress to = new InternetAddress("1ttty76i7i872210@qq.com");
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置主体部件的内容体
part1.setContent("这是一封测试邮件", "text/html;charset=UTF-8");
// 把主体部件添加到集合中
list.addBodyPart(part1);
// 创建MimeBodyPart
MimeBodyPart part2 = new MimeBodyPart();
part2.attachFile(new File("C:/Users/h/ictures/timg.jpeg"));//设置附件的内容
part2.setFileName(MimeUtility.encodeText("大美女.jpg"));//设置显示的文件名称,其中encodeText用来处理中文乱码问题
list.addBodyPart(part2);
message.setContent(list);//把它设置给邮件作为邮件的内容。
// 最后当然就是发送邮件啦
Transport.send(message);
}
}