web applications - Sending email from java -
i trying send confirmation email users of web application, getting following error:
pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target
this java class:
package it.bcsoft.onlinestore.mail; import java.util.properties; import javax.mail.messagingexception; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.addressexception; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import javax.mail.internet.mimemessage.recipienttype; public class emailsender { public static boolean sendemail(string from, string pass, string message, string[] to) { string host="smtp.gmail.com"; properties props= system.getproperties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", pass); props.put("mail.smtp.port", 25); props.put("mail.smtp.auth", "true"); session session= session.getdefaultinstance(props, null); mimemessage mime= new mimemessage(session); try { mime.setfrom(new internetaddress(from)); internetaddress[] toaddress= new internetaddress[to.length]; for(int i=0; i<to.length; i++) { toaddress[i]= new internetaddress(to[i]); } for(int i=0; i<toaddress.length;i++) { mime.addrecipient(recipienttype.to, toaddress[i]); } mime.setsubject("mail onlinestore"); mime.settext(message); transport transport= session.gettransport("smtp"); transport.connect(host, from, pass); transport.sendmessage(mime, mime.getallrecipients()); transport.close(); return true; } catch (addressexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (messagingexception e) { // todo auto-generated catch block e.printstacktrace(); } return false; } }
i not have smtp server installed on pc , using java 1.7
make sure using right port. use gmail's smtp server via tls, have use port 587
.
have @ this blog post on how build smtp connection gmail.
Comments
Post a Comment