I need to ask a question very very simple.
I have created an handler in myextension that i want to use when a customer lose his password.
This handler must send an email with a random string.
I have created this:
Code: Select all
public class RandomString {
/**
*
* @param str
*/
private String str = null;
/**
* metod randomString create a String alpahnumerics to send Via Email by the server to confirm the account
* @param alphaNumerics
*/
public RandomString() {
String alphaNumerics = "qwertyuiopasdfghjklzxcvbnm1234567890";
str = "";
for (int i = 0; i < 8; i++) {
str += alphaNumerics.charAt((int) (Math.random() * alphaNumerics.length()));
}
}
/**
* metod that return the string of str
* @param str
*/
public String toString() {
return str;
}
}
this generate a random string
and then i use java sendemail to settingup a send email like this
Code: Select all
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
private String host = "smtp.googlemail.com"; //tuo smtp
private String from = "name@gmail.com"; //tuo indirizzo email
private String ToAddress; //destinatario
private String user = "user";
private String pass = "password";
private MimeMessage msg = null;
private Session session = null;
public SendEmail(String toAddress) {
ToAddress = toAddress;
try {
//Get system properties
Properties props = System.getProperties( );
//Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth","true");
//Get session
session = Session.getDefaultInstance(props, null);
session.setDebug(true);
session.setPasswordAuthentication(new URLName("smtp",host,25,"INBOX",user,pass), new PasswordAuthentication(user,pass));
//Define message
msg = new MimeMessage(session);
//Set the from address
msg.setFrom(new InternetAddress(from));
//Set the to address
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(ToAddress));
}
catch (MessagingException e) {
System.out.println(e);
}
}
public void send() {
//Send message
Transport tr;
try {
tr = session.getTransport("smtp");
tr.connect(host, user, pass);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}catch (MessagingException e) {
e.printStackTrace();
}
}
public void setText(String txt) {
try {
msg.setText(txt);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void setSubject(String s) {
try {
msg.setSubject(s);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String toAddress = "mymail@gmail.com";
SendEmail se = new SendEmail(toAddress);
String rand = new RandomString().toString();
se.setText(rand);
se.setSubject("Prova");
se.send();
}
}
if i use the main in this class works i can receive an automatic mail on gmail.
now i show the code of my handler
Code: Select all
public class PasswordMissingHandler extends BaseClientRequestHandler {
Object obj = null;
SendEmail se = null;
public void handleClientRequest(User user, ISFSObject params){
String email = params.getUtfString("email");
trace("Sto chiedendo di recuperare la password al server");
se = new SendEmail(email);
}
}
at first i have got an error because in smartfox lib there was a missing libraries like joda-time.jar and mail.jar.
Now i have added it in smartfox folder
but i cannot send an email with my handler?
Something is missing??
Thanks for the Help