질문:
Java 애플리케이션에서 이메일 계정을 활용할 수 있나요? Gmail, Yahoo 또는 Hotmail과 같이 이메일을 보내시나요? 어떻게 이를 달성할 수 있습니까?
답변:
1단계: JavaMail API 가져오기
계속하기 전에 다음 사항을 확인하세요. 필요한 JavaMail API jar를 다운로드하여 classpath.
2단계: GMail 구성
다음 Java 코드 조각은 GMail 계정을 사용하여 이메일을 보내는 방법을 보여줍니다.
import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class Main { private static String USER_NAME = "*****"; // GMail user name (before "@gmail.com") private static String PASSWORD = "********"; // GMail password private static String RECIPIENT = "[email protected]"; public static void main(String[] args) { String from = USER_NAME; String pass = PASSWORD; String[] to = { RECIPIENT }; // List of recipient email addresses String subject = "Java send mail example"; String body = "Welcome to JavaMail!"; sendFromGMail(from, pass, to, subject, body); } private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) { Properties props = System.getProperties(); String host = "smtp.gmail.com"; props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", pass); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); try { // Set email properties message.setFrom(new InternetAddress(from)); // Create InternetAddress array for recipients InternetAddress[] toAddress = new InternetAddress[to.length]; for (int i = 0; i <p><strong>참고:</strong> USER_NAME, PASSWORD, RECIPIENT, 제목 및 본문 변수를 바꾸세요. 적절한 값으로 사용하세요.</p><p><strong>추가 고려 사항:</strong></p>
위 내용은 Java 애플리케이션은 어떻게 Gmail, Yahoo 또는 Hotmail을 통해 이메일을 보낼 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!