java发送邮件
Posted yanan7890
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java发送邮件相关的知识,希望对你有一定的参考价值。
我测试用的QQ,需要到QQ邮箱里开通pop3服务
import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; public class SendMail private static final String MAIL_CC = "xxx@qq.com";// 邮件的抄送人 private static final String MAIL_BCC = "xxx@qq.com";// 邮件的密送人 // 设置邮件服务器 private static final String send_email_host_name = "smtp.qq.com"; // 设置安全连接端口号 private static final String send_email_ssl_port = "465"; // 发送Email用户名 private static final String send_email_user_name = "xxx@qq.com"; // 发送Email密码。即上面开通的pop3的授权码 private static final String send_email_user_pwd = "xxx"; // 发送Email编码 private static final String send_email_charset = "UTF-8"; public static void main(String[] args) throws Exception sendPlainEmail(send_email_user_name, "这是一个subject", "this is content"); /** * @todo 发送纯文本的Email * @param receiver 收信人 * @param subjecct Email主题 * @param msg Email内容 * @throws EmailException * @date 2019年7月31日 * @author yanan */ public static void sendPlainEmail(String receiver, String subjecct, String msg) throws EmailException SimpleEmail email = new SimpleEmail(); email.setHostName(send_email_host_name); email.setSSLOnConnect(true);// 设置使用安全连接 email.setSslSmtpPort(send_email_ssl_port); email.setAuthentication(send_email_user_name, send_email_user_pwd); try email.addTo(receiver, "receiver"); email.setCharset(send_email_charset); email.setFrom(send_email_user_name); email.addBcc(MAIL_BCC); email.addCc(MAIL_CC); email.setSubject(subjecct); email.setMsg(msg); email.send(); catch (EmailException e) throw new EmailException(e);
以上是关于java发送邮件的主要内容,如果未能解决你的问题,请参考以下文章