首页 > 代码库 > Java发送邮件
Java发送邮件
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发送邮件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。