当前位置:宁波企业邮 » 企业目录 » 青岛 » 文章详细

Java实现腾讯企业邮箱收发邮件

Java实现腾讯企业邮箱收发邮件

个人邮箱发邮件很容易,网上很多工具类,使用企业邮箱发邮件其实原理是一样的,主要是加上一个企业邮箱必备的SSL,开启安全协议。
直接上代码,如下

package com.xxxx.util.sendemail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

/**
 * 发邮件工具类
 */
public class MailUtils {

	private static String account = "sendmail@xxxx.cn";// 登录账户
	private static String password = "xxxxxx";// 登录密码
	private static String host = "smtp.exmail.qq.com";// 服务器地址
	private static String port = "465";// 端口
	private static String protocol = "smtp";// 协议

//初始化参数
	public static Session initProperties() {
		Properties properties = new Properties();
		properties.setProperty("mail.transport.protocol", protocol);
		properties.setProperty("mail.smtp.host", host);
		properties.setProperty("mail.smtp.port", port);
		// 使用smtp身份验证
		properties.put("mail.smtp.auth", "true");
		// 使用SSL,企业邮箱必需 start
		// 开启安全协议
		MailSSLSocketFactory mailSSLSocketFactory = null;
		try {
			mailSSLSocketFactory = new MailSSLSocketFactory();
			mailSSLSocketFactory.setTrustAllHosts(true);
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
		properties.put("mail.smtp.enable", "true");
		properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
		properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.put("mail.smtp.socketFactory.fallback", "false");
		properties.put("mail.smtp.socketFactory.port", port);
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(account, password);
			}
		});
		// 使用SSL,企业邮箱必需 end
		// TODO 显示debug信息 正式环境注释掉
		session.setDebug(true);
		return session;
	}

//@param sender 发件人的姓名
//@param subject 邮件的主题
//@param content 邮件的内容
//@param receiverList 接收者的列表,多个接收者之间用","隔开
//@param fileSrc 附件地址
	public static int send(String sender, String subject, String content, String receiverList, String fileSrc) {
		try {
			Session session = initProperties();
			MimeMessage mimeMessage = new MimeMessage(session);
			mimeMessage.setFrom(new InternetAddress(account, sender));// 发件人,可以设置发件人的别名
			// 收件人,多人接收
			InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);
			mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);
			// 主题
			mimeMessage.setSubject(subject);
			// 时间
			mimeMessage.setSentDate(new Date());
			// 容器类 附件
			MimeMultipart mimeMultipart = new MimeMultipart();
			// 可以包装文本,图片,附件
			MimeBodyPart bodyPart = new MimeBodyPart();
			// 设置内容
			bodyPart.setContent(content, "text/html; charset=UTF-8");
			mimeMultipart.addBodyPart(bodyPart);
			// 添加图片&附件
			bodyPart = new MimeBodyPart();
			bodyPart.attachFile(fileSrc);
			mimeMultipart.addBodyPart(bodyPart);
			mimeMessage.setContent(mimeMultipart);
			mimeMessage.saveChanges();
			Transport.send(mimeMessage);
			return 1;
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return  -1;
	}

	public static void main(String[] args) {
		send("乱码行空","主题聚会","内容周五晚上xxx餐厅不见不散","1111111@qq.com","D:\Desktop\feifei.jpg");
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
<>


阅读:1945
日期:2021-03-26

打印 】 【 关闭 】  【 字体: 】 
上一篇: 腾讯企业邮箱api java_腾讯企业邮箱API实现单点登录和获取企业未读邮件
下一篇: 每天一个小技巧——网易邮箱配置阿里云企业邮箱配置信息设置
  >> 相关文章
 

服务热线

0574-55011290

微信二维码