public class GMailSender extends Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body,
String sender, String recipients) throws Exception {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(
body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
recipients));
Transport.send(message);
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
------------------------------------------------------------------
회원가입 클레스 txid 는 가입시 이메일 에딧창
txid = id.getText().toString();
회원가입 버튼 이벤트
GMailSender sender = new GMailSender("제아이디@gmail.com","비번");
try {
sender.sendMail("메일제목 !!", // subject
"메일 본문입니다..~~ ", // body
"제아이디@gmail.com", // from
txid// to
);
} catch (Exception e) {
Log.e("SendMail",
e.getMessage(), e);
}
activation.jar additionnal.jar mail.jar 빌드 설정 해주고 인터넷 퍼미션 다줬는데
로그캣입니다. 뭔가 네트웍 스레드 문제가 있는것 같은데 어떻게 해결해야하는거죠 ㅜㅜ
03-27 10:42:39.856: E/SendMail(1252): android.os.NetworkOnMainThreadException
03-27 10:42:39.856: E/SendMail(1252): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
03-27 10:42:39.856: E/SendMail(1252): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-27 10:42:39.856: E/SendMail(1252): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-27 10:42:39.856: E/SendMail(1252): at java.net.InetAddress.getByName(InetAddress.java:289)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.URLName.getHostAddress(URLName.java:487)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.URLName.hashCode(URLName.java:463)
03-27 10:42:39.856: E/SendMail(1252): at java.util.Collections.secondaryHash(Collections.java:3412)
03-27 10:42:39.856: E/SendMail(1252): at java.util.Hashtable.get(Hashtable.java:265)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.Session.getPasswordAuthentication(Session.java:823)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.Service.connect(Service.java:271)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.Service.connect(Service.java:169)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.Service.connect(Service.java:118)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.Transport.send0(Transport.java:188)
03-27 10:42:39.856: E/SendMail(1252): at javax.mail.Transport.send(Transport.java:118)
03-27 10:42:39.856: E/SendMail(1252): at com.login.GMailSender.sendMail(GMailSender.java:63)
03-27 10:42:39.856: E/SendMail(1252): at com.login.Regist$ClickListener$1.run(Regist.java:150)