ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 회원정보 메일 전송하기
    웹프로그래밍/기타 2018. 5. 27. 16:23

    http://javacan.tistory.com/entry/25


    http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/


    http://gongcha.tistory.com/27


    http://darkhorizon.tistory.com/324



    http://devgwangpal.tistory.com/34


    - gmail 이용 전송 세팅 
    # gmail -> 환경 설정 -> 전달 및 pop/IMAP -> IMAP액세스 ->IMAP 사용 설정 : "참조":https://support.google.com/mail/troubleshooter/1668960?hl=ko&rd=2#ts=1665018 
    # 내 계정 -> 로그인및 보안 -> 연결된 앱 및 사이트 -> 보안 수준이 낮은 앱 허용 : 사용 으로 변경 "참조":http://micropilot.tistory.com/category/Java%20Mail/Gmail%20Account 
    # "coding":http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ 

    출처: http://devgwangpal.tistory.com/34 [개발 필기블로그입니다.]



    http://sillytalking.tistory.com/49





    package mdm.comm.util;


    import java.util.List;

    import java.util.Properties;

    import javax.mail.Address;

    import javax.mail.Authenticator;

    import javax.mail.Message;

    import javax.mail.PasswordAuthentication;

    import javax.mail.Session;

    import javax.mail.Transport;

    import javax.mail.internet.InternetAddress;

    import javax.mail.internet.MimeMessage;

    public class mailSend {

    public static void send(List<String> toMail, String fromMail, String message, String title,String gmail, String id, String pwd)throws Exception{

    Properties p = new Properties();

    p.put("mail.smtp.user", "google계정@gmail.com"); 

    p.put("mail.smtp.host", "smtp.gmail.com");

    p.put("mail.smtp.port", "465");

    p.put("mail.smtp.starttls.enable","true");

    p.put( "mail.smtp.auth", "true");

    p.put("mail.smtp.debug", "true");

    p.put("mail.smtp.socketFactory.port", "465"); 

    p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 

    p.put("mail.smtp.socketFactory.fallback", "false");    

    Authenticator auth = new SMTPAuthenticator(id,pwd);

    Session session = Session.getInstance(p, auth);

    session.setDebug(true); // 메일을 전송할 때 상세한 상황을 콘솔에 출력한다.

    MimeMessage msg = new MimeMessage(session);

    msg.setSubject(title,"UTF-8");

    Address fromAddr = new InternetAddress(fromMail); // 보내는 사람의 메일주소

    msg.setFrom(fromAddr);

      InternetAddress[] addressTo = new InternetAddress[toMail.size()];

      for (int i = 0; i < toMail.size(); i++) {

       addressTo[i] = new InternetAddress(toMail.get(i));

      }

      msg.setRecipients(Message.RecipientType.TO, addressTo);


    msg.setContent(message, "text/html;charset=utf-8");

      

    Transport.send(msg);

    }

    private static class SMTPAuthenticator extends javax.mail.Authenticator {

    String id;

    String pwd;

    SMTPAuthenticator(String id , String pwd){

    this.id = id;

    this.pwd = pwd;

    }

    public PasswordAuthentication getPasswordAuthentication() {

    return new PasswordAuthentication("구글아이디", "pwd"); //구글아이디는 구글계정에서 @이후를 제외한 값이다. (예: abcd@gmail.com --> abcd)

    }

    }


    * 다음과 같은 에러가 발생할 경우 gmail 계정을 2단계 인증으로 등록하고, 위 소스의 pwd란에 gmail용 비밀번호가 아닌 ACCESS 용 비밀번호를 등록해야 한다.

    535-5.7.8 Username and Password not accepted. Learn more at

    535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=xxxx xxxxxx

    javax.mail.AuthenticationFailedException


    * gmail 2단계 인증하여 비밀번호 등록하는 법

    1. https://myaccount.google.com/

    2. https://accounts.google.com/b/0/SmsAuthConfig?hl=ko 
    > 설정 시작

    3. 재로그인

    4. https://accounts.google.com/b/0/SmsAuthSettings?Setup=1
    > 전화번호 입력 후 코드 전송

    > 인증코드 입력

    5. https://security.google.com/settings/security/apppasswords?pli=1
    > 기기선택과 앱(MAIL) 선택 후 생성

    6. 생성된 비밀번호를 위 소스의 pwd란에 입력한다.



    출처: http://darkhorizon.tistory.com/324 [너머]