分享

java QQ 邮件发送代码

 魏正钦的图书馆 2016-10-19
public class EmailDemo {

public static void main(String[] args) throws AddressException, MessagingException {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");//链接时的协议
props.setProperty("mail.smtp.host", "smtp.qq.com");//主机
props.setProperty("mail.smtp.port","465");//主机名
props.setProperty("mail.smtp.auth","true");//是否开启权限控制
props.setProperty("mail.smtp.ssl.enable","true");//是否开启权限控制
props.setProperty("mail.debug","true");//如果为true则会发送邮件是打印发送的信息
//创建程序到邮件服务器的第一次回话,即家到邮局的路
Session session = Session.getInstance(props);
//获取邮件对象
MimeMessage message = new MimeMessage(session);//获取信封
message.setFrom(new InternetAddress("1801895783@qq.com"));//写上发送人,即发送方
message.setRecipients(RecipientType.TO, new InternetAddress[]{new InternetAddress("1035230962@qq.com")});//写上收件人,第二个参数为数组,因为收件人可以有多个
message.setSubject("这是一份来自java程序的第一份信!");
message.setText(" Hello Word!");
//开始创建邮递员
Transport transport=session.getTransport();
transport.connect("1801895783@qq.com","gfmqtpibidlffaec");//告诉邮递员您的用户名和证明
transport.sendMessage(message, message.getAllRecipients());//邮递员将信送出
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多