无法发送电子邮件 - 尝试连接到主机" smtp.mail.gmail.com",端口587,isSSL false

时间:2017-12-24 11:46:59

标签: email spring-mvc

我试图在spring mvc 4中使用javamail使用gmail发送电子邮件,但在那里遇到了一些错误。错误说......

  

信息:收据?= nishadhungana41@gmail.com,主题?=用户验证,消息?=您的引脚是158046

     

信息:DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

     

信息:DEBUG SMTP:useEhlo true,useAuth true

     

信息:DEBUG SMTP:尝试连接到主机" smtp.mail.gmail.com",端口587,isSSL false

     

严重:PWC6117:文件" null"找不到

这是我的弹簧配置

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.mail.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="nishandhungana41@gmail.com" />
    <property name="password" value="*******" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.debug">true</prop>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            <prop key="mail.smtp.socketFactory.port">465</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>

这就是我制作控制器的方式

   @Controller
public class EmailController {

    private String emailToRecipient = "";
    private String emailSubject = "";
    private String emailMessage = "";
    private final String emailFromRecipient = "nishandhungana41@gmail.com";

    @Autowired
    private JavaMailSender mailSenderObj;

    @Autowired
    GeneratePIN generatePin;

    // This Method Is Used To Prepare The Email Message And Send It To The Client
    @RequestMapping(value = "/create-account", method = RequestMethod.POST)
    public @ResponseBody String sendEmailToClient() {

        // Reading Email Form Input Parameters      
        emailSubject = "User Verification";
        emailMessage = "Your pin is "+generatePin.pin();
        emailToRecipient = "nishadhungana41@gmail.com";

        // Logging The Email Form Parameters For Debugging Purpose
        System.out.println("\nReceipient?= " + emailToRecipient + ", Subject?= " + emailSubject + ", Message?= " + emailMessage + "\n");

        mailSenderObj.send(new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {

                MimeMessageHelper mimeMsgHelperObj = new MimeMessageHelper(mimeMessage, true, "UTF-8");
                mimeMsgHelperObj.setTo(emailToRecipient);
                mimeMsgHelperObj.setFrom(emailFromRecipient);
                mimeMsgHelperObj.setText(emailMessage);
                mimeMsgHelperObj.setSubject(emailSubject);

            }
        });
        System.out.println("\nMessage Send Successfully.... Hurrey!\n");

        return "Thank You! Your Email Has Been Sent!";
    }
}

1 个答案:

答案 0 :(得分:1)

更新弹簧配置后问题解决如下:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="587" />
    <property name="username" value="nishandhungana41@gmail.com" />
    <property name="password" value="********" />

    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
        </props>
    </property>
</bean>