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();        }    }}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电地暖安装多少钱 安装地暖多少钱 地暖安装要多少钱 地暖安装费用 装个地暖要多少钱 碳纤维电热地暖 专业地暖公司 地暖有哪些品牌 地暖多少钱一个平方 地暖造价多少钱一平方 天津地暖价格 安装地暖多少钱一平方 地暖哪个公司好 地暖是什么样的 pert地暖管价格 地暖铺设价格 地暖那个品牌比较好 燃气地暖的优缺点 电地暖怎么样 装地暖大概多少钱 家庭装修地暖 做地暖的价格 哪种电地暖好 地暖一个月费用 老房子装地暖 地暖每平米多少钱 石墨烯地暖哪家好 地暖锅炉价格 地暖需要哪些材料 地暖一般多少钱 电地暖有辐射吗 壁暖和地暖哪个好 地暖每平方多少钱 南京地暖价格 地暖管多少钱一米 地暖地板什么品牌好 安装地暖的价格 家里按了地暖后悔了 pert地暖管生产设备 地暖能用多少年 空调地暖一体机