当前位置: 首页 > 知识库问答 >
问题:

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

贺玉石
2023-03-14

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

信息:接收人?=nishadhungana41@gmail.com,subject?=用户验证,消息?=您的pin是158046

<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个答案

高勇
2023-03-14

更新spring配置后,问题得到了解决,如下所示:

<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>
 类似资料: