利用java mail发送QQ邮件

public class MailTool {
    public static void main(String[] args) throws MessagingException, GeneralSecurityException {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        msg.setSubject("seenews 错误");
        StringBuilder builder = new StringBuilder();
        builder.append("测试");
        builder.append("\nHelloWorld");
        builder.append("\n时间 " + TimeTool.getCurrentTime());
        msg.setText(builder.toString());
        msg.setFrom(new InternetAddress("**发送人的邮箱地址**"));

        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");//POP3

        transport.sendMessage(msg, new Address[] { new InternetAddress("**接收人的邮箱地址**") });
        transport.close();
    }
}
代码下载地址http://download.csdn.net/detail/zhuyingya/9730596
发布了17 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zhuyingya/article/details/54137861