[英]Sending Emails using Angular and Spring boot
我想创建一个可以发送电子邮件的应用程序,您可以使用 email 地址和收件人姓名填写表格,单击提交后,应发送 email。 我面临这个错误:
Failed message 1:
javax.mail.SendFailedException: No recipient addresses
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1250)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:465)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
at com.example.demo.controllers.EmailSender.sendMail(EmailSender.java:70)
这是我的 angular 表格:notification.component.html:
<form id="contact-form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Name *</label>
<input [(ngModel)]="emailNotification.name" id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your full name *" required="required" data-error="name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_need">Please specify your need *</label>
<select id="form_need" name="need" class="form-control" required="required" data-error="Please specify your need.">
<option value="Email">Email</option>
<option value="Whatsapp notification">Whatsapp notification</option>
<option value="SMS notification">SMS notification</option>
</select>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-6" >
<div class="form-group">
<label for="form_email">Email Address *</label>
<input [(ngModel)]="emailNotification.email" id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message" (click)="onSubmit()">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required.
</div>
</div>
</div>
通知.component.ts:
export class NotificationsComponent implements OnInit {
emailNotification: Infos =
{
name: '',
email: '',
};
selectedValue: any;
constructor(private https: HttpClient) { }
onSubmit(){
this.https.post<Infos>('http://localhost:8080/sendEmail/getdetails', this.emailNotification).subscribe(
res => {
this.emailNotification = res;
console.log(this.emailNotification);
alert('Email Sent successfully');
location.reload();
},
err => {
alert('An error has occured while sending email');
});
console.log("email sent");
}
ngOnInit(): void {
}
}
interface Infos{
name:string;
email:string;
}
Springboot:EmailNotification.java:
package com.example.demo.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class EmailNotification {
@Id
@GeneratedValue
private long id;
private String name;
private String subject;
private String emailAddress;
private String message;
public EmailNotification(long id, String emailAddress, String body, String message, String subject, String msg) {
super();
this.subject = subject;
this.message = msg;
this.id = id;
this.emailAddress = emailAddress;
this.message = message;
}
public EmailNotification() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
EmailSender.java(控制器)
@RequestMapping("/sendEmail")
@Controller
public class EmailSender {
@Autowired
private JavaMailSender sender;
@Autowired
SpringTemplateEngine templateEngine;
@RequestMapping("/getdetails")
public @ResponseBody EmailNotification sendMail(@RequestBody EmailNotification notification) throws Exception {
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
StandardCharsets.UTF_8.name());
Map<String, Object> model = new HashMap<String, Object>();
model.put("name", notification.getName());
Context context = new Context();
context.setVariables(model);
String html = templateEngine.process("email-template", context);
sender.send(message);
try {
helper.setTo(notification.getEmailAddress());
helper.setText(html, true);
helper.setSubject("Notification!");
} catch (MessagingException e) {
e.printStackTrace();
}
sender.send(message);
return notification;
}
似乎您正在尝试在定义收件人的 email 之前发送它,只需删除第一个“sender.send(message)”,即 try 语句之前的那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.