首先登录Gmail帐户
找到这个地方
然后,检查所有配置
然后粘贴代码:
privatestaticstringfrom=' you account @ Gmail.com '; //发件人的谷歌电子邮件地址私有身份验证密码=' password '; //Google邮箱密码publicstaticbooleansendmailgmail (stringto,String content ) returngmailsender (从、密码、to,到) } privatestaticvoidgmailssl (properties props ) finalstringssl _ factory=' javax.net.SSL.SSL套接字工厂'; props.put('mail.debug ',' true ' ); props.put(Mail.SMTP.host )、) smtp.gmail.com); props.put (' mail.SMTP.SSL.enable ',' true ' ); props.put (' mail.SMTP.socket factory.class ',SSL_FACTORY ); props.put('mail.SMTP.port ',' 465 ' ); //props.put('mail.SMTP.port ',' 587 ' ); props.put (' mail.SMTP.socket factory.port ',' 465 ' ); //props.put (mail.SMTP.socket factory.port ',' 587 '; props.put('mail.SMTP.auth ',' true ' ); } publicstaticbooleangmailsender (字符串从、字符串密码、字符串电子邮件、字符串子对象、字符串内容)属性session session=session.getdefaultinstance (props,new Authenticator ) (保护性dpasswordauthenticationgetpasswordauthenenetor ) 消息消息msg=new mime消息(session; try { msg.setfrom (newinternetaddress ) from ); msg.set recipients (message.recipient type.to,internetaddress.parse ) email ); msg.setsubject(subject; MSG.Settext(content ); msg.setsentdate (新日期) ); 传输. send (msg; }catch(addressexceptione ) {e.printStackTrace ); } catch (消息传递单元) {e.printStackTrace ); }返回真; }
到了这里,别以为可以送哈哈,我也这么想,但是还不能发提示
javax.mail.authenticationfailedexception :535-5.7.8 usernameandpasswordnotaccepted
届时需要修改谷歌账户的配置:
登录发送邮件的Gmail帐户
管理您的谷歌帐户-安全性-不太安全的APP应用程序访问-打开访问(不推荐)
或者
访问URL:https://www.Google.com/settings/security/lesssecureapps
将“允许不安全的APP应用程序:关闭”更改为“允许不安全的APP应用程序:打开”
现在,点击发送就可以发送了!
因为踩漏洞很难,所以请夸奖我~