java 发qq邮件
来源:互联网 发布:电脑淘宝没有登陆页面 编辑:程序博客网 时间:2024/06/12 00:01
/*
1
需要jar包 JavaMail
地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-JPR
oracle需要登录下载,麻烦的可以点下面↓
我在csdn资源里也有:http://download.csdn.net/download/y895315416/10173526
但是需要2c币,因为最低设置2c币
import javax.mail.*;
2
授权码 在 设置 -> 账户-> 往下拉 找到 pop3 smtp ,开启,会得到16位授权码,需要手机发条短信
3 贴码
*/
import com.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.security.GeneralSecurityException;import java.util.Properties;/** * Created by 夜未央 on 2017/12/26. */public class SendEmail { public static void main(String [] args) throws GeneralSecurityException { // 收件人电子邮箱 String to = "xxxxxxxxxxxx@qq.com"; // 发件人电子邮箱 String from = "xxxxxxxxxxc@qq.com"; // 指定发送邮件的主机为 smtp.qq.com String host = "smtp.qq.com"; //QQ 邮件服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); // 获取默认session对象 Session session = Session.getDefaultInstance(properties,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("xxxxxxxx@qq.com", "授权码!!!需要设置开启pop3 smtp,授权码设置在上面"); //发件人邮件用户名、授权码 } }); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("标题"); // 设置消息体 message.setText("主体内容"); // 发送消息 Transport.send(message); System.out.println("发送成功"); }catch (MessagingException mex) { mex.printStackTrace(); } }}
阅读全文