java 如何发送电子邮件
第一次写博客:
所有不知道怎么添加jar包;
activation-1.1.jar
javax.mail-1.6.0.jar
junrar-0.7.jar也可能是这个junrar-3.0.0.jar
1 package com.wj.zxc.mail; 2 3 4 import java.io.Serializable; 5 6 /** 7 * wj 8 */ 9 public class EmailEntity implements Serializable { 10 private static final long serialVersionUID = 1L; 11 //邮箱服务器地址 12 private String host; 13 //主机端口 14 private Integer port; 15 //发送者的邮箱账号 16 private String userName; 17 //发送者的密码 18 private String password; 19 //发送者的邮箱地址 20 private String fromAddress; 21 //接收者的邮箱地址 22 private String toAddress; 23 //设置邮件主题 24 private String subject; 25 //设置邮件内容 26 private String context; 27 //设置邮件类型 28 private String contextType; 29 30 //抄送的邮箱地址 31 private String ccAddress; 32 public String getHost() { 33 return host; 34 } 35 36 public void setHost(String host) { 37 this.host = host; 38 } 39 40 public Integer getPort() { 41 return port; 42 } 43 44 public void setPort(Integer port) { 45 this.port = port; 46 } 47 48 public String getUserName() { 49 return userName; 50 } 51 52 public void setUserName(String userName) { 53 this.userName = userName; 54 } 55 56 public String getPassword() { 57 return password; 58 } 59 60 public void setPassword(String password) { 61 this.password = password; 62 } 63 64 public String getFromAddress() { 65 return fromAddress; 66 } 67 68 public void setFromAddress(String fromAddress) { 69 this.fromAddress = fromAddress; 70 } 71 72 public String getToAddress() { 73 return toAddress; 74 } 75 76 public void setToAddress(String toAddress) { 77 this.toAddress = toAddress; 78 } 79 80 public String getSubject() { 81 return subject; 82 } 83 84 public void setSubject(String subject) { 85 this.subject = subject; 86 } 87 88 public String getContext() { 89 return context; 90 } 91 92 public void setContext(String context) { 93 this.context = context; 94 } 95 96 public String getContextType() { 97 return contextType; 98 } 99 100 public void setContextType(String contextType) { 101 this.contextType = contextType; 102 } 103 public String getCcAddress() { 104 return ccAddress; 105 } 106 107 public void setCcAddress(String ccAddress) { 108 this.ccAddress = ccAddress; 109 } 110 }
1 package com.wj.zxc.mail; 2 import javax.mail.Authenticator; 3 import javax.mail.PasswordAuthentication; 4 5 /** 6 * 验证邮箱 7 */ 8 public class VerifyEmail extends Authenticator { 9 //账号 10 private String userName; 11 //密码 12 private String password; 13 14 public String getUserName() { 15 return userName; 16 } 17 18 public void setUserName(String userName) { 19 this.userName = userName; 20 } 21 22 public String getPassword() { 23 return password; 24 } 25 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 30 //构造方法 31 public VerifyEmail() { 32 super(); 33 } 34 35 public VerifyEmail(String userName, String password) { 36 super(); 37 this.userName = userName; 38 this.password = password; 39 } 40 41 protected PasswordAuthentication getPasswordAuthentication() { 42 43 return new PasswordAuthentication(userName, password); 44 45 } 46 }
1 package com.wj.zxc.mail; 2 3 import com.sun.mail.util.MailSSLSocketFactory; 4 import javax.mail.*; 5 import javax.mail.internet.*; 6 import java.security.GeneralSecurityException; 7 import java.util.Date; 8 import java.util.Properties; 9 10 /** 11 * @Author 12 */ 13 14 public class EmailSend { 15 16 public static boolean EmailSendTest(EmailEntity emailEntity) { 17 try { 18 //配置文件 19 MailSSLSocketFactory sf = null; 20 try { 21 sf = new MailSSLSocketFactory(); 22 sf.setTrustAllHosts(true); 23 } catch (GeneralSecurityException e1) { 24 e1.printStackTrace(); 25 } 26 Properties properties = new Properties(); 27 properties.put("mail.smtp.auth", "true"); 28 properties.put("mail.smtp.host", emailEntity.getHost()); 29 properties.put("mail.smtp.port", 994);//QQ邮箱 30 //properties.put("mail.smtp.port", 465);网易邮箱 31 properties.put("mail.smtp.starrttls.enable", "true"); 32 properties.put("mail.smtp.ssl.enable", "true"); 33 properties.put("mail.smtp.ssl.socketFactory", sf); 34 //创建会话 35 VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword()); 36 Session mailSession = Session.getInstance(properties, verifyEmail); 37 mailSession.setDebug(true); 38 //创建信息对象 39 Message message = new MimeMessage(mailSession); 40 InternetAddress from = new InternetAddress(emailEntity.getFromAddress()); 41 InternetAddress to = new InternetAddress(emailEntity.getToAddress()); 42 //设置邮件信息的来源 43 message.setFrom(from); 44 //设置邮件的接收者 45 InternetAddress[] iaToListcs = new InternetAddress() 46 .parse(emailEntity.getCcAddress()); 47 message.setRecipients(Message.RecipientType.CC, iaToListcs); // 抄送人 48 49 50 message.setRecipient(MimeMessage.RecipientType.TO, to); 51 message.setSubject(emailEntity.getSubject()); 52 //设置邮件发送日期 53 message.setSentDate(new Date()); 54 //设置邮件内容 55 // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 56 Multipart multipart = new MimeMultipart(); 57 BodyPart messageBodyPart = new MimeBodyPart(); 58 message.setContent(emailEntity.getContext() , emailEntity.getContextType()); 59 message.saveChanges(); 60 sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); 61 multipart.addBodyPart(messageBodyPart); 62 63 // 将multipart对象放到message中 64 /* List<File> attachments = new ArrayList<>(); /////////////////////////多个附件 65 Multipart multipart = new MimeMultipart(); 66 File a = new File("C:/Users/CYKJ/Desktop/外部次流模板.xlsx"); 67 File b = new File("C:/Users/CYKJ/Desktop/外部次流模板.xlsx"); 68 attachments.add(a); 69 attachments.add(b); 70 message.setContent(emailEntity.getContext() , emailEntity.getContextType()); 71 if (attachments.size()>0) { 72 for (File attachment : attachments) { 73 BodyPart attachmentPart = new MimeBodyPart(); 74 DataSource source = new FileDataSource(attachment); 75 attachmentPart.setDataHandler(new DataHandler(source)); 76 attachmentPart.setFileName(MimeUtility.encodeWord(attachment.getName())); 77 multipart.addBodyPart(attachmentPart); 78 } 79 } 80 message.setContent(multipart);*/ 81 // 保存邮件 82 message.saveChanges(); 83 // 添加附件的内容 84 //发送邮件 85 86 Transport transport = mailSession.getTransport("smtp"); 87 transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword()); 88 System.out.println("发送:" + transport); 89 transport.sendMessage(message, message.getAllRecipients()); 90 System.out.println("success"); 91 return true; 92 } catch (MessagingException e) { 93 e.printStackTrace(); 94 //System.out.println("失败"); 95 return false; 96 97 } 98 } 99 100 }
package com.wj.zxc.mail; /** 测试 */ public class TestEmail { public static void main(String[] arg) { EmailEntity email = new EmailEntity(); email.setUserName("发送人"); email.setPassword("密码(QQ邮箱→设置→账号→POP3/SMTP服务 开启(获取字符串))"); email.setHost("smtp.qiye.163.com");//email.setHost("smtp.163.com");网易 email.setFromAddress("发送人"); email.setToAddress("接收人"); email.setCcAddress("抄送人"); email.setSubject("最后·的测试"); email.setContext("没有东西"); email.setContextType("text/html;charset=utf-8"); boolean flag = EmailSend.EmailSendTest(email); System.err.println("邮件发送结果==" + flag); } }

浙公网安备 33010602011771号