앱을 개발하는 과정에서 intent를 쓰는 것이 아닌 직접 전송하는 것을 구현하고자 합니다.
구글에서 검색을 통해 한 개발자분께서 알기 쉽도록 설명해 주신 것을 보고 따라하고 있습니다.
그 링크는 http://mainia.tistory.com/506 이곳입니다.
permission도 제대로 주고,
jar 파일 단순히 프로젝트의 속성으로 들어가 외부 jar를 불러오는 방법 과
import한 후 내부 jar를 추가하는 두가지 방법 모두 해보았지만
계속 오류가 나는 원인을 모르겠습니다.
혹시 모르니 코드를 직접 이곳에 써보자면
//// GMailSender.class
package com.success.sample;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
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 GMailSender extends javax.mail.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");
}
}
}
//////// MailSend.class
package com.success.sample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.success.R;
public class SendMail extends Activity {
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.send_mail);
final Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
GMailSender sender = new GMailSender("id@gmail.com","storm200"); // SUBSTITUTE HERE
try {
sender.sendMail(
"메일제목 !!", //subject.getText().toString(),
"메일 본문입니다..~~ ", //body.getText().toString(),
"id@gmail.com", //from.getText().toString(),
"id@naver.com" //to.getText().toString()
);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
});
}
}
위에 있는 링크에서 그대로 가져온 것입니다.
클래스 명 등을 모두 똑같이 하였기에 다른 점이 있다면 package명 정도 뿐입니다.
05-20 00:41:56.226: E/AndroidRuntime(16834): FATAL EXCEPTION: main
05-20 00:41:56.226: E/AndroidRuntime(16834): java.lang.NoClassDefFoundError: com.success.sample.GMailSender
05-20 00:41:56.226: E/AndroidRuntime(16834): at com.success.sample.GMailActivity$1.onClick(GMailActivity.java:24)
05-20 00:41:56.226: E/AndroidRuntime(16834): at android.view.View.performClick(View.java:3620)
05-20 00:41:56.226: E/AndroidRuntime(16834): at android.view.View$PerformClick.run(View.java:14292)
05-20 00:41:56.226: E/AndroidRuntime(16834): at android.os.Handler.handleCallback(Handler.java:605)
05-20 00:41:56.226: E/AndroidRuntime(16834): at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 00:41:56.226: E/AndroidRuntime(16834): at android.os.Looper.loop(Looper.java:137)
05-20 00:41:56.226: E/AndroidRuntime(16834): at android.app.ActivityThread.main(ActivityThread.java:4507)
05-20 00:41:56.226: E/AndroidRuntime(16834): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 00:41:56.226: E/AndroidRuntime(16834): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 00:41:56.226: E/AndroidRuntime(16834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-20 00:41:56.226: E/AndroidRuntime(16834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-20 00:41:56.226: E/AndroidRuntime(16834): at dalvik.system.NativeStart.main(Native Method)
제 로그캣을 복사한 것입니다.
로그캣을 더블클릭하여 문제가 되는 곳을 찾으니
GMailSender sender = new GMailSender("id@gmail.com","storm200"); // SUBSTITUTE HERE
이곳이라 표시가 됩니다.
디버그를 해보니 해당 GMailSender Class 생성자로 가기전에 문제가 생기는 것으로 보아
jar파일이 제대로 등록이 되지 않은걸까 싶긴한데 제 검색능력으로는 해볼 수 있는 방법을 일단은 다 해본듯하네요..
혹 방법을 알고 계신분이 계시다면 조언을 부탁드리겠습니다.
첫 질문글이기에 공지사항을 모두 읽고 질문을 드립니다.
좀 뭐한 질문 인데요 위에 있는 3개의 id 에 실사용자 id를 넣어서 시험하신거죠? 소스상엔 그냥 아이디로 되있어서 물어보는거예요
실사용자 id를 써야 할거 같은데