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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章