I am getting the following error when setting up an email service with spring boot while trying to connect to round cube:
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
This leads me to think roundcude is not using a SSL connection and I should not use port 143. Therefore I try and use port 25, but I get the following error when I do.
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
application.properties
#email setup
spring.mail.host = mail.email address.com
spring.mail.username = email address
spring.mail.password = my password
spring.mail.properties.mail.smtp.auth= true
spring.mail.port = 25 or port 145
spring.mail.properties.mail.smtp.socketFactory.class= javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback= false
spring.mail.properties.mail.smtp.ssl.enable = true
Email service
#Component
public class EmailServiceImpl implements EmailService {
#Autowired
private JavaMailSender javaMailSender;
#Override
public void sendEmail(String toAddress, String fromAddress,
String subject, String body) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(fromAddress);
simpleMailMessage.setTo(toAddress);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(body);
javaMailSender.send(simpleMailMessage);
}
}
I looked at roundcube's documentation and apparently is uses port 143 so this is rather confusing. This making me think I am setting this up wrong.
I also tried gmail but since I have a two factor authentication I ran into more issue so I decided to use roundcube which is what I would rather use anyway.
Advice?
It seems you're trying to connect to SMTP using non-secure ports. Usually secure ports would be 587 or 465.
This is the configuration that works for me to send e-mail using GMail:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=my-email#gmail.com
spring.mail.password=my-password
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.connectiontimeout = 60000
spring.mail.properties.mail.smtp.timeout = 60000
Related
I've been trying to send an email programmatically from the server using SimpleEmail. I use Kotlin. So far it always leads to an error that seems to only use port 465 despite setting it to a different port. I've been trying to find out why it does this but I have not seen any point this out.
SimpleEmail().apply {
hostName = "smtp.gmail.com"
setSmtpPort(587)
setAuthenticator(DefaultAuthenticator("**email**", "**password**"))
setSSLOnConnect(true)
setFrom("**email**")
subject = "TEST"
setMsg("TEST")
addTo(email)
}.send()
The error:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
A little late, but maybe it still helps someone.
Ports 25 and 587 use TLS, while port 465 uses SSL. If you setSSLOnConnect, then it forces the port 465, because that is the SSL port.
Instead, you have to use setStartTLSEnabled and optionally setStartTLSRequired to true, but not setSSLOnConnect.
hostName should be = "smtp.googlemail.com" but not "smtp.gmail.com", if it does not work, check the gmail settings for access to smpt.
const val myEmail = "test#gmail.com"
const val myPassword = "test"
const val receivingAddress = "test"
fun main(args: Array<String>) {
SimpleEmail().apply {
hostName = "smtp.googlemail.com"
isSSLOnConnect = true
subject = ("subject")
setSmtpPort(465)
setAuthenticator(DefaultAuthenticator(myEmail, myPassword))
setFrom(myEmail)
setMsg("message")
addTo(receivingAddress)
}.send() // will throw email-exception if something is wrong
}
I'm making AuthenticationProvider that during authentication connects to Zookeeper (the same it is running on) and check in node if this user gave correct password.
Basically the flow looks smth like this:
#Override
public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) {
final String usernameColonPassword = new String(authData);
String[] split = usernameColonPassword.split(":");
final String username = split[0];
final String password = split[1];
byte[] binary = curator.getData().forPath(ATUH_NODE); // here error is thrown
// check is password is correct
}
The problem is that all the time I'm getting KeeperErrorCode = ConnectionLoss at line when curator get data. What is the reason of this behavior? During an authentication I'm not allowed to do connection to Zookeeper that is authorizing client?
Below full stacktrace:
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for XXX
at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.exists(ZooKeeper.java:1045)
at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:172)
at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:161)
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107)
at org.apache.curator.framework.imps.ExistsBuilderImpl.pathInForeground(ExistsBuilderImpl.java:157)
at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:148)
at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:36)
As I found it is impossible. At this point zookeeper is having some kind of lock/semaphore and he's not accepting new connections from this thread.
I have tried to troubleshoot this issue a lot and now as a last resort posting it here. Please help me!
Issue: I am able to send mail by calling the class from a main method. When I try to call the same class from a Struts2 Action class it does not send an email, rather gives the below error. I am using Jboss EAP 7.0.0.Alpha1. I think the issue might be lying in the JBoss configuration. I have also changed the Standalone-full.xml file.(To note I am using standalone-full.xml and my other web components are working fine).
I did run it in debug mode and saw all values are getting populated. When we run it from the struts2 action class it does not send the message.
Changes made to Standalone-full.xml:
<mail-session name="java:jboss/mail/Default"
from="somedummyuser#gmail.com" jndi-
name="java:jboss/mail/Default">
<smtp-server password="******" username="somedummyuser"
ssl="true" outbound-socket-binding-ref="mail-smtp"/>
</mail-session>
</subsystem>
<outbound-socket-binding name="mail-smtp">
<remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>
Java Code:
public class SendEmail {
private MailSender mailSender;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String from, String to, String subject, String
msg)
throws Exception,NamingException{
//creating message
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(msg);
//sending message
System.out.println("SimpleMailMessage object:"+simpleMailMessage);
mailSender.send(simpleMailMessage);
}
}
Class with MainMethod Which is executing the above code findE and sending the email successfully:
public class MainMethod_SendEmail {
public static void main(String[] args) throws Exception{
Resource r=new ClassPathResource("webApplicationContext.xml");
BeanFactory b=new XmlBeanFactory(r);
SendEmail m=(SendEmail)b.getBean("sendEmail");
String sender = AccessPropertiesUtil.getInstance().getProperty("from");
String receiver = AccessPropertiesUtil.getInstance().getProperty("to");
String subject =
AccessPropertiesUtil.getInstance().getProperty("subject");
String message =
AccessPropertiesUtil.getInstance().getProperty("message");
m.sendMail(sender, receiver, subject, message);
System.out.println("success");
}
}
But the same mail sending code is not getting executed from a Struts2 Action class:
Struts2Action Class:
public String execute()
{
emailSending();
}
private void emailSending() throws Exception
{
System.out.println("Sending Email");
String sender =
AccessPropertiesUtil.getInstance().getProperty("from");
String receiver =
AccessPropertiesUtil.getInstance().getProperty("to");
String subject =
AccessPropertiesUtil.getInstance().getProperty("subject");
String message =
AccessPropertiesUtil.getInstance().getProperty("message");
sendEmail.sendMail(sender, receiver, subject, message);
}
Error Trace:
org.springframework.mail.MailSendException: Mail server connection
failed;
nested exception is javax.mail.MessagingException: Could not
connect to SMTP host: smtp.gmail.com, port: 465, response: -1. Failed
messages: javax.mail.MessagingException: Could not connect to SMTP
host: smtp.gmail.com, port: 465, response: -1; message exception details
(1) are:
2016-07-13 02:45:33 ERROR stderr:71 - Failed message 1:
2016-07-13 02:45:33 ERROR stderr:71 - javax.mail.MessagingException:
Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
2016-07-13 02:45:33 ERROR stderr:71 - at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2042)
2016-07-13 02:45:33 ERROR stderr:71 - at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
2016-07-13 02:45:33 ERROR stderr:71 - at
javax.mail.Service.connect(Service.java:364)
I'm having a trouble sending an email using Google's SMTP Server. I've looked for a solution but i have not found one. So here's the code.
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
...
...
...
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
Properties props = new Properties();
props.put("true", "mail.smtp.auth");
props.put("true","mail.smtp.starttls.enable");
props.put("smtp.gmail.com", "mail.smtp.host");
props.put("587", "mail.smtp.port");
Session sess=Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("myemail#gmail.com", "mypassword");
}
}
);
try{
Message message= new MimeMessage(sess);
message.setFrom(new InternetAddress("myemail#gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("myemail#gmail.com"));
message.setSubject("message");
message.setText("text");
Transport.send(message);
JOptionPane.showMessageDialog(null, "Sent!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Each time when i press the button, it show me this error:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
I've tried 3 ports: 25, 465, 587 , but it always gives me that error. I even made new rules for the port 25 to the firewall settings, but that doesn't seem to do the trick. Any thoughts what am i doing wrong? Could hibernate cause the problem? Because i'm using it in this project. Plus, i have installed mysql.
Your program is trying to connect to a local port, that's why it failed. If your listing is real code, you got the key and value backwards in the props.set statements.
I'm working on an application that uses the JavaMail API to send an email, but I keep getting errors. I'm using Eclipse to code it and I'm using gmail to send it. I took out my real password for obvious reasons so you're probably going to need to replace that with your own if you need to experiment. Now that I've fixed some stuff thanks to you guys, I think I'm getting a timeout error because it takes a long time until it displays the error, but beyond that much, I haven't the foggiest clue. Thanks in advance for any help or advice once more.
Code:
package com.brighamcampbell.sunrisegundersonmail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class Mail {
public static void main(String[] args) throws MessagingException,
UnsupportedEncodingException {
Properties mailProps = new Properties();
mailProps.put("mail.smtp.from", "butterscotchdreamer23#gmail.com");
mailProps.put("mail.smtp.host", "smtp.gmail.com");
mailProps.put("mail.smtp.ssl.trust", "smtp.gmail.com");
mailProps.put("mail.smtp.port", "465");
mailProps.put("mail.smtp.auth", true);
mailProps.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(mailProps, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("butterscotchdreamer23#gmail.com", "password");
}
});
MimeMessage message = new MimeMessage(mailSession);
//set the email sender
message.setFrom(new InternetAddress("butterscotchdreamer23#gmail.com"));
//set the email recipients
String[] emails = { "butterscotchdreamer23#gmail.com" };
InternetAddress dests[] = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
dests[i] = new InternetAddress(emails[i].trim().toLowerCase());
}
message.setRecipients(Message.RecipientType.TO, dests);
//set the email subject
message.setSubject("test");
//set the email content
message.setText("this is a test");
//send
System.out.println("sending...");
Transport.send(message);
System.out.println("done sending email!");
}
}
Error:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2041)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
at javax.mail.Service.connect(Service.java:386)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at com.brighamcampbell.sunrisegundersonmail.Mail.main(Mail.java:56)
Thanks for the patience!
Clearly the mail server doesn't support STARTTLS, so you shouldn't enable it.
Try removing the 3 sslfactory properties as it is not needed anymore for javamail.You should be able to connect that way.
Note:The gmail server definitely support STARTTLS:
x#test:~/$ telnet smtp.gmail.com 587
Trying 74.125.136.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP vv9sm12826892wjc.35 - gsmtp
EHLO me
250-mx.google.com at your service, [147.58.51.121]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
QUIT
General Gmail instructions are here, general connection debugging tips are here, and a list of common mistakes is here. Possibly you're running into a firewall or anti-virus program that's intercepting your attempt to connect. If you still can't make it work, post the JavaMail debug output.