email - java mail API's Transport function not working -
i tried smtp.gmail.com send mail using java api, when used transport.send(mimemessage) shows error, username , password not accepted.
i had followed link , done everything, still did not work.
then, tried using smtptransport, works.
my question is, why did transport.send() not work , how smtptransport worked.
import java.util.properties; import javax.mail.*; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; public class email { email(string from, string pwd, string to, string sub, string msg) { system.out.println("entered"); properties prop = new properties(); prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.host", "smtp.gmail.com"); prop.put("mail.smtp.user", from); // user name prop.put("mail.smtp.password", pwd); // password prop.put("mail.smtp.port", "587"); prop.put("mail.smtp.auth", "true"); authenticator = new authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication("from", "pwd"); } }; system.out.println("authenticating"); session ses = session.getdefaultinstance(prop, a); system.out.println("obtained sesion"); system.out.println(ses); try { mimemessage m = new mimemessage(ses); m.setfrom(new internetaddress(from)); m.addrecipient(message.recipienttype.to, new internetaddress(to)); m.setsubject(sub); m.settext(msg); system.out.println("message constructed"); transport.send(m); // / method causes error system.out.println("transported"); system.out.println("send successfully"); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { new email("xxxxxxxxxxxxxxxx@gmail.com", "xxxxxx", "xxxxx@gmail.com", "javamail", "firstmail"); } }
you have make mistake passing from
, pwd
hardcoded passwordauthentication constructor. remove hard coded values , pass from
, pwd
method.
i have made correction in class given bellow code .
public class email { email(final string from,final string pwd,string to,string sub,string msg){ .... authenticator a=new authenticator() { protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(from,pwd); } }; ... }
Comments
Post a Comment