email - Issues with sending thru Gmail using Java -
i have appears simple code try , send email java app using gmail account. when run it, crashes exception javax.mail.authenticationfailedexception. here code:
// recipient's email id needs mentioned. string = "fredxya@gmail.com"; // sender's email id needs mentioned string = "johnxyzn@gmail.com"; properties props = new properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketfactory.port", "465"); props.put("mail.smtp.socketfactory.class", "javax.net.ssl.sslsocketfactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); session session = session.getdefaultinstance(props, new javax.mail.authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication("fredxyz@gmail.com","password"); } }); try{ // create default mimemessage object. mimemessage message = new mimemessage(session); // set from: header field of header. message.setfrom(new internetaddress(from)); // set to: header field of header. message.addrecipient(message.recipienttype.to, new internetaddress(to)); // set subject: header field message.setsubject("test subject"); // set actual message message.settext("this test"); // send message transport.send(message); system.out.println("sent message successfully...."); }catch (messagingexception mex) { system.out.println (mex) ; mex.printstacktrace(); }
issue was using 2 step verification in gmail. solution use application specific password (asp): https://accounts.google.com/issuedauthsubtokens?hide_authsub=1
Comments
Post a Comment