Using send email

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Using send email

Postby Skills07 » 10 Feb 2017, 08:19

Hello

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
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using send email

Postby Lapo » 10 Feb 2017, 15:35

You can configure the email system in SmartFoxServer and send the email in two lines of code:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item8

Configuration is done via AdminTool > ServerConfigurator > Mailer

hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Using send email

Postby Skills07 » 15 Feb 2017, 08:07

Ok Thanks it works

i need another simple question to you

how can i do a query with like and other parameters like this

Code: Select all

select * from users where name like ? and/or position = ?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using send email

Postby Lapo » 15 Feb 2017, 08:35

I'd recommend reading the SQL documentation for the specific database you're using.

thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Using send email

Postby Skills07 » 15 Feb 2017, 08:51

Lapo wrote:I'd recommend reading the SQL documentation for the specific database you're using.

thanks



i'm using mysql...

the problem its only the like it seems doesn't work with that syntax
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using send email

Postby Lapo » 15 Feb 2017, 11:49

What error do you see and what query exactly are you running?

thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Using send email

Postby Skills07 » 15 Feb 2017, 12:34

No error.
It returns an array with datas not congruent at my query.

I need to search users with
-like filter in the name
-or total trophy like a number (example 50)
-or nationality (example italy)

so if on client i send

(pinco, 30, italy)

my query is
Select * from users where name like %'pinco'% or nationality = 'italy' or total = 30;

But i think i have a problem on syntax beacuse this query returns an array with all datas and not with my filtering
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using send email

Postby Lapo » 15 Feb 2017, 14:29

The syntax doesn't look right:

Code: Select all

Select * from users where name like %'pinco'% or nationality = 'italy' or total = 30;


The percentage sign (%) must go inside the quotes, not outside.

Code: Select all

SELECT * from users where name like '%pinco%' or nationality = 'italy' or total = 30;


I've run a similar query in a test MySQL database using the LIKE expression and it works as expected.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Using send email

Postby Skills07 » 16 Feb 2017, 12:51

Sorry lapo if i take spend you a lot of time

But i have written my query like this

Code: Select all

ISFSArray arr = dbmanager.executeQuery("SELECT * FROM guesswho.Clan where clan_name Like '% ? %' or position = ? or maxUsers <= ? or minUsers >= ?  order by trofei_total desc, clan_name", new Object[] {name, location, maxUsers, minUsers});


but it doesn't work??

I have to write this query in another mode? Like a prepared statment??

or the problem is '% ? %'

Thanks for your help
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using send email

Postby Lapo » 16 Feb 2017, 14:41

What does it mean that it doesn't work?
What is the error you get?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Skills07
Posts: 103
Joined: 07 Nov 2016, 14:54
Location: Italy

Re: Using send email

Postby Skills07 » 16 Feb 2017, 15:02

the server doesn't execute the query

it exits on the try and go to the catch and print sql error.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Using send email

Postby Lapo » 16 Feb 2017, 16:40

Can you please show the error?
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 115 guests