티스토리 뷰

1. mvnrepository 에서 JavaMail 라이브러리 다운받기

http://mvnrepository.com/artifact/javax.mail/mail/1.4.7 에서 관련된 메일을 가져옵니다. 그리고 해당 프로젝트에 임포트 합니다.

첨부도 걸어둡니다.

mail-1.4.7.jar
0.50MB

 

gmail 용 전체 소스 입니다.

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MyMail {
	
	public static void gmailSend() {
        String user = "your_mailid@gmail.com"; // gmail 계정
        String password = "your_password";   // gmail 패스워드

        // SMTP 서버 정보를 설정한다.
        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com"); 
        prop.put("mail.smtp.port", 465); 
        prop.put("mail.smtp.auth", "true"); 
        prop.put("mail.smtp.ssl.enable", "true"); 
        prop.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        
        Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));

            //수신자메일주소
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("수신자메일주소")); 

            // Subject
            message.setSubject("제목을 입력하세요"); //메일 제목을 입력

            // Text
            message.setText("내용을 입력하세요");    //메일 내용을 입력

            // send the message
            Transport.send(message); ////전송
            System.out.println("message sent successfully...");
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        System.out.println();
    }

	public static void main(String[] args) {
		MyMail.gmailSend();
	}

}

 

이러고 바로 실행하면 아래와 같은 에러메시지가 표시될 것이다.

 

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials i184sm11159569pfc.73 - gsmtp


	at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
	at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
	at javax.mail.Service.connect(Service.java:317)
	at javax.mail.Service.connect(Service.java:176)
	at javax.mail.Service.connect(Service.java:125)
	at javax.mail.Transport.send0(Transport.java:194)
	at javax.mail.Transport.send(Transport.java:124)
	at MyMail.gmailSend(MyMail.java:46)
	at MyMail.main(MyMail.java:62)

 

1) javax.mail.AuthenticationFailedException이 발생하는 것은 아래와 같이 보안 수준이 낮은 앱 허용을 해주지 않아서 발생하게 됩니다. 

구글의 내계정 -> 보안에 들어가서 가장 하단에 허용함을 변경합니다.

 

이렇게 하고 다시 실행 하면 에러가 안날것이다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함