T
T
trapce2017-08-26 10:12:57
Java
trapce, 2017-08-26 10:12:57

How to send a message to mail (for example Gmail) from the GlassFish server?

Why is the message not sent from the server?
Send code:
public class GmailSender {
private static String HOST = "smtp.gmail.com";
private static String USER = "loginGmail";
private static String PASSWORD = "Gmail password";
private static String PORT = "465";
private static String FROM = "from where";
private static String TO = "to";
private static String STARTTLS = "true";
private static String AUTH = "true";
private static String DEBUG = "true";
private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static String SUBJECT = "
public static void send(String text) {
//Use Properties object to set environment properties
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.user", USER);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setText(text);
message.setSubject(SUBJECT);
message.setFrom(new InternetAddress(FROM));
message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(TO));
message.saveChanges();
//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//GmailSender.send("text");
Tested dispatch directly with public static void main(bla-bla); - everything works, but as soon as I try to do the same when calling the doPost(bla-bla) method on the GlassFish4(Local) server, nothing happens, including Exception.
My options are: gmail thinks it's a proxy (and if it is, how do I solve it?) and doesn't let through or it's related to "trusted devices". Again, the account has permissions: untrusted apps and I tried to add a new request like new devicehttps://accounts.google.com/DisplayUnlockCaptcha.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
trapce, 2017-08-26
@trapce

Everything. I solved the issue by breaking glassfish4 stupidly, and in order not to suffer for a long time,
I installed glassfish5, and in the end, you know what ??? Everything worked !!! I don't know what the real reason is...But so many nerves went away and some stupidity solved such a great problem for me!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question