spring - Java send email through gmail, sometimes works sometimes hang -
i sending email gmail through web app.
however, working fine stop without message.
does know how solve this?
in spring appconfig.java
@bean public javamailsender getmailsender(){ javamailsenderimpl mailsender = new javamailsenderimpl(); mailsender.sethost("smtp.gmail.com"); mailsender.setport(587); mailsender.setusername("my email from"); mailsender.setpassword("my email password"); properties javamailproperties = new properties(); javamailproperties.put("mail.smtp.starttls.enable", "true"); javamailproperties.put("mail.smtp.auth", "true"); //javamailproperties.put("mail.transport.protocol", "smtps"); javamailproperties.put("mail.transport.protocol", "smtp"); javamailproperties.put("mail.debug", "true"); mailsender.setjavamailproperties(javamailproperties); return mailsender; }
in mailservice.java
public void sendemailwithtemplate(activity activity, object object) { member member = (member) object; mimemessagepreparator verificationemail = getemailfromactivity(activity, member); try { mailsender.send(verificationemail); system.out.println("message sent............................."); } catch (mailexception ex) { system.err.println(ex.getmessage()); } } private mimemessagepreparator getemailfromactivity(final activity activity, final member member) { mimemessagepreparator preparator = new mimemessagepreparator() { public void prepare(mimemessage mimemessage) throws exception { mimemessagehelper helper = new mimemessagehelper(mimemessage, true); helper.setsubject(activity.getactivity_subject()); helper.setfrom("my email"); helper.setto(member.getemail()); string mailcontent = activity.getactivity_content(); helper.settext(mailcontent, true); } }; return preparator; }
sometimes working fine , able send mail, sometime stop more 20 minutes at:
debug smtp: enable sasl debug smtp: useehlo true, useauth false debug smtp: trying connect host "smtp.gmail.com", port 587, isssl false
this method other post, can't post. activity 1 of object storing subject , content.
public void sendemailwithactivity(activity activity, object object, string path) { try { member member = (member) object; string host = "smtp.gmail.com"; string username = "email"; string password = "password"; string body = activity.getactivity_content(); string name = activity.getactivity_name(); string subject = activity.getactivity_subject();= //set properties properties props = new properties(); props.put("mail.smtps.auth", "true"); // set session here session session = session.getdefaultinstance(props); mimemessage msg = new mimemessage(session); // set message content here msg.setsubject(subject); msg.setcontent(body, "text/html"); msg.setfrom(new internetaddress(username)); msg.addrecipient(message.recipienttype.to, new internetaddress(member.getemail())); transport t = session.gettransport("smtps"); t.connect(host, username, password); t.sendmessage(msg, msg.getallrecipients()); t.close(); } catch (exception e) { e.printstacktrace(); } { } }
Comments
Post a Comment