java - working with JavaMailApi in jsp -
i working registration page in need send verification link mail,while executing got error , saying java.net.connectexception: connection refused: connect please me thanku
register.jsp
<html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>registration</title> </head> <body> <%! dbcreation creation; connection connection; %> <% string uid=request.getparameter("userid"); string pwd=request.getparameter("pwd"); string fname=request.getparameter("fname"); string lname=request.getparameter("lname"); string email=request.getparameter("email"); string location=request.getparameter("location"); string encpwd=encryptpwd(pwd); system.out.println(encpwd); connection=dbcreation.getconnection(); string result; // recipient's email id needs mentioned. string toemail = email; // sender's email id needs mentioned string fromemail = "nbarath2008@gmail.com"; // assuming sending email localhost //string host = "localhost"; // system properties object properties properties = system.getproperties(); // setup mail server // properties.setproperty("mail.smtp.host", host); // default session object. session mailsession = session.getdefaultinstance(properties); transport transport = new smtptransport(mailsession,new urlname("localhost")); transport.connect("localhost",25,null,null); try{ // create default mimemessage object. mimemessage message = new mimemessage(mailsession); // set from: header field of header. message.setfrom(new internetaddress(fromemail)); // set to: header field of header. message.setrecipients(message.recipienttype.to,""+email); // set subject: header field message.setsubject("this subject line!"); // send actual html message, big message.setcontent("<h1>this actual message</h1>", "text/html" ); // send message transport.send(message); result = "sent message successfully...."; preparedstatement statement=connection.preparestatement("insert userregistration values(?,?,?,?,?,?,?)"); statement.setstring(1,uid); statement.setstring(2,encpwd); statement.setstring(3,fname); statement.setstring(4, lname); statement.setstring(5, location); statement.setstring(6, fromemail); statement.setstring(7, toemail); int i=statement.executeupdate(); if(i>0) { // send message transport.send(message); result = "sent message successfully...."; requestdispatcher rd=request.getrequestdispatcher("regsuccess.jsp"); rd.forward(request, response); } }catch(exception e) { e.printstacktrace(); } %> </body> </html>
this working code. please ask have doubts!
import java.util.; import javax.mail.; import javax.mail.internet.*;
public class main { private static string user_name = "gmail-user-name"; // gmail user name (just part before "@gmail.com") private static string password = "******"; // enter gmail password private static string recipient = "recipient email address"; public static void main(string[] args) { string = user_name; string pass = password; string[] = { recipient }; // list of recipient email addresses string subject = "java send mail example"; string body = "welcome javamail!"; sendfromgmail(from, pass, to, subject, body); } private static void sendfromgmail(string from, string pass, string[] to, string subject, string body) { properties props = system.getproperties(); string host = "smtp.gmail.com"; 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", "587"); props.put("mail.smtp.auth", "true"); session session = session.getdefaultinstance(props); mimemessage message = new mimemessage(session); try { message.setfrom(new internetaddress(from)); internetaddress[] toaddress = new internetaddress[to.length]; // array of addresses for( int = 0; < to.length; i++ ) { toaddress[i] = new internetaddress(to[i]); } for( int = 0; < toaddress.length; i++) { message.addrecipient(message.recipienttype.to, toaddress[i]); } message.setsubject(subject); message.settext(body); transport transport = session.gettransport("smtp"); transport.connect(host, from, pass); transport.sendmessage(message, message.getallrecipients()); transport.close(); } catch (addressexception ae) { ae.printstacktrace(); } catch (messagingexception me) { me.printstacktrace(); } } }
Comments
Post a Comment