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

时间:2019-05-07 15:30:34

标签: java angular api spring-boot email

我能够发送带有SpringBoot和硬编码数据的电子邮件。

现在的问题是从Angular表单中获取数据,并从那里从中调用数据,但出现500错误。

有人可以知道问题出在哪里吗?

EmailService.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



@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

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

<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

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);
  }

}

2 个答案:

答案 0 :(得分:0)

问题出在CustomerController.java中的enviarEmail()方法。

该方法正在接收的对象“电子邮件”缺少@RequestBody批注。现在工作正常!

答案 1 :(得分:0)

我遇到类似的问题。但是我是否在控制器中具有注释@RequestBody。我与您的代码有所不同,但知道这是相同的想法。