Search code examples
springserversmtpgmailimap

Cannot used GMail server in spring boot application


I don't know whether this is the right place to asked but anyway, I am trying to use gmail as a mail server in my Spring Boot Application. When I run it locally, I can sent the mail with out an error. But when I deploy in my server. There is an error. What could be the problem?

We have open all ports in the server

This is the error report: OPPS! Content not found |Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection| 500

HTML SCRIPT:

<form method="GET" action="/contactusSave" class="">
                <div class="form-group">
                    <label for="email">Name</label>
                     <input type="text" class="form-control" id="fname" placeholder="Full Name" name="name">
                </div>
                <div class="form-group">
                    <label for="email">Email</label>
                     <input type="text" class="form-control" id="fname" placeholder="Enter Email" name="email">
                </div>
                <div class="form-group">
                    <label for="email">Subject</label>
                     <input type="text" class="form-control" id="fname" placeholder="Subject" name="subject">
                </div>

                <div class="form-group mt-3">
                    <label for="comment">Description:</label>
                    <textarea class="form-control" rows="5" id="comment" placeholder="Add Description" name="description"></textarea>
                </div>


                 <button class="admin-login-button login-button">Submit</button>
</form>

Controller:

@Controller
@RequestMapping("")
public class DemoController {

@Autowired
JavaMailSender sender;

@RequestMapping(value = "/contactusSave", method = RequestMethod.GET)
    public String contactusSave(@RequestParam("name") String username,
        @RequestParam("email") String email,
        @RequestParam("subject") String subject,
        @RequestParam("description") String description {


    System.out.println("email");
    String email = "*****@gmail.com";
    String body = "From: "+email +"\n" + "Description: "+ description;

    MyHelper.sendMail(sender,email, subject, body);   

    return "redirect:/index";
}

My Helper Class:

@Service
public class MyHelper {
public static void sendMail(JavaMailSender senders, String mEmail, String messageSubject, String messageBody) {

            SimpleMailMessage msg = new SimpleMailMessage();

            msg.setTo(mEmail);

            msg.setSubject(messageSubject);
            msg.setText(messageBody);
            senders.send(msg);

        }
}

Application Properties:

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=****@gmail.com
spring.mail.password=****

#new properties added
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth = true;
spring.mail.properties.mail.socketFactory.port=587

spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFacto ry spring.mail.properties.mail.socketFactory.fallback=false

This is the new error message i get after adding new properties in the application properties file: OPPS! Content not found |Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1; nested exception is: java.net.SocketException: Permission denied: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1; nested exception is: java.net.SocketException: Permission denied: connect| 500


Solution

  • The problem has been solved. There was a problem with anti-virus in the server.