我安装了邮件插件:

grails install-plugin mail

我根据插件添加了配置:
grails {
    mail {
      host = "smtp.gmail.com"
      port = 465
      username = "youraccount@gmail.com"
      password = "yourpassword"
      props = ["mail.smtp.auth":"true",
               "mail.smtp.socketFactory.port":"465",
               "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
               "mail.smtp.socketFactory.fallback":"false"]
    }
}

我向我的Bootstrap.groovy添加了sendMail
class BootStrap {
    def mailService

    def init = { servletContext ->

        try{
            mailService.sendMail {
             from "username@gmail.com"
             to "username@gmail.com"
             subject "Hello"
             body "Mail"
            }
           }catch (Exception e){
            println e
           }
    }
    def destroy = {
    }
}

但是当我执行应用程序时,我得到了
java.net.ConnectException: Connection refused: connect

完整的错误日志是
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect; message exceptions (1) are:
Failed message 1: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect

最佳答案

重新启动服务器,它将为您工作。当我这样做时,它对我有用。

希望能帮助到你!!

09-04 07:48
查看更多