javamail 发送邮件

xiaoxiao2021-02-28  105

package com.jointsky.automonitor.alarmnotice.noticemanager.utils; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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 javax.mail.internet.MimeUtility; public class EmailSendTool { // 邮箱服务器 private String host; // 这个是你的邮箱用户名 private String username; // 你的邮箱密码 private String password; private String mail_head_name = "this is head of this mail"; private String mail_head_value = "this is head of this mail"; private String mail_to; private String mail_from; private String mail_subject = "this is the subject of this test mail"; private String mail_body = "this is the mail_body of this test mail"; private String personalName = ""; /** * 附件 */ private String[] attachmentPaths; public EmailSendTool() { } /** * <p> * Title: * </p> * <p> * Description: * </p> * * @param host * 邮箱服务器 * @param username * 邮箱服务器登陆用户名 * @param password * 密码 * @param mailto * 指定发送得邮箱(多个以逗号分隔) * @param subject * 邮件标题 * @param text * 邮件文本 * @param name * 指定邮箱名称 * @param head_name * @param head_value * @param attachmentPath * 附件路径 */ public EmailSendTool(String host, String username, String password, String mailto, String subject, String text, String name, String head_name, String head_value, String[] attachmentPaths) { this.host = host; this.username = username; this.mail_from = username; this.password = password; this.mail_to = mailto; this.mail_subject = subject; this.mail_body = text; this.personalName = name; this.mail_head_name = head_name; this.mail_head_value = head_value; this.attachmentPaths = attachmentPaths; } /** * 此段代码用来发送普通电子邮件 * * @throws MessagingException * @throws UnsupportedEncodingException * @throws UnsupportedEncodingException */ public void send() throws MessagingException, UnsupportedEncodingException { Properties props = new Properties(); Authenticator auth = new Email_Autherticator(); // 进行邮件服务器用户认证 props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, auth); // 设置session,和邮件服务器进行通讯。 MimeMessage message = new MimeMessage(session); message.setSubject(mail_subject); // 设置邮件主题 message.setText(mail_body); // 设置邮件正文 message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题 message.setSentDate(new Date()); // 设置邮件发送日期 Address address = new InternetAddress(mail_from, personalName); message.setFrom(address); // 设置邮件发送者的地址 // 设置邮件接收方的地址(单个) /*Address toAddress = new InternetAddress(mail_to); message.addRecipient(Message.RecipientType.TO, toAddress);*/ //设置邮件接收方的地址(多个,用逗号分隔) @SuppressWarnings("static-access") InternetAddress[] iaToList = new InternetAddress().parse(mail_to); message.setRecipients(Message.RecipientType.TO,iaToList); //MimeMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mainPart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart();//创建一个包含HTML内容的MimeBodyPart //设置HTML内容 messageBodyPart.setContent(mail_body,"text/html; charset=utf-8"); mainPart.addBodyPart(messageBodyPart); //存在附件 if (attachmentPaths != null && attachmentPaths.length > 0) { for(String filePath:attachmentPaths){ messageBodyPart = new MimeBodyPart(); File file = new File(filePath); if(file.exists()){//附件存在磁盘中 FileDataSource fds = new FileDataSource(file);//得到数据源 messageBodyPart.setDataHandler(new DataHandler(fds));//得到附件本身并至入BodyPart messageBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));//得到文件名同样至入BodyPart,文件名称进行编码处理 mainPart.addBodyPart(messageBodyPart); } } } //将MimeMultipart对象设置为邮件内容 message.setContent(mainPart); Transport.send(message); // 发送邮件 } /** * 用来进行服务器对用户的认证 */ public class Email_Autherticator extends Authenticator { public Email_Autherticator() { super(); } public Email_Autherticator(String user, String pwd) { super(); username = user; password = pwd; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } public static void main(String[] args) { String[] paths = new String[1]; paths[0] = "f:/101.jpg"; EmailSendTool sendEmail = new EmailSendTool("smtp.163.com", "163邮箱登陆用户名", "登录密码", "目标邮箱1,目标邮箱2", "邮件标题", "邮件内容", "邮件标题", "", "", 附件路径); try { sendEmail.send(); } catch (Exception ex) { ex.printStackTrace(); } } }
转载请注明原文地址: https://www.6miu.com/read-19695.html

最新回复(0)