Java发送邮件,抄送,附加(file文件,url中获取),html

maven

    
      org.apache.commons
      commons-email
      1.4
    
    
      cn.hutool
      hutool-all
      5.7.16
    

代码


import cn.hutool.core.collection.CollectionUtil;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.lang3.StringUtils;


public class EmailSendUtil {

	private static final String host = ""; //host
	private static final String port = ""; //port
	private static final String username = "";//邮箱账号
	private static final String password = "";//邮箱密码
	private static final String protocol = "";//协议

	/**
	 * @param email    收件人
	 * @param copys    抄送人
	 * @param subject  主题
	 * @param content  内容
	 * @param files    附加(文件类型)
	 * @param fileUrls 附加(url类型)
	 */

	public void send(String email, List copys, String subject, String content,
		List files, List fileUrls) {
		Transport transport = null;
		try {
			Properties props = System.getProperties();
			//设置用户的认证方式
			props.setProperty("mail.smtp.auth", "true");
			//设置传输协议
			props.setProperty("mail.transport.protocol", protocol);
			//设置发件人的SMTP服务器地址
			props.setProperty("mail.smtp.host", host);
			//端口
			props.setProperty("mail.smtp.port", port);
			//使用ssl
			props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			Session session = Session.getInstance(props, null);
			MimeMessage mimeMessage = new MimeMessage(session);
			InternetAddress mailFrom = new InternetAddress(username);
			InternetAddress mailTo = new InternetAddress(email);
			//发件箱
			mimeMessage.setFrom(mailFrom);
			//收件箱
			mimeMessage.setRecipient(RecipientType.TO, mailTo);
			//回复地址
			mimeMessage.setReplyTo(new Address[]{mailFrom});
			//邮件标题
			mimeMessage.setSubject(subject);
			//发件人
			mimeMessage.setSender(mailFrom);
			//抄送
			if (CollectionUtil.isNotEmpty(copys)) {
				Address[] addresses = new Address[copys.size()];
				for (int i = 0; i < copys.size(); i++) {
					String copy = copys.get(i);
					addresses[i] = new InternetAddress(copy);
				}
				mimeMessage.addRecipients(RecipientType.CC, addresses);
			}
			//附加
			MimeMultipart mimeMultipart = new MimeMultipart();
			//内容
			if (StringUtils.isNotBlank(content)) {
				BodyPart textPart = new MimeBodyPart();
				textPart.setContent(content, "text/html;charset=UTF-8");
				mimeMultipart.addBodyPart(textPart);
			}
			//文件附加
			if (CollectionUtil.isNotEmpty(files)) {
				files.forEach(file -> {
					BodyPart fileBodyPart = new MimeBodyPart();
					try {
						//构造附件一的数据源
						FileDataSource fileDataSource = new FileDataSource(file);
						//数据处理
						DataHandler dataHandler = new DataHandler(fileDataSource);
						fileBodyPart.setDataHandler(dataHandler);
						fileBodyPart.setFileName(file.getName());
						mimeMultipart.addBodyPart(fileBodyPart);
					} catch (MessagingException e) {
						throw new RuntimeException(e);
					}

				});
			}
			//图片附加
			if (CollectionUtil.isNotEmpty(fileUrls)) {
				for (int i = 0; i < fileUrls.size(); i++) {
					String urlPath = fileUrls.get(i);
					//邮件中包含网络附件
					URL url = null;
					try {
						url = new URL(urlPath);
						DataHandler dataHandler = new DataHandler(url);
						BodyPart urlBodyPart = new MimeBodyPart();
						urlBodyPart.setDataHandler(dataHandler);
						urlBodyPart.setFileName(urlPath);
						mimeMultipart.addBodyPart(urlBodyPart);
					} catch (MalformedURLException e) {
						throw new RuntimeException(e);
					}
				}
			}
			//邮件内容
			mimeMessage.setContent(mimeMultipart);
			transport = session.getTransport(protocol);
			transport.connect(host, username, password);
			//发送邮件
			transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
		} catch (MessagingException e) {
			e.printStackTrace();
		} finally {
			try {
				if (Objects.nonNull(transport)) {
					transport.close();
				}
			} catch (MessagingException e) {
				e.printStackTrace();
			}
		}
	}



}

你可能感兴趣的:(Java,java代码,瞎写,java)