이메일 프로토콜

Yono·2024년 6월 27일

이메일 프로토콜

  • SMTP (Simple Mail Transfer Protocol):

    주로 이메일 발송에 사용됩니다.
    클라이언트에서 메일 서버로, 메일 서버에서 다른 메일 서버로 이메일을 전송합니다.

  • IMAP (Internet Message Access Protocol):

    이메일을 서버에 저장하고 클라이언트에서 이를 접근 및 관리하는 데 사용됩니다.
    이메일을 서버에 남겨두고 여러 기기에서 동기화하여 확인할 수 있습니다.

  • POP3 (Post Office Protocol version 3):

    이메일을 서버에서 클라이언트로 가져오고, 서버에서 이메일을 삭제합니다.
    이메일을 하나의 기기에서 관리할 때 주로 사용됩니다.

  • MIME (Multipurpose Internet Mail Extensions):

    이메일 본문에 여러 종류의 콘텐츠(텍스트, 이미지, 비디오 등)를 포함할 수 있도록 확장합니다.

  • Exchange ActiveSync:

    Microsoft Exchange 서버와 클라이언트 간의 이메일, 캘린더, 연락처 동기화를 위해 사용됩니다.

  • WebDAV:

    웹 기반의 분산 저작 프로토콜로, 서버에 저장된 데이터를 관리하고 동기화합니다.


자바 스프링부트를 사용한 SMTP 예시

1. 의존성 추가

Maven

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

Gradle

plugins {
    id 'org.springframework.boot' version '2.7.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    imple![](https://velog.velcdn.com/images/yono/post/f1d668cd-b6e7-44d4-a1d0-3be601971402/image.jpg)
![](https://velog.velcdn.com/images/yono/post/8ab4b6a2-fa9e-4f77-9af0-5be1ea366aa3/image.jpg)
mentation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}
  1. 설정
spring.mail.host=smtp.example.com
spring.mail.port=587
spring.mail.username=your-email@example.com
spring.mail.password=your-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
  1. 메일 서비스 클래스 작성
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    public void sendSimpleEmail(String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        message.setFrom("your-email@example.com");

        mailSender.send(message);
    }
}
  1. 컨트롤러 작성
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @PostMapping("/send")
    public String sendEmail(@RequestParam String to,
                            @RequestParam String subject,
                            @RequestParam String body) {
        emailService.sendSimpleEmail(to, subject, body);
        return "Email sent successfully!";
    }
}
profile
Java,Spring,JavaScript

0개의 댓글