java之发送mail


package com.datum.cod.framework;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.auth", "true");
        try {
            PopupAuthenticator auth = new PopupAuthenticator();
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            // 创建mime类型邮件
            MimeMessage message = new MimeMessage(session);
            // 设置发信人
            Address addressFrom = new InternetAddress(PopupAuthenticator.mailuser + "@qq.com", "so easy!");
            message.setFrom(addressFrom);
            // 设置主题
            message.setSubject("Test sent Mail by javaMail");
            // 设置邮件内容
            message.setText("哈哈!so easy!");
            // 设置收件人们
            Address addressTo = new InternetAddress("1872387570@qq.com", "hhh");
            message.addRecipient(Message.RecipientType.TO, addressTo);
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.qq.com", PopupAuthenticator.mailuser,PopupAuthenticator.password);
            transport.send(message);
            transport.close();
            System.out.println("sent suc");
        } catch (Exception e) {
            System.out.println(e.toString());
            System.out.println("sent fail");
        }
    }
}

class PopupAuthenticator extends Authenticator {
    public static final String mailuser = "1872387570";
    public static final String password = "aaaaaaa";

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(mailuser, password);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值