【问题标题】:Sending Emails using Angular and Spring boot使用 Angular 和 Spring Boot 发送电子邮件
【发布时间】:2021-03-06 01:42:54
【问题描述】:

我想创建一个可以发送电子邮件的应用程序,您可以在表单中填写电子邮件地址和收件人姓名,点击提交后,应该发送电子邮件。 我正面临这个错误:

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)

这是我的角度形式: 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 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;
}

春季启动: 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;

    }

【问题讨论】:

  • 我可以在您的EmailSender 中看到两次sender.send(message);:一次是在您设置收件人地址之前,一次是之后。

标签: java angular spring-boot


【解决方案1】:

您似乎是在定义收件人的电子邮件之前尝试发送它,只需删除第一个“sender.send(message)”,即 try 语句之前的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多