java - JavaMail API has trouble to work with the Gmail -


i'm working on tutorial @ http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/javamail/javamail.html shows how send email javamail api.

so wrote following code snippet

public class emailsessionbean {  private int port = 465; private string host = "smtp.gmail.com"; private string = "xxx@gmail.com"; private boolean auth = true; private string username = "xxx@gmail.com"; private string password = "mypassword"; private protocol protocol = protocol.smtps; private boolean debug = true;  public void sendemail(string to, string subject, string body) {      // create properties object contain settings      // smtp protocol provider.     properties props = new properties();     props.put("mail.smtp.host", host);     props.put("mail.smtp.port", port);     switch (protocol) {         case smtps:             props.put("mail.smtp.ssl.enable", true);             break;         case tls:             props.put("mail.smtp.starttls.enable", true);             break;     }      // if smtp authentication required must set mail.smtp.auth      // property , construct authenticator instance returns      // passwordauthentication instance username , password.      authenticator authenticator = null;     if (auth) {         props.put("mail.smtp.auth", true);         authenticator = new authenticator() {             private passwordauthentication pa                     = new passwordauthentication(username, password);              @override             public passwordauthentication getpasswordauthentication() {                 return pa;             }         };     }      //create session instance using properties object      // , authenticator object.     session session = session.getinstance(props, authenticator);     session.setdebug(debug);      // construct mimemessage instance, populate message headers      // , content , send message     mimemessage message = new mimemessage(session);     try {         message.setfrom(new internetaddress(from));         internetaddress[] address = {new internetaddress(to)};         message.setrecipients(message.recipienttype.to, address);         message.setsubject(subject);         message.setsentdate(new date());         message.settext(body);         transport.send(message);     } catch (messagingexception ex) {         ex.printstacktrace();     } } } 

when try send email web app, gmail stops me doing , mail me "sign-in attempt prevented".

p.s. disabled "access less secure apps" in gmail account. in next attempt doing gmail suspended account.

any useful comment/solution appreciated.

ensure have checked listed here displayunlockcaptcha


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -