简体   繁体   English

使用 Angular 7 + SpringBoot 发送电子邮件

[英]Sending emails with Angular 7 + SpringBoot

I am able to send emails with SpringBoot and hardcoded data.我能够使用 SpringBoot 和硬编码数据发送电子邮件。

Now the problem is to get the data from my Angular form, and call the API with the data from there, but I'm getting error 500.现在的问题是从我的 Angular 表单中获取数据,并使用来自那里的数据调用 API,但我收到错误 500。

Can someone know where is the problem?有人可以知道问题出在哪里吗?

EmailService.java电子邮件服务.java



@Service
public class EmailService {

    private JavaMailSender javaMailSender;

    @Autowired
    public EmailService(JavaMailSender javaMailSender){
        this.javaMailSender = javaMailSender;
    }

    public void sendEmail(Email email) throws MailException {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo("marioluarca7@gmail.com");
        mail.setFrom(email.getEmail());
        mail.setSubject("Contacto: "+email.getNombre());
        mail.setText(email.getMensaje());

        javaMailSender.send(mail);
    }

}

CustomerController.java客户控制器.java



@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class CustomerController {



  @Autowired
  private EmailService emailService;

 //some other code

  @PostMapping(value = "/email")
  public ResponseEntity<Email> enviarEmail( Email email){
    try {
      emailService.sendEmail(email);
      return new ResponseEntity<>(email,  HttpStatus.OK);
    } catch( MailException e){
      return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }


  }

}

contacto.component.ts contacto.component.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Email } from 'src/app/models/email';
import { EmailService } from 'src/app/services/email.service';
import { NgForm } from '@angular/forms';


@Component({
  selector: 'app-contacto',
  templateUrl: './contacto.component.html',
  styleUrls: ['./contacto.component.css']
})
export class ContactoComponent implements OnInit {

  ngOnInit() {
  }

  mail :Email = new Email();

  constructor(private http: HttpClient, private emailService :EmailService) { }

  private enviarEmail() {
    this.emailService.enviarEmail(this.mail)
      .subscribe(data => console.log(data));
  }

  private onSubmit() {
    this.enviarEmail();
  }


}

contacto.component.html联系方式.component.html

<form (ngSubmit)="onSubmit()" #contacto>
              <div class="form-group">
                <label for="nombre">Nombre</label>
                <input type="text" class="form-control" id="name" required [(ngModel)]="mail.nombre" name="nombre">
              </div>

              <div class="form-group">
                <label for="email">Email</label>
                <input type="text" class="form-control" id="email" required [(ngModel)]="mail.email" name="email">
              </div>

              <div class="form-group">
                <label for="mensaje">Mensaje</label>
                <input type="text" class="form-control" id="mensaje" required [(ngModel)]="mail.mensaje" name="mensaje">
              </div>

              <button type="submit" class="btn btn-success">Contactar</button>
            </form>

email.service.ts电子邮件.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Email } from '../models/email';

@Injectable({
  providedIn: 'root'
})
export class EmailService {

  constructor(private http :HttpClient) { }

  private baseUrl = 'http://localhost:8080/api/email';

  enviarEmail(email :Email): Observable<any> {
    return this.http.post(`${this.baseUrl}`, email);
  }

}

The problem was at the method enviarEmail() in CustomerController.java.问题出在 CustomerController.java 中的方法 enviarEmail() 上。

The object "Email" the method was receiving was missing the @RequestBody annotation.该方法接收的对象“电子邮件”缺少 @RequestBody 注释。 Working well now!现在运作良好!

I am with a problem similar.我有一个类似的问题。 But I if have the annotation @RequestBody in the controller.但是我如果在控制器中有 @RequestBody 注释。 I've difference with your code but know that is the same idea.我与您的代码有所不同,但知道这是相同的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM