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

java调用邮箱_Java调用api使用企业邮箱账户发送邮件(示例代码)
<>

package cn.ucmed.otaka.healthcare.rubik.common;

import lombok.extern.slf4j.Slf4j;

import javax.mail.Message;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.util.Date;

import java.util.Properties;

import java.util.regex.Pattern;

@Slf4jpublic classEmailUtil {private static String emailRegular = "^s*w+(?:.{0,1}[w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*.[a-zA-Z]+s*$";public staticboolean validEmail(String emailAddress) {returnPattern.matches(emailRegular, emailAddress);

}private static String fromMailAddress = "邮箱地址";private static String fromMailPwd = "邮箱密码";private static String fromMailSMTPHost = "smtp.exmail.qq.com";private static String smtpPort = "465";private staticProperties setProperties() {

Properties properties= newProperties();

properties.setProperty("mail.transport.protocol", "smtp");

properties.setProperty("mail.smtp.host", fromMailSMTPHost);

properties.setProperty("mail.smtp.auth", "true");

properties.setProperty("mail.smtp.port", smtpPort);

properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

properties.setProperty("mail.smtp.socketFactory.fallback", "false");

properties.setProperty("mail.smtp.socketFactory.port", smtpPort);

properties.setProperty("mail.smtp.ssl.enable", "true");

properties.setProperty("mail.debug", "true");returnproperties;

}private staticMessage setMail(Session session, String toEmailAdress, String emailTitle, String emailContent) throws Exception {

MimeMessage message= newMimeMessage(session);

message.setFrom(new InternetAddress(fromMailAddress, fromMailAddress, "UTF-8"));

message.setRecipient(Message.RecipientType.TO,new InternetAddress(toEmailAdress, toEmailAdress, "UTF-8"));

message.setSubject(emailTitle);

message.setContent(emailContent,"text/html;charset=utf-8");

message.setSentDate(newDate());

message.saveChanges();returnmessage;

}private staticSession setSession() {

Properties properties=setProperties();

Session session=Session.getInstance(properties);

session.setDebug(true);returnsession;

}public staticboolean sendEmail(String toEmailAdress, String emailContent, String emailTitle) {

Session session=setSession();try{

Message message=setMail(session, toEmailAdress, emailTitle, emailContent);

Transport transport=session.getTransport();

transport.connect(fromMailAddress, fromMailPwd);

transport.sendMessage(message, message.getAllRecipients());

transport.close();

}catch(Exception e) {

log.error("toEmailAdress =" + toEmailAdress + ", emailContent =" + emailContent + ", emailTitle =" + emailTitle + ", error =" +e.getMessage());return false;

}return true;

}public static voidmain(String[] args) {

EmailUtil.sendEmail("miaoying.new@qq.com", "testContent", "testTitle");

}

}



阅读:0
日期:2021-03-18

打印 】 【 关闭 】  【 字体: 】 
上一篇: java调用腾讯企业邮箱给谷歌(gmail)邮箱发送邮件丢失或进入垃圾邮箱
下一篇: 2021-01-3 VBA利用企业邮箱自动发送邮件
  >> 相关文章
 

服务热线

0574-55011290

微信二维码