我花了一小时时间,寻找正确的环境,利用 j诈发送电子邮件,最后是这样做的。 我之所以这样说,是因为我找不到一个全面的例子,通过电子邮件与javamailsender发送,希望这将有助于想做同样事情的人:
STEP:
将下列环境添加到邮件中。 财产:
mail.protocol=smtp
mail.host=smtp.gmail.com
mail.port=465
mail.smtp.socketFactory.port=465
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.debug=true
mail.smtp.starttls.required=true
mail.smtp.socketFactory.fallback=false
mail.from=XXX@gmail.com
mail.username=XXX@gmail.com
mail.password=my_password
之后,在您的邮件组别中,
@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfiguration {
@Value("${mail.protocol}")
private String protocol;
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private int port;
@Value("${mail.smtp.socketFactory.port}")
private int socketPort;
@Value("${mail.smtp.auth}")
private boolean auth;
@Value("${mail.smtp.starttls.enable}")
private boolean starttls;
@Value("${mail.smtp.starttls.required}")
private boolean startlls_required;
@Value("${mail.smtp.debug}")
private boolean debug;
@Value("${mail.smtp.socketFactory.fallback}")
private boolean fallback;
@Value("${mail.from}")
private String from;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.auth", auth);
mailProperties.put("mail.smtp.starttls.enable", starttls);
mailProperties.put("mail.smtp.starttls.required", startlls_required);
mailProperties.put("mail.smtp.socketFactory.port", socketPort);
mailProperties.put("mail.smtp.debug", debug);
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.put("mail.smtp.socketFactory.fallback", fallback);
mailSender.setJavaMailProperties(mailProperties);
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setProtocol(protocol);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
}
请注意,我的春天服务器是SSL,因此,我停靠465号港口。 SSL使用港口465。 如果你使用487条,你必须使用TLS。
STEP:
在连接之后,选择在上的上进入较安全的地方。
https://www.google.com/ establishings/security/lesssecureapps。
STEP:
如果您在《常设仲裁法院》上签字,则从业。 AVAST邮报盾与发送电子邮件发生冲突。 如果你不放弃,你将出现以下错误:
Servlet.service() for servlet [dispatcherServlet] in context with path
[] threw exception [Request processing failed; nested exception is
org.springframework.mail.MailSendException: Mail server connection
failed; nested exception is javax.mail.MessagingException: Exception
reading response; nested exception is:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target.