邮件服务器连接失败

时间:2019-04-03 07:35:13

标签: java spring model-view-controller javamail mail-server

我正在尝试使用JavaMailSender发送邮件,我觉得一切都很好,但是每当我碰到JavaMailSender时,我都会遇到错误。我在下面提到了细节。

这是完整的错误

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Error in getting welcome msg. Failed messages: javax.mail.MessagingException: Error in getting welcome msg; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Error in getting welcome msg
    at org.apache.geronimo.javamail.transport.smtp.SMTPConnection.protocolConnect(SMTPConnection.java:145)
    at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:165)
    at javax.mail.Service.connect(Service.java:274)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:397)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:346)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:363)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:351)
    at com.tarento.graptive.notification.service.EmailService.prepareAndSend(EmailService.java:175)
    at com.tarento.graptive.notification.service.EmailService$$FastClassBySpringCGLIB$$db5de937.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:97)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    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)

这是我在SendMail.Java中的代码


mailService.prepareAndSend("toanotherperson@gmail.com", "sample message");

这是我在EmailService.java中的代码

public void prepareAndSend(String recipient, String message) {

          MimeMessagePreparator preperator = new MimeMessagePreparator() {
              public void prepare(MimeMessage mimeMessage) throws Exception {
                  MimeMessageHelper message =new MimeMessageHelper(mimeMessage, true, "UTF-8");

                  message.setFrom("mymail@gmail.com");
                  message.setTo(recipient);
                  message.setSubject("test subject");
                  message.setText("test body");
              }
          };
        try {
            mailSender.send(preperator);
        } catch (Exception e) {
             e.printStackTrace();
        }
    }

这是email.xml中的“我的配置”

 <bean id="mailSend" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${email_host}"/>
        <property name="port" value="${email_port}"/>
        <property name="username" value="${email_username}"/>
        <property name="password" value="${email_password}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${email_mail_smtp_auth}</prop>
                <prop key="mail.smtp.starttls.enable">${email_mail_smtp_starttls_enable}</prop> 
            </props>
        </property>
    </bean>

这是我在project.properties中的属性

#Email properties
email_host = smtp.gmail.com
email_port = 587
email_username = mymail@gmail.com  
email_password = mymail@123
email_mail_transport_protocol = smtp
email_mail_smtp_auth = true
email_mail_smtp_starttls_enable = true
email_from =mymail@gmail.com

0 个答案:

没有答案