package com.itafterschool;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MAIL {
public static void main(String[] args) {
Dimension dim = new Dimension(800,350);
JFrame frame = new JFrame("Mail Sender Program with Google");
frame.setLocation(250, 100);
frame.setPreferredSize(dim);
Container contentPane = frame.getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 2));
JButton button = new JButton("Send");
JTextField text1 = new JTextField();
JTextField text2 = new JTextField();
JTextField text3 = new JTextField();
JTextField text4 = new JTextField();
JTextField text5 = new JTextField();
JTextField text6 = new JTextField();
panel.add(new JLabel("보낼 주소"));
panel.add(text1);
panel.add(new JLabel("보내는 사람 주소 "));
panel.add(text2);
panel.add(new JLabel("제목 "));
panel.add(text3);
panel.add(new JLabel("내용 "));
panel.add(text4);
panel.add(new JLabel("ID (Google ID) "));
panel.add(text5);
panel.add(new JLabel("Password (Google PSW) "));
panel.add(text6);
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
ActionListener lister = new button1(text1, text2, text3, text4, text5, text6);
button.addActionListener(lister);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
이게 MAIL 을 보내기위한 윈도우프로그램 소스이고 ,
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.log4j.Logger;
public class MailSender {
private static Logger logger = Logger.getLogger(MailSender.class);
private String smtpHost;
private int smtpPort;
private String smtpUser;
private String smtpPassword;
public MailSender(String smtpHost, int smtpPort, String smtpUser, String smtpPassword) {
this.smtpHost = smtpHost;
this.smtpPort = smtpPort;
this.smtpUser = smtpUser;
this.smtpPassword = smtpPassword;
}
public int send(MailMessage msg) {
int rt = ITAfterSchoolConst.RT_INTERNAL_ERROR;
if(msg == null) {
logger.info("MailMessage object is null");
return ITAfterSchoolConst.RT_WRONG_PARAMETER;
}
Email mail = new SimpleEmail();
mail.setDebug(true);
mail.setHostName(smtpHost);
mail.setSmtpPort(smtpPort);
mail.setCharset("utf-8");
if (smtpUser != null && smtpPassword != null) {
mail.setAuthentication(smtpUser, smtpPassword);
}
mail.setSubject(msg.getMailSubject());
try {
mail.setSSLOnConnect(true);
mail.setFrom(msg.getMailFrom());
mail.setMsg(msg.getMailBody());
mail.addTo(msg.getMailTo());
mail.send();
rt = ITAfterSchoolConst.RT_SUCCESS;
} catch (EmailException e) {
logger.warn("Email Error" , e);
rt = ITAfterSchoolConst.RT_MAIL_ERROR;
}
return rt;
}
이게 send 메소드이구여
public void testSend(String str1, String str2, String str3 , String str4 , String str5, String str6) {
MailMessage mailMsg = new MailMessage();
mailMsg.setMailTo(str1);
mailMsg.setMailFrom(str2);
mailMsg.setMailSubject(str3);
mailMsg.setMailBody(str4);
MailSender sender = new MailSender("smtp.gmail.com", 587, str5, str6);
int rt = sender.send(mailMsg);
System.out.println("Return code : " + rt);
}
이게 send 명령어를 실행해주는 class 구요
package com.itafterschool;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class button1 implements ActionListener {
JTextField text1;
JTextField text2;
JTextField text3;
JTextField text4;
JTextField text5;
JTextField text6;
public button1(JTextField text1,JTextField text2,JTextField text3,JTextField text4,JTextField text5,JTextField text6) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
this.text4 = text4;
this.text5 = text5;
this.text6 = text6;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String str1 = text1.getText();
String str2 = text2.getText();
String str3 = text3.getText();
String str4 = text4.getText();
String str5 = text5.getText();
String str6 = text6.getText();
TestMailSender msg = new TestMailSender();
msg.testSend(str1, str2, str3, str4, str5, str6);
}
}
이건 맨위의 Mail.class 에서 button1을 처리해주는 class구요
(
ActionListener lister = new button1(text1, text2, text3, text4, text5, text6);
button.addActionListener(lister);
) 이부분
실행을 하면 메일 내용 및 제목 등등 전부 잘 전송이 되는데 딱 한곳 , 보내는사람주소 가
GMAIL로 뜹니다.
전 분명 다른 email 로 보내려햇는데...ㅠㅠㅠ 자꾸 google 아이디로 뜹니다 엉엉...
저 소스는 구글에서 포트를 얻어서 사용하는거에요!
저는 보내는이의 주소를 바꿔서 보내고싶은데 그게안됩니다 ㅠㅠㅠ 왜이럴까요
개인정보는 가렸습니다.
실행은 정상적으로되는데 꼭 저 ... 보낸사람주소가 안바뀌네요 ㅠㅠㅠㅠ
원래 안바뀌는건가요? 그럼 해당기능은 필요가없는걸까요 흠