Java - 谷歌邮箱发送邮件详解

匿名 (未验证) 提交于 2019-12-02 21:53:52


错误如下:

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)         at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)         at javax.mail.Service.connect(Service.java:317)         at javax.mail.Service.connect(Service.java:176)         at javax.mail.Service.connect(Service.java:125)         at javax.mail.Transport.send0(Transport.java:194)         at javax.mail.Transport.send(Transport.java:124)         at com.ecnu.mail.JavaMailDemo.gmailSender(JavaMailDemo.java:60)         at com.ecnu.mail.JavaMailDemo.main(JavaMailDemo.java:24)

通过查看gmail邮箱帮助:https://support.google.com/accounts/answer/6010255?authuser=1,需要开启“允许不够安全的应用”

开启后,可以通过gmail邮箱发送邮件到其他邮箱。


附上工具类供大家使用:

public class SendGmail {     /*      * gmail邮箱SSL方式      */     private static void gmailssl(Properties props) {         final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";         props.put("mail.debug", "true");         props.put("mail.smtp.host", "smtp.gmail.com");         props.put("mail.smtp.ssl.enable", "true");         props.put("mail.smtp.socketFactory.class", SSL_FACTORY);         props.put("mail.smtp.port", "465");         props.put("mail.smtp.socketFactory.port", "465");         props.put("mail.smtp.auth", "true");     }       //gmail邮箱的TLS方式     private static void gmailtls(Properties props) {         props.put("mail.smtp.auth", "true");         props.put("mail.smtp.starttls.enable", "true");         props.put("mail.smtp.host", "smtp.gmail.com");         props.put("mail.smtp.port", "587");     }      /*      * 通过gmail邮箱发送邮件      */     public static void gmailSender(String email) {          // Get a Properties object         Properties props = new Properties();         //选择ssl方式         gmailssl(props);          final String username = "";//gmail邮箱         final String password = "";//密码         Session session = Session.getDefaultInstance(props,                 new Authenticator() {                     protected PasswordAuthentication getPasswordAuthentication() {                         return new PasswordAuthentication(username, password);                     }                 });         // -- Create a new message --         Message msg = new MimeMessage(session);           // -- Set the FROM and TO fields --         try {             msg.setFrom(new InternetAddress(username));             msg.setRecipients(Message.RecipientType.TO,                     InternetAddress.parse(email));             msg.setSubject("");             msg.setText("");             msg.setSentDate(new Date());             Transport.send(msg);         } catch (AddressException e) {             e.printStackTrace();         } catch (MessagingException e) {             e.printStackTrace();         }           System.out.println("Message sent.");     } }




标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!