java email
import com.qm.be.model.EmailMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.File; import java.util.Date; import java.util.Properties; public class MailOperation { private static final Logger logger = LoggerFactory.getLogger(MailOperation.class); /** * 发送邮件 * @param emailMessage * @return * @throws Exception */ public static void sendMail(EmailMessage emailMessage) { logger.info(emailMessage.toString()); if (emailMessage.to != null){ Properties props = System.getProperties(); // props.put("mail.smtp.port", "25"); props.put("mail.smtp.port", emailMessage.getPort()); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", emailMessage.getHost()); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.ssl.trust",emailMessage.getHost()); Session session; if (emailMessage.getUser() != null && emailMessage.getPassword() != null){ Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emailMessage.getUser(),emailMessage.getPassword()); } }; session = Session.getInstance(props,auth); }else { session = Session.getInstance(props); } session.setDebug(true); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(emailMessage.from)); if (!"".equals(emailMessage.to.trim())) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailMessage.to.trim())); } if (emailMessage.getCc() != null && emailMessage.getCc().size() > 0) { for (String address: emailMessage.cc) { if(!"".equals(address.trim())) { message.addRecipient(Message.RecipientType.CC, new InternetAddress(address.trim())); } } } //主题 message.setSubject(emailMessage.subject); //内容 BodyPart contentBodyPart = new MimeBodyPart(); //add email content part. // 设置邮件的文本内容 contentBodyPart.setContent(emailMessage.getContent(), "text/html;charset=utf-8"); // 整个邮件:正文+附件 Multipart multipart = new MimeMultipart(); multipart.addBodyPart(contentBodyPart); if(emailMessage.getFiles()!=null && emailMessage.getFiles().size()>0){ for(File file:emailMessage.getFiles()){ MimeBodyPart mimeBodyPart = new MimeBodyPart(); // 设置要发送附件的文件路径 DataSource source = new FileDataSource(file.getPath()); mimeBodyPart.setDataHandler(new DataHandler(source)); // 处理附件名称中文(附带文件路径)乱码问题 mimeBodyPart.setFileName(MimeUtility.encodeText(file.getName())); multipart.addBodyPart(mimeBodyPart); } } message.setContent(multipart); message.addHeader("X-Mailer", "JavaMail"); message.setSentDate(new Date()); message.saveChanges(); Transport transport=session.getTransport("smtp"); transport.connect(); Transport.send(message, message.getAllRecipients()); transport.close(); } catch (Exception e){ logger.error("发送失败", e); e.printStackTrace(); } } } }