Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JS Coding

[Java] 간단한 mail 코딩 형식 본문

Java

[Java] 간단한 mail 코딩 형식

JSKJS 2024. 4. 11. 22:54

 

간단하게 메일을 보내는 코드를 소개하려 한다.

public class MailJar {
	public static void main(String[] args) throws MessagingException {
		
		String senderId = "kjs@happy.net"; // 예시 이메일
		String senderPw = "1234";
		String receiverId = "kjs123@sad.com"; // 받을 이메일
		
		Properties props = new Properties();
	    props.put("mail.smtp.host", "smtp.naver.com");
	    props.put("mail.smtp.auth", "true");
		//props.put("mail.smtp.ssl.enable","true");
	    //props.put("mail.smtp.port", "465");
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.port", "587");
		
		
		Session session = Session.getDefaultInstance(props, new Authenticator() {
		      protected PasswordAuthentication getPasswordAuthentication() {
		       return new PasswordAuthentication(senderId, senderPw);
		      }
		     });
		
		MimeMessage message = new MimeMessage(session);
	      message.setFrom(new InternetAddress(senderId));
	      message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverId));
	      message.setSubject("메일 제목");
	      message.setText("메일 내용");
	      
	      Transport.send(message);
	
	}
	}