gpt4 book ai didi

java - 我如何在Spring Boot中使用Mailgun接收电子邮件

转载 作者:行者123 更新时间:2023-11-30 05:51:34 29 4
gpt4 key购买 nike

基本上我想使用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请求。就像我说的,任何方向都会受到高度赞赏。

最佳答案

我通过在Mailgun中添加一条路由-catchall()来解决该问题,该路由将消息重定向到我的gmail。

关于java - 我如何在Spring Boot中使用Mailgun接收电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53821550/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com