为什么我不能发送电子邮件?

我面临无法发送电子邮件的事实。我不明白原因。我使用 Spting Boot 2。该示例使用 JavaMailSender 类。但是你可以不用它的实现和在 application.properties 中指定的必要参数吗?


@Configuration

public class MainConfiguration {


    @Bean

    public JavaMailSender getJavaMailSender() {

        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        mailSender.setHost("smtp.yandex.ru");

        mailSender.setPort(465);


        mailSender.setUsername("test@yandex.ru");

        mailSender.setPassword("test122223");


        Properties props = mailSender.getJavaMailProperties();

        props.put("mail.transport.protocol", "smtp");

        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.starttls.enable", "true");

        props.put("mail.debug", "true");

        props.put("mail.smtp.socketFactory.port", "465");

        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

        //props.put("mail.smtp.timeout", 1000);


        return mailSender;

    }


    @Bean

    public SimpleMailMessage templateSimpleMessage() {

        SimpleMailMessage message = new SimpleMailMessage();

        message.setText("This is the test email template for your email:");

        return message;

    }


}

2 分钟后,出现错误:"javax.mail.MessagingException: Could not connect to SMTP host"


手掌心
浏览 134回答 2
2回答

侃侃无极

尝试添加spring.mail.properties.mail.smtp.ssl.enable=true或者props.put("mail.smtp.ssl.enable", "true");我不明白你为什么要使用配置类,如果你使用的是 spring boot 2,你可以将所有电子邮件配置放在你的 application.properties 文件中:spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.ssl.enable=truespring.mail.host=smtp.yandex.ruspring.mail.port=465spring.mail.username=test@yandex.ruspring.mail.password=test122223

江户川乱折腾

试试这个代码:Intent i = new Intent(Intent.ACTION_SEND);   i.setType("message/rfc822");   i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});   i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");   i.putExtra(Intent.EXTRA_TEXT   , "body of email");try{     startActivity(Intent.createChooser(i, "Send mail..."));   }catch (android.content.ActivityNotFoundException ex) {     Toast.makeText(MyActivity.this, "There are no email clients installed.",      Toast.LENGTH_SHORT).show();   }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java