首页 > 编程知识 正文

谷歌邮箱服务器设置(java如何实现发送邮件)

时间:2023-05-03 10:03:15 阅读:71020 作者:4370

首先登录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应用程序:打开”

现在,点击发送就可以发送了!

因为踩漏洞很难,所以请夸奖我~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。