似乎无法使用 jhipster 应用程序发送电子邮件
Can't seem to send email with jhipster application
我正在尝试制作一个简单的表单,在提交时发送并通过电子邮件发送到固定的电子邮件。
我正在使用 spring 并且我搜索了如何配置 application.yml 并且我正在使用似乎是我的 jhipster 应用程序生成的邮件发送方法。
我已经构建了我的 FE 服务以连接到后端:
sendForm(): Observable<any>{
return this.http.post(SERVER_API_URL + 'api/sendForm', "");
}
我已经构建了 onsubmit 方法来订阅上面的方法:
onSubmit() {
this.auth.sendForm().subscribe( data => {
console.log(data);
})
}
我已经对邮件资源进行了硬编码,只是为了模拟电子邮件以确保其正常工作:
@PostMapping("/sendForm")
public void sendForm() {
this.mailService.sendEmail("mymail@gmail.com","Header","texto",false,true);
}
发送邮件提交信息的 sendMail 方法是自动生成的,我相信它应该有效
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
isMultipart, isHtml, to, subject, content);
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name());
message.setTo(to);
message.setFrom(jHipsterProperties.getMail().getFrom());
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
log.debug("Sent email to User '{}'", to);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", to, e);
} else {
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
}
}
}
这是我的应用程序-dev.yml(我还在开发中)
spring:
profiles:
active: dev
mail:
host: smtp.gmail.com
port: 587
username: gmailuserid@gmail.com #Replace this field with your Gmail username.
password: ************ #Replace this field with your Gmail password.
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
starttls.enable: true
ssl.trust: smtp.gmail.com
我遇到的错误如下:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localh
ost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeo
ut -1;
我只希望收到一封包含我使用过的模拟的邮件,但我似乎无法让它正常工作。
我希望我没有把 post 写得太长,并且一切都得到了很好的解释。
提前感谢任何愿意提供帮助的人
显然 properties-prod.yml 没有加载到服务器。我必须创建一个包含所有配置的配置文件才能正常工作
我正在尝试制作一个简单的表单,在提交时发送并通过电子邮件发送到固定的电子邮件。
我正在使用 spring 并且我搜索了如何配置 application.yml 并且我正在使用似乎是我的 jhipster 应用程序生成的邮件发送方法。
我已经构建了我的 FE 服务以连接到后端:
sendForm(): Observable<any>{
return this.http.post(SERVER_API_URL + 'api/sendForm', "");
}
我已经构建了 onsubmit 方法来订阅上面的方法:
onSubmit() {
this.auth.sendForm().subscribe( data => {
console.log(data);
})
}
我已经对邮件资源进行了硬编码,只是为了模拟电子邮件以确保其正常工作:
@PostMapping("/sendForm")
public void sendForm() {
this.mailService.sendEmail("mymail@gmail.com","Header","texto",false,true);
}
发送邮件提交信息的 sendMail 方法是自动生成的,我相信它应该有效
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
isMultipart, isHtml, to, subject, content);
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name());
message.setTo(to);
message.setFrom(jHipsterProperties.getMail().getFrom());
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
log.debug("Sent email to User '{}'", to);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", to, e);
} else {
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
}
}
}
这是我的应用程序-dev.yml(我还在开发中)
spring:
profiles:
active: dev
mail:
host: smtp.gmail.com
port: 587
username: gmailuserid@gmail.com #Replace this field with your Gmail username.
password: ************ #Replace this field with your Gmail password.
protocol: smtp
tls: true
properties.mail.smtp:
auth: true
starttls.enable: true
ssl.trust: smtp.gmail.com
我遇到的错误如下:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localh
ost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeo
ut -1;
我只希望收到一封包含我使用过的模拟的邮件,但我似乎无法让它正常工作。
我希望我没有把 post 写得太长,并且一切都得到了很好的解释。
提前感谢任何愿意提供帮助的人
显然 properties-prod.yml 没有加载到服务器。我必须创建一个包含所有配置的配置文件才能正常工作