我在Sring Boot应用程序中使用JavaMailSender,并且如果例如密码之类的属性错误以防止登录到邮件网关,则它将无法自动装配Bean,并且无法加载应用程序上下文。
Spring邮件属性:
void handle_kext(const void* key, const void* value, void* context)
{
// OSBundlePath - CFString (this is merely a hint stored in the kernel; the kext is not guaranteed to be at this path)
CFStringRef bundle_path_key = CFStringCreateWithCString(kCFAllocatorDefault, "OSBundlePath", kCFStringEncodingUTF8);
const char* bundle_id_cstring = CFStringGetCStringPtr(key, kCFStringEncodingUTF8);
const char* bundle_path_cstring = CFStringGetCStringPtr(CFDictionaryGetValue(value, bundle_path_key, kCFStringEncodingUTF8);
// #1 Approach: Compare your 'I-Want-This-BundleID' with loaded kext path
// #2 Approach: Compare your 'I-Want-This-BundlePath' with loaded kext ID
}
void some_func()
{
CFDictionaryRef loaded_kexts = KextManagerCopyLoadedKextInfo(NULL, NULL);
CFDictionaryApplyFunction(loaded_kexts, handle_kext, NULL);
}
属性由属性读取器加载:
spring.mail.host=smtp.gmail.com
spring.mail.port=465
spring.mail.protocol=smtp
spring.mail.username=xxx@gmail.com
spring.mail.password=xxx
spring.mail.from=xxx@gmail.com
如何使用它,以便使用错误的邮件密码不会阻止应用程序启动?
依赖项:
public abstract class AbstractUserProperties implements ApplicationProperties {
@Value("${" + PropertyNames.CONFIG_AUTH_TOKEN_PRIVATE_KEY + "}")
private String authenticationTokenPrivateKey;
@Value("${" + PropertyNames.CONFIG_MAIL_HOST + "}")
private String host;
@Value("${" + PropertyNames.CONFIG_MAIL_PORT + "}")
private String port;
@Value("${" + PropertyNames.CONFIG_MAIL_PROTOCOL + "}")
private String protocol;
@Value("${" + PropertyNames.CONFIG_MAIL_USERNAME + "}")
private String username;
@Value("${" + PropertyNames.CONFIG_MAIL_PASSWORD + "}")
private String password;
@Value("${" + PropertyNames.CONFIG_MAIL_FROM + "}")
private String mailFrom;
@Value("${" + PropertyNames.CONFIG_MAIL_TEST_CONNECTION + "}")
private boolean mailTestConnection;
@Value("${" + PropertyNames.CONFIG_MAILING_ENABLED + "}")
private boolean mailingEnabled;
public String getAuthenticationTokenPrivateKey() {
return authenticationTokenPrivateKey;
}
@Override
public String getHost() {
return host;
}
@Override
public String getPort() {
return port;
}
@Override
public String getProtocol() {
return protocol;
}
@Override
public String getUsername() {
return username;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getMailFrom() {
return mailFrom;
}
@Override
public boolean getMailTestConnection() {
return mailTestConnection;
}
@Override
public boolean getMailingEnabled() {
return mailingEnabled;
}
}
我在<version>2.0.3.RELEASE</version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
上。
这是应用程序的邮件配置:
Spring Boot 2.0.3
更新:
我已经将@Configuration
public class MailConfig {
@Autowired
private ApplicationProperties applicationProperties;
@Bean
public JavaMailSender javaMailSender() {
String host = applicationProperties.getHost();
String port = applicationProperties.getPort();
String protocol = applicationProperties.getProtocol();
String username = applicationProperties.getUsername();
String password = applicationProperties.getPassword();
if (!password.isEmpty() && !username.isEmpty() && !protocol.isEmpty() && !port.isEmpty() && !host.isEmpty()) {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(host);
javaMailSender.setPort(Integer.parseInt(port));
javaMailSender.setProtocol(protocol);
javaMailSender.setUsername(username);
javaMailSender.setPassword(password);
javaMailSender.setJavaMailProperties(getMailProperties());
return javaMailSender;
} else {
return null;
}
}
@Bean
public SimpleMailMessage simpleMailMessage() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(applicationProperties.getMailFrom());
return simpleMailMessage;
}
private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.smtp.quitwait", "false");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.debug", "true");
return properties;
}
}
属性放置在spring.mail.test-connection=false
文件中,并且控制台日志显示它已被复制:
application.properties
我还删除了[INFO] Copying 1 resource
[DEBUG] Copying file application.properties
类,以便默认使用Spring提供的类。
错误消息仍然相同。
附带说明一下,我想知道如何在自己的bean配置中使用该属性,而不必使用默认的Spring bean。
更新:
我可以在配置bean中添加MailConfig
属性,如下所示:
mail.test-connection
现在,即使没有提供密码,也不会检查连接,应用程序上下文也可以正常加载。
答案 0 :(得分:1)
您可以将以下行添加到application.properties
文件中,以便在应用程序启动时不测试邮件连接:
spring.mail.test-connection=false
使用此配置,如果您是第一次尝试发送邮件,例如您的密码不正确会引发异常。
但是我不确定为什么您要这么做:D
更新:如果您想了解Spring Boot的工作方式,请查看XYZAutoConfiguration
的{{1}}类,他们使用以下代码测试连接:
MailSender