Java实现发送邮件功能

代码:

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
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.MimeUtility;

import org.apache.commons.lang3.StringUtils;


public class EmailUtil {

	/**
	 * 发送邮件
	 * @param to 接收邮件邮箱号
	 * @param cc 抄送邮件邮箱号
	 * @param subject 邮件标题
	 * @param content 邮件内容
	 * @return
	 */
	public boolean sendEmail(String to, String cc, String subject, String content) {
		if (StringUtils.isEmpty(to)) {
			return false;
		}
		EmailConfig config = createEmailConfig(to, cc);
		try {
			return send(config, subject, content);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	private static EmailConfig createEmailConfig(String to, String cc) {
		EmailConfig config = new EmailConfig();
		config.setHost("发送邮件服务器");
		config.setPort("服务器发送端口");
		config.setEmail("系统使用邮箱号");
		config.setUserName("系统发送邮件用户名");
		config.setPassword("系统发送邮件所用邮箱密码");
		config.setToEmails("接收邮件邮箱号");
		config.setCcEmails("抄送邮件邮箱号");
		return config;
	}
	
	private static boolean send(EmailConfig config, String subject, String content) {
		Session session = EmailUtil.creatSession(config);
		try {
			MimeMessage message = new MimeMessage(session);
			// 设置邮件头及编码格式
			subject = MimeUtility.encodeWord(subject, "UTF-8", "Q");
			message.setSubject(subject);
			// 邮件署名
			message.setFrom(new InternetAddress(config.getEmail(), config.getUserName()));
			// 消息接收者(收件地址)
			if (StringUtils.isNotBlank(config.getToEmails())) {
				String[] emails = config.getToEmails().split(",");
				InternetAddress[] addresses = new InternetAddress[emails.length];
				for (int i = 0; i < emails.length; i++) {
					addresses[i] = new InternetAddress(emails[i]);
				}
				message.addRecipients(Message.RecipientType.TO, addresses);
			}
			// 消息抄送者(收件地址)
			if (StringUtils.isNotBlank(config.getCcEmails())) {
				String[] emails = config.getCcEmails().split(",");
				InternetAddress[] addresses = new InternetAddress[emails.length];
				for (int i = 0; i < emails.length; i++) {
					addresses[i] = new InternetAddress(emails[i]);
				}
				message.addRecipients(Message.RecipientType.CC, addresses);
			}
			message.saveChanges();
			// 设置邮件内容及编码格式
			// 后一个参数可以不指定编码,如"text/plain",但是将不能显示中文字符
			message.setContent(content, "text/plain;charset=UTF-8");
			// 发送
			Transport.send(message);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private static Session creatSession(EmailConfig config) {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");
		props.setProperty("mail.smtp.host", config.getHost());
		props.setProperty("mail.smtp.port", config.getPort());
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.ssl.enable", "true");
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.socketFactory.port", config.getPort());
		props.setProperty("mail.smtp.connectiontimeout", "5000");
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				// 登录用户名密码
				return new PasswordAuthentication(config.getEmail(), config.getPassword());
			}
		});
		// session.setDebug(true);
		return session;
	}
}

 

转载于:https://www.cnblogs.com/Big-Boss/p/9964312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值