码迷,mamicode.com
首页 > 编程语言 > 详细

Java发送邮件

时间:2017-03-07 19:17:12      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:host   技术分享   util   发送   return   cep   javamail   vax   nic   

2017-03-07

首先需要导入javax.mail.jar包,下载地址为https://java.net/projects/javamail/pages/Home

163邮箱需要打开客户端授权码

技术分享

 

import java.util.Properties;

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

public class Email {
    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");//设置发送主机
        props.setProperty("mail.smtp.auth", "true");// 设置是否需要验证
        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("hfutlj", "********");//密码为163客户端授权码,不是登录密码
            }
        };
        Session session = Session.getInstance(props, auth);
        MimeMessage message = new MimeMessage(session);
        String nick = "";
        nick = javax.mail.internet.MimeUtility.encodeText("张三");//昵称的处理,否则为乱码
        message.setFrom(new InternetAddress(nick + "<hfutlj@163.com>"));
        message.setRecipient(RecipientType.TO, new InternetAddress("12345678@qq.com"));
        message.setRecipient(RecipientType.CC, new InternetAddress("87654321@qq.com"));

        message.setSubject("测试邮件");
        message.setContent("注意正文和标题部分不能写test、helloword等否则会被163视为垃圾邮件,报534错误", "text/html;charset=utf-8");

        Transport.send(message);
    }

}

 

Java发送邮件

标签:host   技术分享   util   发送   return   cep   javamail   vax   nic   

原文地址:http://www.cnblogs.com/hfutlj/p/6516164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!