【问题标题】:correct configuration my smtp settings in jhipster project在 jhipster 项目中正确配置我的 smtp 设置
【发布时间】:2015-12-22 20:21:50
【问题描述】:

我的 jhipster 应用程序配置存在一些问题。这是我的配置文件的一部分:

mail:
    host: smtp.mail.yahoo.com
    port: 465
    username: My_smtp_login
    password: my_smtp_password
    protocol: smtp
    tls: true
    auth: true
    from: mymail@yahoo.pl
messages:
    cache-seconds: 1
thymeleaf:
    cache: false

还有我的日志:

2015-12-22 20:47:09.680 DEBUG 13428 --- [hero-Executor-2] com.sun.mail.smtp                        : useEhlo true, useAuth false
2015-12-22 20:47:09.680 DEBUG 13428 --- [hero-Executor-2] com.sun.mail.smtp                        : trying to connect to host "smtp.mail.yahoo.com", port 465, isSSL false
2015-12-22 20:48:09.832 DEBUG 13428 --- [hero-Executor-2] com.sun.mail.smtp                        : EOF: [EOF]
2015-12-22 20:48:09.833 DEBUG 13428 --- [hero-Executor-2] com.sun.mail.smtp                        : could not connect to host "smtp.mail.yahoo.com", port: 465, response: -1
2015-12-22 20:48:09.834  WARN 13428 --- [hero-Executor-2] pl.mailinghero.service.MailService       : E-mail could not be sent to user 'xxxx@yandex.com', exception is: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 465, response: -1
2015-12-22 20:48:09.834 DEBUG 13428 --- [hero-Executor-2] p.mailinghero.aop.logging.LoggingAspect  : Exit: pl.mailinghero.service.MailService.sendActivationEmail() with result = null

这不仅是 yahoo 的问题,我检查过 AOL、Gmail 和 Yandex,但 smtp 也存在同样的问题。 smtp配置问题出在哪里?

--编辑:

这是我的新配置:

spring:
    profiles:
        active: dev
    devtools:
        restart:
            enabled: true
        livereload:
            enabled: false # we use Grunt + BrowserSync for livereload
    datasource:
        driver-class-name: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
        url: jdbc:mysql://localhost:3306/DemoApp?useUnicode=true&characterEncoding=utf8
        name:
        username: root
        password: password123
    jpa:
        database-platform: org.hibernate.dialect.MySQLInnoDBDialect
        database: MYSQL
        show_sql: true
        properties:
            hibernate.cache.use_second_level_cache: true
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
    data:
        elasticsearch:
            cluster-name:
            cluster-nodes:
            properties:
                path:
                  logs: target/elasticsearch/log
                  data: target/elasticsearch/data
    mail:
        host: smtp.mail.yahoo.com
        port: 587
        username: login@yahoo.com
        password: password
        protocol: smtp
        tls: true
        properties.mail.smtp:
             auth: true
             starttls.enable: true
             ssl.trust: smtp.mail.yahoo.com
        from: login@yahoo.com
    messages:
        cache-seconds: 1
    thymeleaf:
        cache: false

liquibase:
    contexts: dev

server:
    port: 8080

# ===================================================================
# JHipster specific properties
# ===================================================================

jhipster:
    datasource: # JHipster-specific configuration, in addition to the standard spring.datasource properties
        cachePrepStmts: true
        prepStmtCacheSize: 250
        prepStmtCacheSqlLimit: 2048
        useServerPrepStmts: true
    cache: # Hibernate 2nd level cache, used by CacheConfiguration
        timeToLiveSeconds: 3600
        ehcache:
            maxBytesLocalHeap: 16M
    mail: # specific JHipster mail property, for standard properties see MailProperties
        from: DemoApp@localhost
    metrics: # DropWizard Metrics configuration, used by MetricsConfiguration
        jmx.enabled: true
        spark:
            enabled: false
            host: localhost
            port: 9999
        graphite:
            enabled: false
            host: localhost
            port: 2003
            prefix: DemoApp

但问题依然存在:

<html>
    <head>
        <title>DemoApp account activation</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p>Dear dkj</p>
        <p>Your DemoApp account has been created, please click on the URL below to activate it:</p>
        <p>
            <a href="http://localhost:8080/#/activate?key=95189449636004891182">dkj</a>
        </p>
        <p>
            <span>Regards,</span>
            <br />
            <em>DemoApp Team.</em>
        </p>
    </body>
</html>

2015-12-25 18:23:31.119 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : useEhlo true, useAuth false
2015-12-25 18:23:31.119 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : trying to connect to host "smtp.mail.yahoo.com", port 587, isSSL false
2015-12-25 18:23:31.581 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : connected to host "smtp.mail.yahoo.com", port: 587

