'안드로이드 이메일 보내기'에 해당되는 글 1건

  1. 2012.02.15 안드로이드 E-Mail 보내기 예제 따라하기 (10)
JAVA/Android2012.02.15 14:16
- 이메일 전송 App을 만들어보자. 이를 통해서 내 App의 사용자들의 의견등을 받아 볼 수 있다.
- 밑 예제의 E-mail 주소와 Password 만 코드 상에 내것으로 바꾸면 된다.
- 인터넷 퍼미션을 주는 것을 잊지 말자 !!!!!

-Java 소스
public class EmailActivity extends Activity implements OnClickListener {
ProgressDialog dialog;
EditText et;
GMailSender sender;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button) this.findViewById(R.id.bt_help);
bt.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
et = (EditText) this.findViewById(R.id.et);
sender = new GMailSender("XXXXX@gmail.com", "XXXXXXX"); // SUBSTITUTE ID PASSWORD
timeThread();
}

public void timeThread() {

dialog = new ProgressDialog(this);
dialog = new ProgressDialog(this);
dialog.setTitle("Wait...");
dialog.setMessage("의견을 보내는 중입니다.");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
new Thread(new Runnable() {

public void run() {
// TODO Auto-generated method stub
try {
sender.sendMail("의견보내기", // subject.getText().toString(),
et.getText().toString(), // body.getText().toString(),
"XXXXX@hanmail.net", // from.getText().toString(),
"XXXXX@hanmail.net" // to.getText().toString()
);
sleep(3000);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
Toast.makeText(EmailActivity.this, "신청 실패", Toast.LENGTH_SHORT)
.show();

}
dialog.dismiss();
}

private void sleep(int i) {
// TODO Auto-generated method stub

}

}).start();
}

}
--------------------------------------------------------------------------------------------------------
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");
}
}
}
--------------------------------------------------------------------------------------------------------- xml 소스
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:scaleType="fitXY" >

        <EditText
            android:id="@+id/et"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="vertical" >

        <Button
            android:id="@+id/bt_help"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="15dp"
            android:text="보내기 버튼" />
    </LinearLayout>

</LinearLayout>
 -------------------------------------------------------------------------------------------------------- 추가 파일
프로젝트 오른쪽 마우스 -> Properties -> Java Build Path -> Add External JARs... -> jar 파일 3개 추가



--------------------------------------------------------------------------------------------------------
- 실행 화면

신고
Posted by Namseungil

티스토리 툴바