java:email

发邮件分为几种协议:

  • IMAP:收imap.example.com , 发stmp.example.com,端口,加密(SSL/TLS、 STARTTLS)。
  • exchange:服务器、域、SSL
  • POP3: 收pop.example.com , 发stmp.example.com,端口,加密。

stmp协议代码:

注意:

  1. 端口
  2. 加密
  3. 如果是ssl连接,需要在session的properties中增加设置:“mail.smtp.ssl.enable”, “true”。
  4. //设置超时时间为20秒 prop.setProperty(“mail.smtp.timeout”, “20000”);
	public static void sendMail1(String sendFromDBAccount, String sendFromDBpassword, String sendTo, String sendCc, String mailTitle,
			String mailContent) throws Exception {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		Properties prop = new Properties();
		prop.setProperty("mail.smtp.host", Mail_host);
		prop.setProperty("mail.smtp.port", Mail_port);
		//设置用户认证方式
		prop.setProperty("mail.smtp.auth", "true"); 
		Session session = Session.getDefaultInstance(prop, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(Mail_from_account, Mail_from_password);
			}
		});
		session.setDebug(true);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(Mail_from_account));
		msg.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress().parse(sendTo));
		msg.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress().parse(sendCc));
		msg.setSubject(mailTitle);
		msg.setText(mailContent);

		Transport.send(msg);
	}

exchange代码:

注意:

  1. host很特殊,样例为 https://*****/ews/exchange.asmx
  2. BodyType.HTML
  3. 多收件人,逗号分割
  4. 抄送人
  5. 附件

	public static boolean sendMail2(String to, String subject, String content, String filePath) {
		mailLog.info(String.format("exchange邮件发送 to:{%s}, subject:{%s}, content:{%s},filePath:{%s}", to, subject, content, filePath));
		boolean isOK = false;
		ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
		ExchangeCredentials credentials = new WebCredentials(Mail_from_account2, Mail_from_password2);
		service.setCredentials(credentials);
		try {
			service.setUrl(new URI(Mail_host2));
			EmailMessage msg = new EmailMessage(service);
			msg.setSubject(subject);
			MessageBody body = MessageBody.getMessageBodyFromText(content);
			// body.setBodyType(BodyType.HTML);
			body.setBodyType(BodyType.Text);
			msg.setBody(body);
			// 支持多个收件人
			InternetAddress[] addresses = InternetAddress.parse(to);
			for (InternetAddress address : addresses) {
				msg.getToRecipients().add(address.getAddress());
			}
			if (!CommonUtils.isNullString(filePath)) {
				msg.getAttachments().addFileAttachment(filePath);
			}
			msg.send();
			isOK = true;
		} catch (Exception e) {
			e.printStackTrace();
			mailLog.error("=>sendMail5-exchage-fail", e);
			isOK = false;
		}
		// System.out.println(isOK);
		return isOK;
	}

引用包:

        // e-mail
        compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.0'
		// e-mail-exchange
		compile group: 'com.microsoft.ews-java-api', name: 'ews-java-api', version: '2.0'
		compile group: 'joda-time', name: 'joda-time', version: '2.10.6'

参考资料:

  1. https://www.jb51.net/article/180095.htm
  2. https://segmentfault.com/q/1010000013832196
  3. https://mvnrepository.com/artifact/com.microsoft.ews-java-api/ews-java-api/2.0
  4. https://segmentfault.com/q/1010000004497746
  5. https://blog.csdn.net/zyhlwzy/article/details/79989577
  6. https://www.cnblogs.com/JackFe/p/6557365.html
  7. https://www.bbsmax.com/A/qVdeBAKrzP/
  8. https://www.cnblogs.com/qq642193463/p/10691516.html
  9. http://www.manongjc.com/article/29461.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值