2015-12-25 18:23:31.740 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "PIPELINING", arg ""
2015-12-25 18:23:31.740 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "SIZE", arg "41697280"
2015-12-25 18:23:31.740 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "8", arg "BITMIME"
2015-12-25 18:23:31.740 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "STARTTLS", arg ""
2015-12-25 18:23:32.436 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "PIPELINING", arg ""
2015-12-25 18:23:32.436 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "SIZE", arg "41697280"
2015-12-25 18:23:32.436 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "8", arg "BITMIME"
2015-12-25 18:23:32.436 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2 XYMCOOKIE"
2015-12-25 18:23:32.436 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
2015-12-25 18:23:33.470 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : use8bit false
2015-12-25 18:23:33.703 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : got response code 501, with response: 501 Syntax error in arguments

2015-12-25 18:23:33.945 DEBUG 5677 --- [-app-Executor-1] com.sun.mail.smtp                        : MessagingException while sending

com.sun.mail.smtp.SMTPSendFailedException: 501 Syntax error in arguments

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2203) ~[javax.mail-1.5.4.jar:1.5.4]
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1694) ~[javax.mail-1.5.4.jar:1.5.4]
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1194) ~[javax.mail-1.5.4.jar:1.5.4]
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448) [spring-context-support-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) [spring-context-support-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340) [spring-context-support-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at net.demo.service.MailService.sendEmail(MailService.java:65) [classes/:na]
    at net.demo.service.MailService.sendActivationEmail(MailService.java:81) [classes/:na]
    at net.demo.service.MailService$$FastClassBySpringCGLIB$$bf501f3f.invoke(<generated>) [classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at net.demo.aop.logging.LoggingAspect.logAround(LoggingAspect.java:51) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108) [spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_65]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_65]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_65]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65]
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax error in arguments

    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1703) ~[javax.mail-1.5.4.jar:1.5.4]
    ... 29 common frames omitted

2015-12-25 18:23:34.189  WARN 5677 --- [-app-Executor-1] net.demo.service.MailService             : E-mail could not be sent to user 'login@gmail.com', exception is: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 501 Syntax error in arguments
;
  nested exception is:
    com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax error in arguments

2015-12-25 18:23:34.189 DEBUG 5677 --- [-app-Executor-1] net.demo.aop.logging.LoggingAspect       : Exit: net.demo.service.MailService.sendActivationEmail() with result = null

我找到了删除的信息

来自:mymail@yahoo.pl 应该有帮助,但在我的情况下它不起作用

