[java] Javamail을 사용하여 이메일에 전화 번호 추가하기

Javamail은 Java에서 이메일을 보내고 받는 기능을 제공하는 라이브러리입니다. 이 라이브러리를 사용하여 이메일에 전화 번호를 추가하는 방법에 대해 알아보겠습니다.

Javamail 설치

먼저, Javamail을 사용하기 위해서는 해당 라이브러리를 프로젝트에 추가해야 합니다. Maven을 사용한다면, pom.xml 파일에 다음 의존성을 추가합니다:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

그리고 Gradle을 사용한다면, build.gradle 파일에 다음 종속성을 추가합니다:

implementation 'com.sun.mail:javax.mail:1.6.2'

이메일에 전화 번호 추가하기

이제 Javamail을 사용하여 이메일에 전화 번호를 추가하는 방법을 알아보겠습니다. 아래 예제 코드를 참고하세요:

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailSender {
    public static void main(String[] args) {
        // SMTP 서버 설정
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.example.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        // 이메일 계정 정보
        String username = "your-email@example.com";
        String password = "your-password";

        // 이메일 생성
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new javax.mail.PasswordAuthentication(username, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("your-email@example.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));
            message.setSubject("샘플 이메일");
            message.setText("안녕하세요, 전화번호는 123-456-7890입니다.");

            // 이메일 전송
            Transport.send(message);

            System.out.println("이메일 전송 완료!");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

위 코드에서 "smtp.example.com", "your-email@example.com", "your-password", "recipient@example.com" 등의 값을 실제로 사용하는 값으로 변경해야 합니다. 또한, 이메일 내용에 전화번호를 원하는 형식으로 포함시킬 수 있습니다.

이제 Javamail을 사용하여 이메일에 전화 번호를 추가하는 방법에 대해 알게 되었습니다. Javamail을 사용하면 손쉽게 이메일을 보낼 수 있으며, 필요한 정보를 적절하게 포함시켜 전송할 수 있습니다.

참고 자료