这个小东西大概是在某一个很闲的暑假写的吧。
突然觉得莫得意思,想整整同学。
曾经写过给某位同学连续发消息,虽然很好玩,但是对方就知道我是谁了。
想来想去,邮箱是一个很好的选择,建一个小号,给谁发都可以,而对方又不知道我是谁。
准备
1.打开qq邮箱,设置->账户->打开POP3/SMTP服务
2.导入jar包
commons-email-1.5.jar和javax.mail.jar
如果不会导包请看 https://blog.csdn.net/qq_43176366/article/details/102533431
代码
public class SendQQMailUtil{
public static void main(String[] agrs) throws EmailException{
final SendQQMailUtil s=new SendQQMailUtil();
new Thread(){
public void run(){
for(int i=0;i<5;i++){
s.sendEmail("hello","愿时光静好 岁月无忧");
System.out.println("发送成功!");
}
}
}.start();
}
public void sendEmail(String title ,String context){
/**
* 第一步,确定连接
* 因为这一步骤涉及到的信息比较多,所以这里用Properties保存,在接下来使用的时候会很方便,直接在Properties环境下创建邮件
* */
final Properties props=new Properties(); //记录邮箱的一些属性
props.put("mail.smtp.auth", "true"); //表示使用SMTP发送邮件,须经过身份验证
props.put("mail.smtp.host", "smtp.qq.com"); //此处填写SMTP服务器,我们是用QQ,所以写QQ邮箱的服务器
props.put("mail.smtp.port", "587"); //端口号,QQ邮箱提供了两个端口,随意选一个,此处用587
props.put("mail.user", "########.qq.com"); // 你的QQ账号,即发件人
props.put("mail.password", "########"); // 此处的密码是腾讯提供的16位STMP口令
/**
* 第二步,验证/获得权限
* 构建授权信息,用于SMTP身份验证
* */
Authenticator authenticator = new Authenticator() {
//返回一个PasswordAuthentication类型的对象
protected PasswordAuthentication getPasswordAuthentication() {
String userName = props.getProperty("mail.user");
String userPwd = props.getProperty("mail.password");
return new PasswordAuthentication(userName, userPwd); //将读取到的用户名和密码拿过来
}
};
/**
* 第三步,创建邮件
* 使用环境属性和授权信息,创建邮件会话
* */
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession); //通过Session对象创建邮件消息
/**
* 第四步,设置发件人
* */
// InternetAddress sendUser = null;
// try {
// sendUser = new InternetAddress(props.getProperty("mail.user"));
// message.setFrom(sendUser); //通过MimeMessage对象的setFrom()方法设置发件人,需要一个InternetAddress对象
// } catch (MessagingException e) {
// e.printStackTrace();
// }
String nick="";
try {
nick=javax.mail.internet.MimeUtility.encodeText("一代风流~");
message.setFrom(new InternetAddress(nick+" <"+props.getProperty("mail.user")+">"));
} catch (Exception e) {
e.printStackTrace();
}
/**
* 第五步,设置收件人
* */
InternetAddress getUser = null;
try {
getUser = new InternetAddress("########@qq.com");
/* Address[] addresses=new Address[]{
new InternetAddress("收件人@qq.com")
};*/
//Address[] addresses=new Address[]{InternetAddress对象};
//Message.RecipientType.TO代表收件人
//Message.RecipientType.CC代表抄送
//Message.RecipientType.BCC代表暗送 用于群发的时候,不让别人看到其他的收件人
//第二个参数是收件人的地址,也可以是一个Address[]数组,用于群发的场景,群发用message.setRecipients(Message.RecipientType.TO, Address[])
message.setRecipient(Message.RecipientType.TO, getUser); //通过MimeMessage对象的setRecipient()方法设置收件人,需要一个InternetAddress对象
} catch (MessagingException e) {
e.printStackTrace();
}
/**
* 第六步,设置邮件标题和内容
* */
try {
// 设置邮件标题
//message.setSubject("测试邮件");
message.setSubject(title);
// 设置邮件的内容体
//message.setContent("通过Java发送QQ邮件", "text/html;charset=UTF-8");
message.setContent(context,"text/html;charset=UTF-8");
/**
* 最后一步,发送邮件,需要一个邮件对象作为参数
* */
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
顺便一提
谁要是拿这个轰我的邮箱 我就锤死他!!!(顶多轰我5条,不能再多了)