基本上,我想使用REST-POST请求接收电子邮件。任何方向将不胜感激。我正在尝试Mailgun服务以使其正常工作。到目前为止,我得到了什么:
Email.java:
public class Email {
@NotNull
private String name;
@NotNull
private String email;
@NotNull
private String subject;
@NotNull
private String url;
@NotNull
@Size(min = 10)
private String messageText;
public String getName() {
return name;
}
public void setName(@NotNull String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(@NotNull String email) {
this.email = email;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
application.properties:
#TO DO: Set the mailgun props here
spring.mail.host=smtp.mailgun.org
spring.mail.port=2525
spring.mail.username=postmaster@sandbox**************************.mailgun.org
spring.mail.password=************************************
EmailConfig.java
@Component
public class EmailConfig {
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
EmailController.java
@RestController
public class EmailController {
private EmailConfig emailConfig;
// this constructor performs autowired DIa
public EmailController(EmailConfig emailConfig) {
this.emailConfig = emailConfig;
}
@PostMapping("/sendEmail")
public void sendFeedback() {
// create mail sender
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(this.emailConfig.getHost());
mailSender.setPort(this.emailConfig.getPort());
mailSender.setUsername(this.emailConfig.getUsername());
mailSender.setPassword(this.emailConfig.getPassword());
// create an email instance
SimpleMailMessage mailMessage = new SimpleMailMessage();
// email.getEmail()
mailMessage.setFrom("admin@sportapp.bg");
mailMessage.setTo("zirow000@gmail.com");
// email.getSubject()
mailMessage.setSubject("Test subject");
// email.getMessageText()
mailMessage.setText("Test text !!!");
// send mail
mailSender.send(mailMessage);
}
}
我将在AJAX中使用此POST请求来获取用户输入(名称,电子邮件,主题和文本)并通过我的gmail接收。至少那是我想做的(我希望有可能)。我已将其部署在Heroku上,并与Postman进行了空的POST请求。就像我说的,任何方向都将受到高度赞赏。
答案 0 :(得分:1)
我通过在Mailgun中添加一条路由-catchall()解决了该问题,该路由将邮件重定向到我的gmail。