Send mail without authentication

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

Moderators: Lapo, Bax

User avatar
gabrielvpy
Posts: 69
Joined: 23 Jul 2015, 20:18

Send mail without authentication

Postby gabrielvpy » 23 Mar 2021, 12:51

We need to switch to gmail's smtp-relay configuration which doesn't require authentication. Is there a way to do this?

This java code is a working example.

Code: Select all

public static void testEmailRelay(){
        // Recipient's email ID needs to be mentioned.
        String[] to = {"someone@gmail.com"};

        // Sender's email ID needs to be mentioned
        String from = "no-reply@our.domain";

        // Assuming you are sending email from through gmails smtp
        String host = "smtp-relay.gmail.com";

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "false");

        // Get the Session object.// and pass username and password
        Session session = Session.getInstance(properties);

        // Used to debug SMTP issues
        session.setDebug(true);

        String messageBody = "Test email body";

        for (int i = 0; i < to.length; i++) {
            sendEmail(to[i], from, session, messageBody);
        }

    }

    private static void sendEmail(String to, String from, Session session, String messageBody) {
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            // Set Subject: header field
            message.setSubject("Test email subject");

            // Now set the actual message
            message.setText(messageBody);

            System.out.println("sending...");
            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }


The attached image show a config I tested to no prevail.
Attachments
mail relay.png
(32.11 KiB) Not downloaded yet
We made an awesome GPS - MOBA.
Try it out here: Get Fhacktions
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send mail without authentication

Postby Lapo » 23 Mar 2021, 15:31

Hi,
the native SmartFoxServer system for sending emails is via an SMTP server, which typically requires authentication although there can be exceptions if you manage your own SMTP.

Otherwise, If you have access to another way/service that doesn't rely on connecting to an SMTP directly then you can bypass our system and use your own.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
gabrielvpy
Posts: 69
Joined: 23 Jul 2015, 20:18

Re: Send mail without authentication

Postby gabrielvpy » 23 Mar 2021, 16:25

Ok, in that case I need your help. When I run that code in sfs2x it doesn't set the mail.smtp.ssl.enable correctly.

when I run the code on a stand alone jar this is the output

Code: Select all

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp-relay.gmail.com", port 465, isSSL true

and it works just fine.

but when I run it on sfs2x this is the output

Code: Select all

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp-relay.gmail.com", port 465, isSSL false

DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: could not connect to host "smtp-relay.gmail.com", port: 465, response: -1

javax.mail.MessagingException: Could not connect to SMTP host: smtp-relay.gmail.com, port: 465, response: -1
   at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1201)
   at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
   at javax.mail.Service.connect(Service.java:233)
   at javax.mail.Service.connect(Service.java:134)
   at javax.mail.Service.connect(Service.java:86)
   at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
   at javax.mail.Transport.send0(Transport.java:150)
   at javax.mail.Transport.send(Transport.java:80)
   at com.hackembostudios.crypto_game.zone.ZoneExtension.sendEmail(ZoneExtension.java:232)
   at com.hackembostudios.crypto_game.zone.ZoneExtension.rateWarning(ZoneExtension.java:207)
   at com.hackembostudios.crypto_game.zone.ZoneExtension.init(ZoneExtension.java:162)
   at com.smartfoxserver.v2.entities.managers.SFSExtensionManager.createExtension(SFSExtensionManager.java:303)
   at com.smartfoxserver.v2.entities.managers.SFSZoneManager.createZone(SFSZoneManager.java:428)
   at com.smartfoxserver.v2.entities.managers.SFSZoneManager.initializeZones(SFSZoneManager.java:239)
   at com.smartfoxserver.v2.SmartFoxServer.start(SmartFoxServer.java:293)
   at com.smartfoxserver.v2.Main.main(Main.java:13)


isSSL turns to false even though this line is in the code: properties.put("mail.smtp.ssl.enable", "true");
We made an awesome GPS - MOBA.
Try it out here: Get Fhacktions
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send mail without authentication

Postby Lapo » 23 Mar 2021, 17:51

Usually when sending via GMail SMTP you shouldn't you use smtp.gmail.com? I don't know what smtp-relay does.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
gabrielvpy
Posts: 69
Joined: 23 Jul 2015, 20:18

Re: Send mail without authentication

Postby gabrielvpy » 23 Mar 2021, 17:55

That's the relay they use for you to bypass the authentication process. But you need to access it from a whitelisted IP. But that's beside the point.
Why would the config change the isSSL to false? That's the problem I'm facing.
We made an awesome GPS - MOBA.
Try it out here: Get Fhacktions
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send mail without authentication

Postby Lapo » 24 Mar 2021, 10:18

