JavaMail发送邮件至outlook邮箱

1、引入maven依耐 javax.mail

		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>

2、邮件配置 , src/main/resources 下面创建配置文件,config/email.properties

#smtp.office365.com   端口587
mail.host=smtp.office365.com
mail.port=587
mail.nick=系统管理员
mail.username=test1@outlook.com
mail.password=password
mail.from=test1@outlook.com
mail.toList=test2@outlook.com;test3@outlook.com

3、核心代码

package com.demo.email.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

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

public class SendEmail {

	private static String host;
	private static String port;
	private static String nick;
	private static String username;
	private static String password;
	private static String from;
	private static List<String> toList;

	static {
		init();
	}

	private static boolean isEmpty() {
		if (nick != null && host != null && port != null && username != null && password != null && from != null && toList != null) {
			return false;
		}
		return true;
	}

	private static void init() {
		toList = new ArrayList<String>();
		InputStream inStream = SendEmail.class.getResourceAsStream("/config/email.properties");
		try {
			Properties prop = new Properties();
			prop.load(inStream);
			host = prop.getProperty("mail.host");
			port = prop.getProperty("mail.port");
			nick = prop.getProperty("mail.nick");
			username = prop.getProperty("mail.username");
			password = prop.getProperty("mail.password");
			from = prop.getProperty("mail.from");
			String toListStr = prop.getProperty("mail.toList");
			String[] split = toListStr.split(";");
			for (String string : split) {
				toList.add(string);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inStream != null) {
				try {
					inStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static boolean sendEmail(String title,String content) {

		if(isEmpty()) {
			init() ;
		}
		
		Properties properties = System.getProperties();

		properties.setProperty("mail.transport.protocol", "smtp");
		properties.setProperty("mail.smtp.host", host);
		properties.setProperty("mail.smtp.port", port);
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.starttls.enable", "true");// outlook邮箱需要加上

		Authenticator authenticator = new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		};

		Session session = Session.getDefaultInstance(properties, authenticator);

		try {
			MimeMessage message = new MimeMessage(session);

			message.setFrom(new InternetAddress(nick + " <"+from +">"));

			/**
			 *  设置收件人地址(可以增加多个收件人、抄送、密送),即下面这一行代码书写多行
	         * MimeMessage.RecipientType.TO:发送
	         * MimeMessage.RecipientType.CC:抄送
	         * MimeMessage.RecipientType.BCC:密送
	         */
			for (String to : toList) {
				message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			}

			message.setSubject(title, "UTF-8");
			message.setContent(content, "text/html;charset=UTF-8");

			Transport.send(message);
			
			System.out.println("Sent message successfully.... title:"+title);
			return true ;
			
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}

		return false;
	}

	public static void main(String[] args) {
		String content = "<a href='http://www.baidu.com'>测试地址</a>" ;
		SendEmail.sendEmail("测试内容", content) ;
		
	}
	
	
	
	
	
}

 

仅供参考。。。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值