[java] Javamail을 사용하여 이메일에 서명 추가하기

이메일을 보낼 때, 종종 메일에 서명을 추가하는 것이 좋습니다. Javamail 라이브러리를 사용하여 Java로 이메일을 보낼 때, 이메일에 서명을 추가하는 방법을 알아보겠습니다.

1. 의존성 추가

먼저, 프로젝트의 의존성에 Javamail을 추가해야 합니다. Maven을 사용하는 경우, pom.xml 파일에 다음 의존성을 추가하세요:

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

Gradle을 사용하는 경우, build.gradle 파일에 다음 의존성을 추가하세요:

dependencies {
    implementation 'javax.mail:javax.mail-api:1.6.2'
    implementation 'com.sun.mail:javax.mail:1.6.2'
}

의존성을 추가한 후, 프로젝트를 다시 빌드하세요.

2. 이메일에 서명 추가하기

이제 이메일에 서명을 추가하는 방법을 알아보겠습니다. 아래 코드는 Javamail을 사용하여 Gmail SMTP 서버를 통해 이메일을 보내고, 이메일에 서명을 추가하는 예제입니다.

import java.util.Properties;
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;

public class EmailExample {

    public static void main(String[] args) {
        String host = "smtp.gmail.com";
        int port = 587;
        String username = "your-email@gmail.com";
        String password = "your-password";
        
        String recipient = "recipient-email@example.com";
        String subject = "Hello";
        String content = "This is the content of the email.";
        
        Properties properties = System.getProperties();
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        
        Session session = Session.getInstance(properties);
        
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
            message.setSubject(subject);
            message.setText(content);
            message.setSentDate(new java.util.Date());
            
            // 서명 추가
            String signature = "Best regards,\nYour Name";
            message.setText(content + "\n\n" + signature);
            
            Transport transport = session.getTransport("smtp");
            transport.connect(host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            
            System.out.println("Email sent successfully.");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

위 코드에서, usernamepassword를 보내는 이메일의 Gmail 계정으로 변경하세요. recipient를 이메일을 받을 사람의 이메일 주소로 변경하세요. content 변수에 이메일의 내용을 입력하세요.

서명을 추가하는 부분은 아래와 같습니다:

// 서명 추가
String signature = "Best regards,\nYour Name";
message.setText(content + "\n\n" + signature);

이메일의 내용 다음에 \n\n로 개행을 추가하고, signature 변수에 해당 서명을 추가하세요.

결론

Javamail을 사용하여 Java로 이메일에 서명을 추가하는 방법에 대해 알아보았습니다. Javamail을 사용하여 이메일을 보낼 때 서명을 추가하여 더 전문적인 이메일을 보낼 수 있습니다.