Skip to content

Commit

Permalink
[fix] : mail api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeoongu committed Jul 1, 2024
1 parent 9edb015 commit 636c080
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import com.Nunbody.domain.Mail.domain.MailBody;
import com.Nunbody.domain.Mail.domain.MailHeader;
import com.Nunbody.domain.Mail.domain.MailList;
import com.Nunbody.domain.Mail.domain.PlatformType;
import com.Nunbody.domain.Mail.repository.MailBodyRepository;
import com.Nunbody.domain.Mail.repository.MailRepository;
import com.Nunbody.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.io.IOException;
Expand All @@ -37,10 +37,12 @@ public class GmailController {
private static String platformHost ="imap.gmail.com";
private final Pattern pattern = Pattern.compile("<(.*?)>");
private final MemberRepository memberRepository;
private final MailBodyRepository mailBodyRepository;
public static List<MailBody> mailBodies = new ArrayList<>();
@GetMapping("/gmail")
public MailList getGmailMessages() {
List<MailBody> mailBodies = new ArrayList<>();
MailList mailList = MailList.builder().memberId(userId).build();
public ResponseEntity<?> getGmailMessages() throws MessagingException, IOException {

// MailList mailList = MailList.builder().memberId(userId).build();
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.host", "imap.gmail.com");
Expand All @@ -53,32 +55,28 @@ public MailList getGmailMessages() {
Session session = Session.getInstance(props);
StringBuilder result = new StringBuilder();

try {

Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "[email protected]", "eqno csrt cmcm xnaa");


Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

Message[] messages = inbox.getMessages(1, 10); // 최근 10개 메일 가져오기
Message[] messages = inbox.getMessages(1, 9); // 최근 10개 메일 가져오기

MailHeader latestMail = mailRepository.findFirstByMemberIdAndPlatformTypeOrderByDateDesc(userId, platformType).orElse(null);

if (latestMail == null) {
reset(messages, userId, platformType, platformHost);
} else {
// if (latestMail == null) {
// reset(messages, userId, platformType, platformHost);
// } else {
processNewMails(messages, userId, platformType, platformHost, latestMail, mailBodies);
}

mongoTemplate.insertAll(mailBodies);
// }

} catch (Exception e) {
// log.error("Error in mailSetting", e);
throw new RuntimeException("Failed to process emails", e);
}
// mongoTemplate.insertAll(mailBodies);
mailBodyRepository.saveAll(mailBodies);

return mailList;
return ResponseEntity.ok(null);
}
@Transactional
public void reset(Message[] messages, Long userId, PlatformType platformType, String platformHost)
Expand All @@ -95,20 +93,20 @@ public void reset(Message[] messages, Long userId, PlatformType platformType, St
}
private void processNewMails(Message[] messages, Long userId, PlatformType platformType, String platformHost,
MailHeader latestMail, List<MailBody> mailBodies) throws MessagingException, IOException {
LocalDateTime latestMailDate = LocalDateTime.parse(latestMail.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// LocalDateTime latestMailDate = LocalDateTime.parse(latestMail.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
ZoneId seoulZone = ZoneId.of("Asia/Seoul");

for (int i = messages.length - 1; i >= 0; i--) {
Message message = messages[i];
LocalDateTime messageDate = LocalDateTime.ofInstant(message.getReceivedDate().toInstant(), seoulZone);

if (messageDate.isAfter(latestMailDate)) {
// if (!messageDate.isAfter(latestMailDate)) {
MailHeader mailHeader = createMailHeader(message, userId, platformType);
mailRepository.save(mailHeader);
mailBodies.add(extractMailBody(platformHost, message, mailHeader.getId()));
} else {
break; // No need to check older messages
}
// } else {
// break; // No need to check older messages
// }
}
}
private String handleMultipart(Multipart multipart) throws MessagingException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SpringSecurityConfig {
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtTokenProvider tokenProvider;
private final CorsConfig corsConfig;
private static final String[] whiteList = {"/api/member/signup", "/api/member/signin", "/api/member/validate", "/api/mail/validate"};
private static final String[] whiteList = {"/api/member/signup", "/api/member/signin", "/api/member/validate", "/api/mail/validate","/gmail"};

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
Expand Down

0 comments on commit 636c080

Please sign in to comment.