【问题讨论】:

    标签: java spring email jakarta-mail jhipster


    【解决方案1】:

    我和你有同样的错误。我的解决方案是将from: mymail@yahoo.pl(必须与雅虎邮件用户名相同)放到该部分:

    jhipster: mail: from: mymail@yahoo.pl

    也可以在 application-prod.yml 或 application-dev.yml 文件中找到,但在以下几行:

    spring:
           mail:
    

    部分。 所以我的整个解决方案如下(使用您的示例凭据):

    spring:
            ...
              mail:
                   host: smtp.mail.yahoo.com
                   port: 587
                   username: mymail@yahoo.pl
                   password: my_smtp_password
                   protocol: smtp
                   tls: true
              properties.mail.smtp:
                   auth: true
                   starttls.enable: true
                   ssl.trust: smtp.mail.yahoo.com
    

    和:

    jhipster:
             ...
             mail: 
                  from: mymail@yahoo.pl
    

    【讨论】:

      【解决方案2】:

      您是否检查过您是否可以从 JHipster 外部连接到该 SMTP 服务器,例如使用 telnet?也许你被防火墙阻止了。

      您还应该为其他属性设置spring.mail.properties.mail.smtp,如下所示。

      spring:
          mail:
              host: smtp.mail.yahoo.com
              port: 587
              username: My_smtp_login
              password: my_smtp_password
              protocol: smtp
              tls: true
              properties.mail.smtp:
                   auth: true
                   starttls.enable: true
                   ssl.trust: smtp.mail.yahoo.com
              from: mymail@yahoo.pl
      

      【讨论】:

      • 谢谢 :) 向前迈出了一大步,似乎应用程序可以连接到 smtp,但仍然有问题:>2015-12-24 10:06:51.146 WARN 7709 --- [ -app-Executor-2] >net.ddemo.service.MailService :电子邮件无法发送到 >user 'xxx@xxx.pl',例外是:失败的消息:>com.sun.mail.smtp.SMTPSendFailedException : >arguments > 中的 501 语法错误; >嵌套异常是: > com.sun.mail.smtp.SMTPSenderFailedException: 501 Syntax >参数错误我也尝试关注此链接:goo.gl/lkfg6a 但它不起作用
      【解决方案3】:

      我发现的大多数示例都使用来自以下位置的经典配置:https://jhipster.github.io/tips/011_tip_configuring_email_in_jhipster.html

      我遇到了与您尝试使用 Exchange Server 连接时相同的错误。 修复方法是将 SMTP 属性添加到 spring:mail,以便在 Session 中加载这些属性

      所以我在配置文件 application-dev.yml 中添加了我需要的 SMTP 属性 (见:https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

      spring:
          mail:
              host: 10.10.10.10
              port: 25
              username: 
              password: 
              properties.mail.smtp:
                  host: 10.10.10.10
                  port: 25
                  auth: false
                  starttls.enable: false
                  connectiontimeout: 180000
                  timeout: 180000
      
      jhipster:
          mail: 
              from: yourproject@something.com
      

      为了检查 Session,我在 MailService 类中添加了一些调试日志。

      类:your.pack.service.MailService.java

      @Inject
      private MailProperties mailProperties;
      
      @Async
      public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
          log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
              isMultipart, isHtml, to, subject, content);
      
          // Prepare message using a Spring helper
          MimeMessage mimeMessage = javaMailSender.createMimeMessage();
          try {
              MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
              message.setTo(to);
              message.setFrom(jHipsterProperties.getMail().getFrom());
              message.setSubject(subject);
              message.setText(content, isHtml);
      
              // --- Debug Code
              log.debug("----- spring: mail: -----");
              log.debug("spring mail: host={}", mailProperties.getHost());
              log.debug("spring mail: port={}", mailProperties.getPort());
              log.debug("spring mail: username={}", mailProperties.getUsername());
              log.debug("spring mail: password={}", mailProperties.getPassword());
              log.debug("spring mail: protocol={}", mailProperties.getProtocol());
      
              log.debug("----- spring: mail: properties.mail.smtp -----");
              for (Entry<String, String> entry : mailProperties.getProperties().entrySet()) {
                  log.debug("properties.mail.smtp: {}={}", entry.getKey(), entry.getValue());
              }
      
              log.debug("----- session object -----");
              for (Entry<Object, Object> entry : message.getMimeMessage().getSession().getProperties().entrySet()) {
                  log.debug("session: {}={}", entry.getKey(), entry.getValue());
              }
              // --- Debug Code
      
              javaMailSender.send(mimeMessage);
              log.debug("Sent e-mail to User '{}'", to);
          } catch (Exception e) {
              log.warn("E-mail could not be sent to user '{}', exception is: {}", to, e.getMessage());
          }
      }
      

      结果,所有属性都加载到了会话中

      your.pack.service.MailService    : Sending activation e-mail to 'thenewuser@server.com'
      your.pack.service.MailService    : Send e-mail[multipart 'false' and html 'true'] to 'thenewuser@server.com' with subject 'YourProject account activation' and content=<!DOCTYPE html>
      ...
      ...
      your.pack.service.MailService    : ----- spring: mail: -----
      your.pack.service.MailService    : spring mail: host=10.10.10.10
      your.pack.service.MailService    : spring mail: port=25
      your.pack.service.MailService    : spring mail: username=
      your.pack.service.MailService    : spring mail: password=
      your.pack.service.MailService    : spring mail: protocol=smtp
      ...
      your.pack.service.MailService    : ----- spring: mail: properties.mail.smtp -----
      your.pack.service.MailService    : properties.mail.smtp: mail.smtp.port=25
      your.pack.service.MailService    : properties.mail.smtp: mail.smtp.auth=false
      your.pack.service.MailService    : properties.mail.smtp: mail.smtp.starttls.enable=false
      your.pack.service.MailService    : properties.mail.smtp: mail.smtp.timeout=180000
      your.pack.service.MailService    : properties.mail.smtp: mail.smtp.host=10.10.10.10
      your.pack.service.MailService    : properties.mail.smtp: mail.smtp.connectiontimeout=180000
      ...
      your.pack.service.MailService    : ----- session object -----
      your.pack.service.MailService    : session: mail.smtp.starttls.enable=false
      your.pack.service.MailService    : session: mail.smtp.port=25
      your.pack.service.MailService    : session: mail.smtp.host=10.10.10.10
      your.pack.service.MailService    : session: mail.smtp.auth=false
      your.pack.service.MailService    : session: mail.smtp.timeout=180000
      your.pack.service.MailService    : session: mail.smtp.connectiontimeout=180000
      

      至少对我来说,它有效!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 2017-02-05
        相关资源
        最近更新 更多