我正试图从Ktor应用程序发送电子邮件。当以下代码在localhost上运行时,它可以正常工作
val responseEmail = SimpleEmail()
responseEmail.hostName = "smtp.googlemail.com"
responseEmail.setSmtpPort(465)
responseEmail.setAuthenticator(DefaultAuthenticator("email@gmail.com", "pass"))
responseEmail.isSSLOnConnect = true
responseEmail.setFrom("email@gmail.com")
responseEmail.subject = "Subject"
responseEmail.setMsg("msg")
responseEmail.addTo(email)
responseEmail.send()
即使我设置responseEmail.isSSLOnConnect = false
仍然不起作用但是,当我在VPS(没有SSL(上运行应用程序时,我会得到以下异常:
org.apache.commons.mail.EmailException: Sending the email to the following server failed: smtp.googlemail.com:465
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at org.apache.commons.mail.Email.send(Email.java:1496)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.data.modules.AccountManagementModule.forgottenPassword(AccountManagementModu>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.routes.AccountManagementKt$accountManagementRoute$11$1.invokeSuspend(Account>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.routes.AccountManagementKt$accountManagementRoute$11$1.invoke(AccountManagem>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at eu.bbsapps.forgottenfilmsapi.routes.AccountManagementKt$accountManagementRoute$11$1.invoke(AccountManagem>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing.executeResult(Routing.kt:151)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing.interceptor(Routing.kt:35)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:103)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:110)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.ContentNegotiation$Feature$install$1.invoke(ContentNegotiation.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.CallLogging$Feature$install$2.invokeSuspend(CallLogging.kt:139)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.features.CallLogging$Feature$install$2.invoke(CallLogging.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invokeSuspend(DefaultEnginePipeline>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invoke(DefaultEnginePipeline.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invokeSuspend(NettyApplicationCallHandle>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invoke(NettyApplicationCallHandler.kt)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler.handleRequest(NettyApplicationCallHandler.kt:43)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.NettyApplicationCallHandler.channelRead(NettyApplicationCallHandler.kt:34)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:61)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:370)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.ktor.server.netty.EventLoopGroupProxy$Companion$create$factory$1$1.run(NettyApplicationEngine.kt:216)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at java.lang.Thread.run(Thread.java:748)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 4-yJLa62muQ6uc6fMiBsu1s3xvCVFPJMWkV7YJdO-ZDZWOwlDsYdqEJjjq0uadKzohvM1
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 56A4kwN7LQ9Sg-98Z2lpmlz7gz5ozvN6E4A-kQJ2U_Kk79iX5ziUZy503ETUA2-J>
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 Please log in via your web browser and then try again.
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534-5.7.14 Learn more at
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: 534 5.7.14 https://support.google.com/mail/answer/78754 ay38sm4917546wmb.3 - gsmtp
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Service.connect(Service.java:388)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Service.connect(Service.java:246)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Service.connect(Service.java:195)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Transport.send0(Transport.java:254)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at javax.mail.Transport.send(Transport.java:124)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)
Feb 11 11:20:53 ubuntu-s-1vcpu-1gb-fra1-01 java[737632]: ... 51 common frames omitted
我该怎么解决这个问题?有没有一种方法可以在没有SSL的情况下从运行在VPS上的Ktor服务器发送电子邮件。
我所做的事情:我尝试使用不同的端口:46587我启用了不太安全的应用程序
解决方案:使用https://accounts.google.com/DisplayUnlockCaptcha链接这可以解决身份验证问题。