分两步:
1.打开开关:允许不够安全的应用
链接:https://myaccount.google.com/lesssecureapps
2.访问这个链接:https://accounts.google.com/DisplayUnlockCaptcha允许任何设备登陆
最后,请尝试在您的新设备上或在新应用中重新登录您的 Google 帐号。
/*** 邮件发送* @param mailHost 邮件服务地址* @param fromMail 发件人* @param fromName 发件人名* @param fromMailPwd 发件人密码* @param toMails 收件人,多个用英文逗号分隔* @param mailTitle 邮件标题* @param mailContent 邮件内容* @throws Exception*/public static void sendMail(String mailHost, String fromMail, String fromName, String fromMailPwd,String toMails, String mailTitle, String mailContent) throws Exception {String[] toMailArr;if (toMails != null && !toMails.equals("")) {toMailArr = toMails.split(",");} else {throw new Exception("邮件发送人不能为空");}// 邮件属性信息Properties props = new Properties();props.put("mail.host", mailHost);props.put("mail.transport.protocol", "smtp");props.put("mail.smtp.auth", "true");// props.put("mail.smtp.ssl.enable", "true");
// props.put("mail.smtp.port", 465);props.setProperty("mail.smtp.starttls.enable", "true");props.put("mail.smtp.port", 587);Session session = Session.getInstance(props); // 根据属性新建一个邮件会话session.setDebug(true); // 是否打印调试信息toMailArr = toMails.split(",");for (String to : toMailArr) {MimeMessage message = new MimeMessage(session); // 由邮件会话新建一个消息对象message.setFrom(new InternetAddress(fromMail));// 设置发件人的地址message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, fromName));// 设置收件人,并设置其接收类型为TOmessage.setSubject(mailTitle);// 设置标题message.setContent(mailContent, "text/html;charset=UTF-8"); // 设置邮件内容类型为htmlmessage.setSentDate(new Date());// 设置发信时间message.saveChanges();// 存储邮件信息// 发送邮件Transport transport = session.getTransport();transport.connect(fromMail, fromMailPwd);transport.sendMessage(message, message.getAllRecipients());transport.close();}}
转载自:https://blog.csdn.net/zs345048102/article/details/81070399