Comment envoyer un gmail automatique avec Spring boot et Angular?

Je voudrais créer une application qui peut envoyer des emails, vous pouvez remplir le formulaire avec l’adresse email et le nom du destinataire, une fois que vous cliquez sur soumettre, l’email devrait être envoyé. Je suis confronté à cette erreur :

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)

Voici mon formulaire angulaire : 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>

notification.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 envoyé avec succès');
          location.reload();
 
        },
        err => {
          alert("Une erreur s'est produite lors de l'envoi de l'email");
        });
        console.log("email envoyé");
  }
  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 (controller)

@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;

    }

Il semble que vous essayez d’envoyer le message avant de définir l’adresse électronique du destinataire. Il suffit de supprimer la ligne « sender.send(message) », celle qui précède l’instruction « try ».