-
회원정보 메일 전송하기웹프로그래밍/기타 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 [너머]'웹프로그래밍 > 기타' 카테고리의 다른 글
프로젝트 외부에 업로드 폴더 설정하기 - 이클립스/윈도우/톰캣 (1) 2018.06.12 아이디 찾기 결과 - 문자열 치환 (0) 2018.06.12 자바에러 - import 잘 안될때 (0) 2018.05.29 form 태그 get방식 한글 인코딩 - 톰캣서버 (0) 2018.05.17 자바 - 이클립스 단축키 (0) 2018.02.09