+ use web controller

master
io42630 1 year ago
parent eee0ae52a8
commit 1c0330a931

@ -2,7 +2,7 @@
## Goals ## Goals
* connect to mailbox * connect to mailbox (OK)
* connect to ollama * connect to ollama
* check mail * check mail (OK)
* move to burn folder * move to burn folder

@ -22,6 +22,14 @@
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>

@ -1,7 +0,0 @@
#!/bin/bash
docker push io42630/burns-mail:0.1

@ -1,9 +1,7 @@
package com.olexyn.burnsmail; package com.olexyn.burnsmail;
import com.olexyn.burnsmail.mail.EmailService;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication @SpringBootApplication
@ -11,24 +9,8 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy(proxyTargetClass = true) @EnableAspectJAutoProxy(proxyTargetClass = true)
public class BurnsMailApplication { public class BurnsMailApplication {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BurnsMailApplication.class, args); SpringApplication.run(BurnsMailApplication.class, args);
Thread.sleep(10000L);
// Retrieve the EmailService bean
EmailService emailService = context.getBean(EmailService.class);
// Call the readEmails method
try {
emailService.readEmails();
} catch (Exception e) {
e.printStackTrace();
// Handle exceptions as needed
}
// Close the application context
context.close();
} }
} }

@ -18,8 +18,8 @@ import java.util.Properties;
public class EmailService { public class EmailService {
public void readEmails() throws MessagingException, IOException { private Store connect() throws MessagingException {
Properties properties = new Properties(); var properties = new Properties();
properties.setProperty("mail.store.protocol", "imaps"); properties.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(properties, null); Session session = Session.getDefaultInstance(properties, null);
@ -29,6 +29,12 @@ public class EmailService {
System.getenv("spring.mail.username"), System.getenv("spring.mail.username"),
System.getenv("spring.mail.password") System.getenv("spring.mail.password")
); );
return store;
}
public void readEmails() throws MessagingException, IOException {
Store store = connect();
Folder inbox = store.getFolder("INBOX"); Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY); inbox.open(Folder.READ_ONLY);
@ -37,14 +43,23 @@ public class EmailService {
Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
for (Message message : messages) { for (Message message : messages) {
// Process each email message //TODO: do something with the message
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Body: " + message.getContent());
} }
// Close resources // Close resources
inbox.close(false); inbox.close(false);
store.close(); store.close();
} }
public void expunge() throws MessagingException {
Store store = connect();
Folder trash = store.getFolder("INBOX").getFolder("Trash");
trash.open(Folder.READ_WRITE);
for (Message message : trash.getMessages()) {
message.setFlag(Flags.Flag.DELETED, true);
}
trash.close(true); // expunge
store.close();
}
} }

@ -0,0 +1,44 @@
package com.olexyn.burnsmail.web;
import com.olexyn.burnsmail.mail.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
private final EmailService emailService;
@Autowired
public Controller(
EmailService emailService
){
this.emailService = emailService;
}
@PostMapping("/read")
public ResponseEntity<String> postConfig() {
try {
emailService.readEmails();
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
@PostMapping("/expunge")
public ResponseEntity<String> expunge() {
try {
emailService.expunge();
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok().build();
}
}

@ -0,0 +1,6 @@
POST http://localhost:42003/expunge
Content-Type: application/json
{
}

@ -0,0 +1,6 @@
POST http://localhost:42003/read
Content-Type: application/json
{
}
Loading…
Cancel
Save