0

My scenario is as follows: There are several entities, and I need to send custom designed email for each entity "by Id". For example, I am trying to send the email by this URL for "babs" entity: http://localhost:8081/api/v1/test/babss/sendmail/b1d0c331-35ac-430d-87ca-1718b06351c3

Sample entity for BaBS:

    import com.fasterxml.jackson.annotation.JsonBackReference;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.*;
    import javax.persistence.*;
    import java.util.UUID;

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    @Entity
    @Table(name = "babs")
    public class BaBs {

    @Id
    @Column(name = "id")
    private UUID id;

    @Column(name = "accountCode")
    private String accountCode;

    @Column(name = "accountName")
    private String accountName;

    @Column(name = "eMail")
    private String eMail;
 
    @ManyToOne
    @JsonBackReference
    @JoinColumn(name = "accountid")
    private Account account;

    }

email Sending Service

    import gokhan.mutabakatcore.models.BaBs;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.mail.MailException;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Component;

    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;

    @Component
    public class SentBaBsEmail {

    @Autowired
    private JavaMailSender sender;

    public HttpStatus mailBaBs(BaBs baBs) throws MailException {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo(baBs.getEMail());
        mail.setSubject("Trial Mail for Sending BaBs");
        mail.setText("Normally Details from Object ");
        sender.send(mail);
        System.out.println("eMail sent");
        return HttpStatus.GONE;
    }
    
    }

Corresponding part of BaBS Controller

    import gokhan.mutabakatcore.models.BaBs;
    import gokhan.mutabakatcore.services.BaBsService;
    import gokhan.mutabakatcore.utils.SentBaBsEmail;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;

    import javax.servlet.http.HttpServletRequest;
    import javax.validation.Valid;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.UUID;
    import java.util.function.Function;

    @RestController
    @RequestMapping("/api/v1/test/babss")
    public class BaBsController {
    private final BaBsService baBsService;

    public BaBsController(BaBsService baBsService) {
        this.baBsService = baBsService;
    }

    @Autowired
    SentBaBsEmail baBsEmail;

    @RequestMapping(value = "/sendmail/{Id}", method = { RequestMethod.GET, RequestMethod.POST })
    public ResponseEntity<?> eMailBaBsByCustomerId(@PathVariable("Id") UUID uuid){
        try{
            if (!baBsService.findById(uuid).equals(Optional.empty())){

                // Converts Optional Object to Normal Object
                BaBs baBs = (BaBs) toList(baBsService.findById(uuid));
                baBsEmail.mailBaBs(baBs);
                return new ResponseEntity(baBs ,HttpStatus.CREATED);
            }
            return new ResponseEntity<>("Unrecorded BaBS!", HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    }

Application.properties Mailing part

    #SMTP Email Properties
spring.mail.host=mail.xyz.com
spring.mail.port=587
spring.mail.username=info@xyz.com
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.starttls.enable=true
spring.mail.properties.mail.starttls.required=true

Relavant POM.xml dependencies

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

The error I got:

    This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Aug 06 08:39:31 EET 2022
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

I'll appreciate if you can show me the issue...

2
  • 1
    it seems that you have two request methods in your request mapping, "RequestMethod.GET, RequestMethod.POST"! you have to have one request method for each method. Aug 6 at 8:47
  • I found this on stackoverflow as a solution, which did not work in my case. I kept it incase somebody offers the same. Aug 6 at 8:55

0

Browse other questions tagged or ask your own question.