使用Java应用程序发送 E-mail 十分简单,但是首先你应该在你的机器上安装 JavaMail API 和Java Activation Framework (JAF) 。
您可以从 Java 网站下载最新版本的 JavaMail,打开网页右侧有个 Downloads 链接,点击它下载。
下载并解压缩这些文件,在新创建的顶层目录中,您会发现这两个应用程序的一些 jar 文件。您需要把 mail.jar 和 activation.jar 文件添加到您的 CLASSPATH 中。
如果是maven项目,只需依赖mail这个jar,它会传递依赖activation
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.5</version>
- </dependency>
-
- 本实例以 QQ 邮件服务器为例,你需要在登录QQ邮箱后台在"设置"=》账号中开启POP3/SMTP服务()
- 项目结构:

配置文件 mail.properties(也是可以不用配置文件,直接传入参数):

发送邮件的帮助类(EmailHelper):
- import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.io.UnsupportedEncodingException;import java.util.Properties;import java.util.ResourceBundle;/**
- * @author hh
- * @Date 2018/11/7 */public class EmailHelper {/** * 获取配置文件 */private static final ResourceBundle bundle = ResourceBundle.getBundle("mail");/** * 发件人邮件用户名(发件人) */private static final String sendFrom = bundle.getString("email.from");/** * 发件人邮件授权码 */private static final String password = bundle.getString("password");/** * 指定发送邮件的主机为 smtp.qq.com (QQ 邮件服务器) */private static final String host = bundle.getString("email.host");/** * 发件人昵称 */private static final String nickName = bundle.getString("email.nickName");/** *
- * @param addressList 邮箱地址(多个以逗号隔开)
- * @param emailTitle 邮件标题
- * @param content 邮件内容 */public static void sendEmail(String addressList, String emailTitle, String content)throws MessagingException,UnsupportedEncodingException {// 步骤一 创建与邮件服务器连接会话// 配置与服务器连接参数Properties props = new Properties();// 设置properties 属性//服务器的本地主机(我用的是qq邮箱)smtp.qq.comprops.setProperty("mail.host", host);// 连接认证props.setProperty("mail.smtp.auth", "true");//发邮件前进行身份校验Authenticator authenticator = new Authenticator(){
- @Overridepublic PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(sendFrom,password);
- }
- };// 与邮件服务器连接会话Session session = Session.getDefaultInstance(props,authenticator);
-
- session.setDebug(true);// 步骤二 编写Message(邮件)Message message = new MimeMessage(session);//发送人昵称String personal = new String(nickName.getBytes("ISO-8859-1"),"utf-8");// from字段(发送人邮箱地址)message.setFrom(new InternetAddress(sendFrom,personal,"utf-8"));// to 字段(收件人邮箱地址)InternetAddress.parse():解析字符串成InternetAddress addressList:可以逗号隔开,同时给多个人发送 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addressList));// subject字段(邮件标题) message.setSubject(emailTitle);// 邮件正文内容 (可以发送HTML 内容)message.setContent(content,"text/html;charset=UTF-8");//步骤三 使用Transport发送邮件 Transport.send(message);
- }
- }
程序入口(AppMain):
- import javax.mail.MessagingException;import java.io.UnsupportedEncodingException;/**
- * @author hh
- * @Date 2018/11/7 */public class AppMain {public static void main(String[] args) {
- String content = "<h1>This is actual message</h1>";try {
- EmailHelper.sendEmail("692262908@qq.com", "title", content);
- } catch (MessagingException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
-
- }
- }
运行结果(收件人邮箱):

- 一篇讲JavaMail的博客,讲的挺不错的:Link