java - Exception in thread "main" javax.mail.AuthenticationFailedException -
the code i'm using send simple mail:
import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; import javax.activation.datahandler; import javax.activation.filedatasource; public class sendmailusingauthentication { public static final string smtp_host_name = "smtp.gmail.com"; public static final string smtp_auth_user = "username@gmail.com"; public static final string smtp_auth_pwd = "password"; public static final string emailmsgtxt ="error found wgile clicking on element"; public static final string emailsubjecttxt ="test email"; public static final string emailfromaddress ="hiphop.sujai@gmail.com"; public static final string[] emaillist = {"receiver.username@gmail.com"}; public void postmail(string recipients [], string subject, string message, string from) throws messagingexception { boolean debug = false; properties props = new properties(); props.put("mail.smtp.starttls.enable","true"); props.put("mail.smtp.host", smtp_host_name); props.put("mail.smtp.auth", "true"); authenticator auth = new smtpauthenticator(); session session = session.getdefaultinstance(props, auth); session.setdebug(debug); message msg = new mimemessage(session); internetaddress addressfrom = new internetaddress(from); msg.setfrom(addressfrom); internetaddress[] addressto = new internetaddress[recipients.length]; (int = 0; < recipients.length; i++) { addressto[i] = new internetaddress(recipients[i]); } msg.setrecipients(message.recipienttype.to, addressto); msg.setsubject(subject); msg.setcontent(message, "text/plain"); transport.send(msg); system.out.println("successfully sent mail users"); } private class smtpauthenticator extends javax.mail.authenticator { public passwordauthentication getpasswordauthentication() { string username = smtp_auth_user; string password = smtp_auth_pwd; return new passwordauthentication(username, password); } } }
when run above code got following exception, user name provide , password provided correct. tried giving username without adding @gmail.com still doesn't work.
exception in thread "main" javax.mail.authenticationfailedexception: 534-5.7.14 <https://accounts.google.com/continuesignin?sarp=1&scc=1&plt=akgnsbvti 534-5.7.14 qlg6lxdgfhnbfadbrttzo4ynmurcja7ksqyojglcds9layfp42i6l75p9b_90npxqr-bj- 534-5.7.14 0yzk9vwoigatogtd3_d4vp4fej7c8octftj4krjfrjwjjvgpzdz7ujc1znyksymyhlbaip 534-5.7.14 lpypcxjklz90-rbx5ndwejtu_1u1fenax4qxvjsyaeeujxmjfk6c1azbmm2e485o3pv2h4 534-5.7.14 9rq-6ha> please log in via web browser , try again. 534-5.7.14 learn more @ 534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 om7sm11753908pdb.61 - gsmtp @ com.sun.mail.smtp.smtptransport$authenticator.authenticate (smtptransport.java:648) @ com.sun.mail.smtp.smtptransport.protocolconnect(smtptransport.java:583) @ javax.mail.service.connect(service.java:313) @ javax.mail.service.connect(service.java:172) @ javax.mail.service.connect(service.java:121) @ javax.mail.transport.send0(transport.java:190) @ javax.mail.transport.send(transport.java:120) @ sendmailusingauthentication.postmail(sendmailusingauthentication.java:87) @ capture_screen.main(capture_screen.java:32)
i had same issue. turns out google blocks suspicious attempts access mail accounts. logged in gmail in browser , there error/warning message @ top of window alerting me of suspicious activity. after clicking on message able allow connection "suspicious" host indeed app running on aws.
Comments
Post a Comment