I have no idea, honestly. :( It could be an issue specific to the relay server.
Testing with the standard gmail SMTP works fine, but I don't know what the relay server does, or what kind of requirements does it have.

If you want you can bypass the SFS2X mailer and work directly with the Java Mail API, which is already included with SFS2X.
I would also check what are the specific requirements for connecting and sending emails with Gmail relay server, if they're available.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
gabrielvpy
Posts: 69
Joined: 23 Jul 2015, 20:18

Re: Send mail without authentication

Postby gabrielvpy » 24 Mar 2021, 13:45

The issue I'm reporting has nothing to do with the relay. Before java even tries to reach the smtp server it is ignoring the configuration. I'll post my code here (it uses the regular gmail smtp server). Notice how the the isSSL is the issue. Can you please run this in a test zoneExtension in the init() to test it? Use your a valid account please

Code: Select all

public void sendEmailJava(){
        // Recipient's email ID needs to be mentioned.
        String[] to = {"to@gmail.com"};

        // Sender's email ID needs to be mentioned
        String from = "from@gmail.com";

        // Assuming you are sending email from through gmails smtp
        String host = "smtp.gmail.com";

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "true");

        logInfo("properties: " + properties.toString());

        // Get the Session object.// and pass username and password
//        Session session = Session.getInstance(properties);
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication("from@gmail.com", "yourpassword");

            }

        });
        // Used to debug SMTP issues
        session.setDebug(true);

        String messageBody = "Mail de prueba desde el java de smartfoxserver";

        for (int i = 0; i < to.length; i++) {
            sendEmail(to[i], from, session, messageBody);
        }


    }

    private void sendEmail(String to, String from, Session session, String messageBody) {
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            // Set Subject: header field
            message.setSubject("Coin Hunt Test Email");

            // Now set the actual message
            message.setText(messageBody);

            logInfo("sending...");
            // Send message
            Transport.send(message);
            logInfo("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }


Before sending the email I log the properties which has "mail.smtp.ssl.enable=true" but then the mail logger prints out the SSL as false when it was set to true. See below.

Code: Select all

13:32:24,628 INFO  [main] zone.ZoneExtension     - properties: {java.runtime.name=OpenJDK Runtime Environment, sun.boot.library.path=/home/ubuntu/SmartFoxServer_2X/jre/lib/amd64, java.vm.version=25.222-b10, mail.smtp.auth=true, java.vm.vendor=AdoptOpenJDK, java.vendor.url=http://java.oracle.com/, path.separator=:, java.vm.name=OpenJDK 64-Bit Server VM, file.encoding.pkg=sun.io, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/home/ubuntu/SmartFoxServer_2X/SFS2X, java.runtime.version=1.8.0_222-b10, java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, java.endorsed.dirs=/home/ubuntu/SmartFoxServer_2X/jre/lib/endorsed, os.arch=amd64, java.io.tmpdir=/tmp, line.separator=
, java.vm.specification.vendor=Oracle Corporation, os.name=Linux, mail.smtp.ssl.enable=true, sun.jnu.encoding=UTF-8, java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib, java.specification.name=Java Platform API Specification, java.class.version=52.0, mail.smtp.port=465, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, os.version=5.4.0-1039-aws, user.home=/home/ubuntu, user.timezone=Etc/UTC, java.awt.printerjob=sun.print.PSPrinterJob, file.encoding=UTF-8, java.specification.version=1.8, java.class.path=./:lib/sfs2x-util.jar:lib/sfs2x.jar:lib/commons-lang-2.4.jar:lib/maxmind-db-1.2.1.jar:lib/jmx-util-1.2.3.jar:lib/commons-pool-1.5.4.jar:lib/smtp.jar:lib/commons-collections-3.2.2.jar:lib/scala-library-2.11.6.jar:lib/postgresql-42.2.5.jar:lib/geoip2-2.8.0.jar:lib/joda-time-2.2.jar:lib/logs-backup-app.jar:lib/httpcore-4.4.9.jar:lib/sfs2x-admin.jar:lib/slf4j-log4j12-1.7.5.jar:lib/commons-dbcp-1.4.jar:lib/commons-validator-1.5.0.jar:lib/commons-compress-1.19.jar:lib/commons-vfs-1.0.jar:lib/commons-logging-1.2.jar:lib/mailapi.jar:lib/jackson-databind-2.8.2.jar:lib/jackson-annotations-2.8.0.jar:lib/jackson-core-2.8.2.jar:lib/hsqldb.jar:lib/commons-codec-1.9.jar:lib/imap.jar:lib/log4j-1.2.15.jar:lib/logs-analysis-app.jar:lib/boot.jar:lib/xpp3_min-1.1.4c.jar:lib/commons-beanutils-1.7.0.jar:lib/sfs2x-lib.jar:lib/jython.jar:lib/commons-io-1.4.jar:lib/guava-18.0.jar:lib/antlr-2.7.7.jar:lib/httpclient-4.5.2.jar:lib/json-lib-2.2.3-jdk15.jar:lib/pop3.jar:lib/ezmorph-1.0.6.jar:lib/slf4j-api-1.7.5.jar:lib/sfs2x-core.jar:lib/xstream-1.3.1.jar:lib/stringtemplate-3.2.1.jar:lib/apache-tomcat/bin/tomcat-juli.jar:lib/apache-tomcat/bin/bootstrap.jar:lib/apache-tomcat/bin/commons-daemon.jar:extensions/__lib__/google-http-client-jackson2-1.30.1.jar:extensions/__lib__/google-api-services-games-v1-rev20200528-1.30.9.jar:extensions/__lib__/opencensus-api-0.21.0.jar:extensions/__lib__/restfb-1.35.0.jar:extensions/__lib__/gson-2.1.jar:extensions/__lib__/google-http-client-1.30.1.jar:extensions/__lib__/json-20160810.jar:extensions/__lib__/server-sdk-0.113.200629.jar:extensions/__lib__/google-oauth-client-1.30.1.jar:extensions/__lib__/opencensus-contrib-http-util-0.21.0.jar:extensions/__lib__/grpc-context-1.19.0.jar:extensions/__lib__/transfer_bot-1.0.jar:extensions/__lib__/google-api-client-1.30.2.jar:extensions/__lib__/okhttp-3.4.1.jar:extensions/__lib__/okio-1.9.0.jar, user.name=ubuntu, java.vm.specification.version=1.8, sun.java.command=com.smartfoxserver.v2.Main, java.home=/home/ubuntu/SmartFoxServer_2X/jre, sun.arch.data.model=64, user.language=en, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.awt.X11.XToolkit, java.vm.info=mixed mode, java.version=1.8.0_222, java.ext.dirs=/home/ubuntu/SmartFoxServer_2X/jre/lib/ext:/usr/java/packages/lib/ext, sun.boot.class.path=/home/ubuntu/SmartFoxServer_2X/jre/lib/resources.jar:/home/ubuntu/SmartFoxServer_2X/jre/lib/rt.jar:/home/ubuntu/SmartFoxServer_2X/jre/lib/sunrsasign.jar:/home/ubuntu/SmartFoxServer_2X/jre/lib/jsse.jar:/home/ubuntu/SmartFoxServer_2X/jre/lib/jce.jar:/home/ubuntu/SmartFoxServer_2X/jre/lib/charsets.jar:/home/ubuntu/SmartFoxServer_2X/jre/lib/jfr.jar:/home/ubuntu/SmartFoxServer_2X/jre/classes, java.vendor=AdoptOpenJDK, file.separator=/, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, mail.smtp.host=smtp.gmail.com, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=}
DEBUG: setDebug: JavaMail version 1.3.2
13:32:24,768 INFO  [main] zone.ZoneExtension     - sending...
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
We made an awesome GPS - MOBA.
Try it out here: Get Fhacktions
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send mail without authentication

Postby Lapo » 24 Mar 2021, 15:50

Gives the same problem to me.
To fix this you need several steps.

1) You need to specify the smtps Transport. Like this:

Code: Select all

private void sendEmail(String to, String from, Session session, String messageBody)
    {
       
        try
        {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            // Set Subject: header field
            message.setSubject("Coin Hunt Test Email");

            // Now set the actual message
            message.setText(messageBody);

            trace("sending...");
           
            // Send message
            Transport.send(message);
           
            Transport trnsport;
            trnsport = session.getTransport("smtps");
            trnsport.connect();
            message.saveChanges();
            trnsport.sendMessage(message, message.getAllRecipients());
            trnsport.close();
           
           
            trace("Sent message successfully....");
        }
       
        catch (MessagingException mex)
        {
            mex.printStackTrace();
        }
    }


2) This however won't work with the current javax.mail API provided in SFS2X. We upgraded to the latest 1.6.2 which now comes as a single jar which replaces the old 4 jar files: smtp,jar, imap.jar, pop3.jar, mailapi.jar

Now it works
p.s. = we'll look into updating the java mail API with the next update.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Send mail without authentication

Postby Lapo » 24 Mar 2021, 16:10

UPDATE: it also works from regular SFS2X Mail API, using the standard smtp.gmail.com (using javax.mail 1.6.2)

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
gabrielvpy
Posts: 69
Joined: 23 Jul 2015, 20:18

Re: Send mail without authentication

Postby gabrielvpy » 24 Mar 2021, 17:50

Awesome. Thanks lapo!
We made an awesome GPS - MOBA.
Try it out here: Get Fhacktions

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 85 guests