我面临无法发送电子邮件的事实。我不明白原因。我使用Spting Boot2。该示例使用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"
答案 0 :(得分:1)
尝试添加
spring.mail.properties.mail.smtp.ssl.enable=true
或
props.put("mail.smtp.ssl.enable", "true");
我不明白为什么要使用Configuration类,如果您使用的是Spring Boot 2,则可以将所有电子邮件配置都放在application.properties文件中:
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.host=smtp.yandex.ru
spring.mail.port=465
spring.mail.username=test@yandex.ru
spring.mail.password=test122223
答案 1 :(得分:-3)
尝试以下代码:
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();
}