자바 스프링 메일 안에 이미지 추가하는 방법

2023. 7. 18. 16:38·프로그래밍 일기/Java & Spring
반응형
버전 정보

스프링 레거시 3.2.9.RELEASE

자바 1.8

javax.mail 1.6.2

maven 프로젝트

 

MailUtil.java
messageHelper.addInline("image", new ClassPathResource("/images/hanati/logo.png"));

아래는 전체 로직 코드입니다.

public boolean sendMail(Map msgInfo) throws Exception {
   String subject = (String) msgInfo.get("p_mail_subject");//제목
   String text = (String) msgInfo.get("p_mail_content");//내용
   String fromUser = (String) msgInfo.get("p_send_email");//발신자 이메일
   String fromUserName = (String) msgInfo.get("p_send_name");//발신자명
   String toUser = (String) msgInfo.get("p_recv_email");//수신자 이메일

   MimeMessage message = mailSender.createMimeMessage();
   DataSource dataSource = null;  
   
       try {
           MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, SMTP_ENC);
           messageHelper.setSubject(subject);
           messageHelper.setTo(toUser);      
           messageHelper.setFrom(fromUser, StringUtil.nvl(fromUserName, fromUser));
           messageHelper.setText(text, true);
      	   messageHelper.addInline("image", new ClassPathResource("/images/hanati/logo.png"));
        
           mailSender.send(message);

       } catch (Exception e) {
           e.printStackTrace();
           return false;
       }
       return true;
}

 

HTML 그려주는 곳
sb.append("<img src='cid:image' title='이미지'/>");

messageHelper.addInline("원하는 명칭", new ClassPathResource("경로"));

<img src='cid:원하는 명칭'/> 

위 두 값을 잘 맞추기만 하면 정상작동 합니다.

반응형
저작자표시 (새창열림)

'프로그래밍 일기 > Java & Spring' 카테고리의 다른 글

Spring Boot Nuxt3 카카오 로그인 및 JWT 토큰 발급  (0) 2024.07.22
JWT란?  (0) 2024.07.09
[에러해결] javax.mail.MessagingException: Could not connect to SMTP host: smtp.worksmobile.com, port: 465, response: -1 (isSSL true 설정)  (0) 2023.07.18
[Spring Boot] 파일 다운로드 로직 구현 feat.Nuxt3  (0) 2023.07.12
[Spring Boot] 단일, 다중 파일 업로드 로직 구현 feat.Nuxt3  (0) 2023.07.12
'프로그래밍 일기/Java & Spring' 카테고리의 다른 글
  • Spring Boot Nuxt3 카카오 로그인 및 JWT 토큰 발급
  • JWT란?
  • [에러해결] javax.mail.MessagingException: Could not connect to SMTP host: smtp.worksmobile.com, port: 465, response: -1 (isSSL true 설정)
  • [Spring Boot] 파일 다운로드 로직 구현 feat.Nuxt3
MakeMe
MakeMe
제가 포스팅한 글 중 잘못된 부분이 있으면 알려주세요!
  • MakeMe
    Developer blog
    MakeMe
    • 모든 글 (71)
      • 프로그래밍 일기 (57)
        • Java & Spring (21)
        • Python & Flask (3)
        • Linux (12)
        • Front-End (10)
        • DB & SQL (6)
        • Git (3)
        • IDE (2)
      • 자격증 (7)
        • 정보처리기능사 (2)
        • SQLD (1)
        • SW개발_L5 (1)
        • AWS (3)
      • 독립일기 (7)
        • 중소기업청년대출 (7)
  • 인기 글

  • 태그

    젠킨스 우분투 설치
    Vue
    스프링
    스프링부트
    넉스트
    springboot
    java
    AWS
    중소기업청년대출
    IntelliJ
    nuxt
    자동배포설정
    고용보험내역서
    nuxt3
    DB
    젠킨스 자동 배포
    중기청서류
    flask 세팅
    건강보험자격득실확인서
    중기청후기
    인텔리제이
    중기청필요서류
    psql
    flask
    자바환경변수
    중기청필수서류
    Spring
    DBeaver
    MYSQL
    자바
  • hELLO· Designed By정상우.v4.10.1
MakeMe
자바 스프링 메일 안에 이미지 추가하는 방법
상단으로

티스토리툴바