Skip to content

Commit

Permalink
feat: 구글메일 불러오기
Browse files Browse the repository at this point in the history
  • Loading branch information
beakgugong committed Dec 8, 2023
1 parent 9cd3d46 commit 8b1a398
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ dependencies {
//DB gradle
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

implementation 'org.jsoup:jsoup:1.17.1'
}

tasks.named('test') {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/Nunbody/domain/Mail/domain/MailList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class MailList {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
@Builder.Default
public ArrayList<MailHeader> data = new ArrayList<>();
//@Builder.Default
//public ArrayList<MailHeader> data = new ArrayList<>();

public void addData(MailHeader mailHeader) {
/*public void addData(MailHeader mailHeader) {
data.add(mailHeader);
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import com.Nunbody.domain.Mail.repository.MailRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.Nunbody.domain.Mail.service.MailManageService;
import com.Nunbody.domain.Mail.service.MailService;
import com.Nunbody.global.common.SuccessResponse;
import java.util.Map;
import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Page;
Expand All @@ -28,8 +29,16 @@ public class MailController {
private final MailService mailService;
private final MailManageService mailManageService;
@GetMapping("/mails")
public ResponseEntity<SuccessResponse<?>> getMail(@RequestParam Long userId) {
final MailList mailList = mailService.getMail(userId);
public ResponseEntity<SuccessResponse<?>> getMail(@RequestParam Long userId, @RequestBody Map<String,String> type) {
MailList mailList;
String platform = type.get("platform");

if(platform.equals("naver")) {
mailList = mailService.getNaverMail(userId);
}
else {
mailList = mailService.getGoogleMail(userId);
}
return SuccessResponse.ok(mailList);
}
@GetMapping("/header")
Expand Down
47 changes: 38 additions & 9 deletions src/main/java/com/Nunbody/domain/mail/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public class MailService {



public MailList getMail(Long userId){
List<MailBody> mailBodies = new ArrayList<>();
public MailList getNaverMail(Long userId){

MailList naverMail = MailList.builder()
.userId(userId)
.build();

String id = memberRepository.findNaverIdById(userId).orElse(null);
String decode = EncoderDecoder.decodeFromBase64(memberRepository.findNaverPasswordById(userId).orElse(null));
String decode = EncoderDecoder.decodeFromBase64(memberRepository.findNaverPasswordById(userId).get());

/** naver mail */
final String naverHost = "imap.naver.com";
Expand All @@ -61,10 +61,36 @@ public MailList getMail(Long userId){

final String naverPassword = decode;

naverMail = mailSetting(userId,naverHost,naverId,naverPassword,naverMail);
return naverMail;

}
public MailList getGoogleMail(Long userId){

MailList googleMail = MailList.builder()
.userId(userId)
.build();

String id = memberRepository.findGmailIdById(userId).orElse(null);
String decode = EncoderDecoder.decodeFromBase64(memberRepository.findGmailPasswordById(userId).get());

/** google mail */
final String googleHost = "imap.gmail.com";

final String googleId = id;

final String googlePassword = decode;

googleMail = mailSetting(userId, googleHost, googleId, googlePassword ,googleMail);
return googleMail;

}
public MailList mailSetting(Long userId,String platformHost, String platformId, String platformPassword, MailList mailList){
List<MailBody> mailBodies = new ArrayList<>();

try {
Properties prop = new Properties();
prop.put("mail.imap.host", naverHost);
prop.put("mail.imap.host", platformHost);
prop.put("mail.imap.port", 993);
prop.put("mail.imap.ssl.enable", "true");
prop.put("mail.imap.ssl.protocols", "TLSv1.2");
Expand All @@ -75,33 +101,36 @@ public MailList getMail(Long userId){

// Store 클래스
Store store = session.getStore("imap");
store.connect(naverHost, naverId, naverPassword);
store.connect(platformHost,platformId,platformPassword);

// 받은 편지함
Folder folder = store.getFolder("INBOX");
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);

Message[] messages = folder.getMessages();
MailHeader mailHeaderData;


for (int i = 0; i < 20; i++) {
for (int i = 0; i <10 ; i++) {
matcher = pattern.matcher(messages[i].getFrom()[0].toString());
if (matcher.find()) {
String fromPerson = matcher.group(1);
mailHeaderData = MailHeader.builder()
.title(messages[i].getSubject())
.fromPerson(fromPerson)
.date(String.valueOf(messages[i].getReceivedDate()))
.member(memberRepository.findById(userId).get())
.build();
} else {
mailHeaderData = MailHeader.builder()
.title(messages[i].getSubject())
.fromPerson(messages[i].getFrom()[0].toString())
.date(String.valueOf(messages[i].getReceivedDate()))
.member(memberRepository.findById(userId).get())
.build();
}
mailRepository.save(mailHeaderData);
naverMail.addData(mailHeaderData);
//mailList.addData(mailHeaderData);

Long mailId = mailHeaderData.getId();
mailBodies.add(extractMailBody(messages[i],mailId));
Expand All @@ -116,7 +145,7 @@ public MailList getMail(Long userId){
e.printStackTrace();
}

return naverMail;
return mailList;
}

public MailBody extractMailBody(Message messages, Long mailId) throws MessagingException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import org.springframework.data.jpa.repository.Query;

public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByAccount(String account);
@Query("SELECT m.naverId FROM Member m WHERE m.id = :id")
Optional<String> findNaverIdById(Long id);
@Query("SELECT m.naverPassword FROM Member m WHERE m.id = :id")
Optional<String> findNaverPasswordById(Long id);

@Query("SELECT m.gmailId FROM Member m WHERE m.id = :id")
Optional<String> findGmailIdById(Long id);
@Query("SELECT m.gmailPassword FROM Member m WHERE m.id = :id")
Optional<String> findGmailPasswordById(Long id);
}

0 comments on commit 8b1a398

Please sign in to comment.