카테고리 없음
IP공유
zigo0
2024. 7. 16. 22:47
고정 IP를 사용하지 않기 때문에 IP가 변경이 될때마다 메일로 전송하는 프로그램을 개발했다.
javax.mail
javax.mail-api
라이브러리를 사용해서 개발했다.
이메일의 경우
####이 본인의 이메일과 비밀번호를 입력하면되고 aaaa에 전송할 메일을 적으면된다.
그럼 ####에서 AAAA로 접근이 가능하다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
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.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Test {
static final String user_email = "####";
static final String user_pw = "####";
static final String smtp_host = "smtp.gmail.com";
static final int smtp_port = 465; // TLS : 587, SSL : 465
public static void main(String[] args) throws InterruptedException {
String ip;
String before_ip = ip_chek();
// 1시간 마다 IP 체크해서 달라지면 메일 전송
while (true) {
ip = ip_chek();
Date today = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd E요일 HH:mm:ss");
System.out.println("실행 중 :" +sdf1.format(today)+"\n");
System.out.println("현재 IP :"+ip);
Thread.sleep(1000 * 60 * 60);
if (before_ip.equals(ip)) {
break;
}
before_ip = ip;
send_mail(ip);
}
}
// ip 체크
public static String ip_chek() {
String line = "";// PC IP 저장
// PC IP 조회
try {
String command = "nslookup myip.opendns.com resolver1.opendns.com"; // 실행할 명령어
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
if (line.contains("myip.opendns.com")) {
line = reader.readLine();
line = line.substring(10);
System.out.println(line);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
// ip 담아서 메일 보내기
public static boolean send_mail(String ip) {
Properties props = System.getProperties();
props.put("mail.smtp.host", smtp_host);
props.put("mail.smtp.port", smtp_port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.trust", smtp_host);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user_email, user_pw);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(user_email));
// 받는 이메일
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("####, aaaa")
);
// 제목
Date today = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd E요일 HH:mm:ss");
message.setSubject("IP 변경 알림"+sdf1.format(today));
// 내용
message.setText("IP 주소 : \n\n"+ip);
// 발송
Transport.send(message);
return true;
} catch (MessagingException e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
}
단 구글메일에서 SMTP 설정을 해야한다.
또한 구글의 경우 2차인증이 들어가기 때문에 2차인증 비밀번호를 발급받아야한다.
Google - Gmail SMTP 사용을 위한 세팅
Google - Gmail SMTP 사용을 위한 세팅 G메일에서 제공하는 SMTP를 사용하여 메일발송을 해야 하는 경우가 있습니다. 보통 자체 서버를 운용하는 게 아니라, 웹호스팅 등을 사용하고 있는 경우라면 SMTP
kincoding.com
여기서 발급받은 앱 비밀번호를 비밀번호에 입력한다.
이때 비밀번호를 연속해서 적으면된다. (4자리씩 끊어서 나옴)