通过java程序给qq邮箱发送信息。
1、第一步:下载mail的jar包:javax.mail.jar
下载地址:https://javaee.github.io/javamail/#Latest_News
2、直接使用下面的代码
public static voidsendMessages() {try{//创建Properties 类用于记录邮箱的一些属性
final Properties props = newProperties();//表示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", "你的qq号@qq.com");//此处的密码就是前面说的16位STMP口令//获取口令
props.put("mail.password", "你的口令");//构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = newAuthenticator() {protectedPasswordAuthentication getPasswordAuthentication() {//用户名、密码
String userName = props.getProperty("mail.user");
String password= props.getProperty("mail.password");return newPasswordAuthentication(userName, password);
}
};//使用环境属性和授权信息,创建邮件会话
Session mailSession =Session.getInstance(props, authenticator);//创建邮件消息
MimeMessage message = newMimeMessage(mailSession);//设置发件人
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);//设置收件人的邮箱
InternetAddress to = new InternetAddress("收件人邮箱@qq.com");
message.setRecipient(RecipientType.TO, to);//设置邮件主题
message.setSubject("测试邮件");//设置消息日期
message.setSentDate(newDate());//html文件
StringBuilder sb = newStringBuilder();
sb.append("
文本内容
");//设置邮件的内容体message.setContent(sb.toString(), "text/html;charset=UTF-8");//最后当然就是发送邮件
Transport.send(message);
System.out.println("发送成功!");
}catch(AddressException e) {
System.out.println("发送失败!"+e.getMessage());
e.printStackTrace();
}catch(MessagingException e) {
System.out.println("发送失败!"+e.getMessage());
e.printStackTrace();
}
}
3、上面代码中主要填写三个地方
①:发送的邮箱号
②:发送的邮箱号的口令(在下面会介绍如何获取)
③:接收人的邮箱号
4、这样就可以发送邮箱给别人了。
代码参考:https://blog.csdn.net/qq_41750725/article/details/83831589
5、如何获取发送人的口令
①:登录发送方的qq邮箱-》找到账号
②:拉到下面,找到需要开启的服务,然后把相应的服务开启即可。
③:开启之后,就会获取到一个16位的口令了。
④:具体参考:https://jingyan.baidu.com/article/fedf0737af2b4035ac8977ea.html
上面的方法只是以qq邮箱为例子,其他的邮箱也是差不多同样的操作。下面提供各个邮箱的邮件服务器和端口号
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)
126邮箱:
POP3服务器地址:pop.126.com(端口:110)
SMTP服务器地址:smtp.126.com(端口:25)
139邮箱:
POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:SMTP.139.com(端口:25)163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
QQ邮箱
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
QQ企业邮箱
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587HotMail
POP3服务器地址:pop3.live.com (端口:995)
SMTP服务器地址:smtp.live.com (端口:587)
gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)
china.com:
POP3服务器地址:pop.china.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com
具体参考:https://blog.csdn.net/smok56888/article/details/50070453#
原文:https://www.cnblogs.com/masha2017/p/11122879.html