我从grails发送邮件时遇到问题。向端口587添加一个例外,但没有任何帮助。仍然发生错误:
连接被拒绝:连接。 Stacktrace如下:
java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:432)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
at grails.plugins.mail.MailMessageBuilder.sendMessage(MailMessageBuilder.groovy:131)
at grails.plugins.mail.MailService.sendMail(MailService.groovy:55)
at grails.plugins.mail.MailService.sendMail(MailService.groovy:59)
at grailsproject.UserController.save(UserController.groovy:31)
... 14 common frames omitted
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2118)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712)
at javax.mail.Service.connect(Service.java:366)
at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421)
... 20 common frames omitted
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2084)
... 24 common frames omitted
application.yml
mail:
host: "smtp.gmail.com"
port: 465
username: "***@gmail.com"
password: "***"
props:
mail.smtp.auth: "true"
mail.smtp.socketFactory.port: "465"
mail.smtp.socketFactory.class: "javax.net.ssl.SSLSocketFactory"
mail.smtp.socketFactory.fallback: "true"
build.gradle
runtime "org.grails.plugins:mail:2.0.0.RC4"
我也尝试过: 运行时“ org.grails.plugins:mail:2.0.0” 运行时“ org.grails.plugins:mail:2.0.0.RC6” 而且也不起作用。
UserController.groovy
package grailsproject
class UserController {
def mailService
def form() {
}
def save() {
def user = new Userr(params)
user.validate()
if(user.hasErrors())
{
user.errors.allErrors.each {
println it
}
render (view:"/registrationForm/registrationForm", model: [user: user]);
}else
{
user.save(flush: true)
render(view: "user", model: [user: user]);
}
mailService.sendMail{
to "***@gmail.com"
from "***@gmail.com"
subject "This is a test mail"
body "Hello, This is a test mail, how are you?"
}
}
}
我不知道,我还要检查什么... 我也想知道为什么我给gmail主机时为什么尝试连接本地主机?
答案 0 :(得分:1)
您的application.yml应该是
grails:
mail:
host: "smtp.gmail.com"
port: 465
username: "your_mail@gmail.com"
password: "Your_password"
props:
mail.smtp.auth: "true"
mail.smtp.socketFactory.port: "465"
mail.smtp.socketFactory.class: "javax.net.ssl.SSLSocketFactory"
mail.smtp.socketFactory.fallback: "false"
build.gradle将是
compile "org.grails.plugins:mail:2.0.0.RC6"
UserController.groovy应该是
package grailsproject
class UserController {
def index() { }
def send() {
def multipartFile = request.getFile('attachment')
sendMail {
multipart true
to params.address
subject params.subject
html params.body
if(multipartFile && !multipartFile.empty) {
File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + multipartFile.getOriginalFilename());
multipartFile.transferTo(tmpFile);
attach tmpFile
}
}
flash.message = "Email Sent Successfully"
redirect action:"index"
}
}