반응형
=====================================================================================
- 라이브러리 등록
1. 프로젝트 우클릭 -> Properties클릭
2. Java Build Path 클릭 -> 라이브러리탭 클릭 -> Add External JARs...클릭
3. 다운받은 3개의 라이브러리 등록
4. 프로젝트내 libs폴더에 라이브러리 파일 3개 복사
=====================================================================================
[ activity_mail.xml]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="setting.MailActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >
<EditText
android:id="@+id/et_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="제목" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10"
android:orientation="vertical" >
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="내용" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right"
android:orientation="vertical" >
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="메일 보내기" />
</LinearLayout>
</LinearLayout>
|
cs |
=====================================================================================
[ MailActivity.java] (User_id와 Password 변경, Password는 아래 내용 참고)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.sample.R;
public class MailActivity extends ActionBarActivity
{
private GMailSender m;
EditText et_content;
EditText et_title;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mail);
Button btn_send = (Button) this.findViewById(R.id.btn_send);
et_content = (EditText) findViewById(R.id.et_content);
et_title = (EditText) findViewById(R.id.et_title);
btn_send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
GMailSender sender = new GMailSender("User_id@gmail.com", "Password"); // SUBSTITUTE
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// HERE
try
{
sender.sendMail(et_title.getText().toString(), // subject.getText().toString(),
et_content.getText().toString(), // body.getText().toString(),
"User_id@gmail.com", // from.getText().toString(),
"User_id@gmail.com" // to.getText().toString()
);
toast();
} catch (Exception e)
{
Log.e("SendMail", e.getMessage(), e);
}
}
});
}
public void toast()
{
Toast.makeText(this, "전송되었습니다.", Toast.LENGTH_SHORT).show();
finish();
}
}
|
cs |
=====================================================================================
[ GMailSender.java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
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");
}
}
}
|
cs |
=====================================================================================
* gmail 2단계 인증하여 비밀번호 등록하는 법
1. https://myaccount.google.com/
2. https://accounts.google.com/b/0/SmsAuthConfig?hl=ko
> 설정 시작
3. 재로그인
4. https://accounts.google.com/b/0/SmsAuthSettings?Setup=1
> 전화번호 입력 후 코드 전송
> 인증코드 입력
5. https://security.google.com/settings/security/apppasswords?pli=1
> 기기선택과 앱(MAIL) 선택 후 생성
6. 생성된 비밀번호를 위 소스의 pwd란에 입력한다.
반응형
'프로그래밍' 카테고리의 다른 글
[Android] Intent를 이용하여 GMail 보내기 (0) | 2019.04.23 |
---|---|
[C언어] scanf_s(%d %c %d) (0) | 2016.04.23 |
[Andoird] 메테리얼 탭 아이콘적용하기(Material Sliding Tabs With Icons) (0) | 2015.10.11 |
[Android] 메테리얼 탭(Material Sliding Tabs) (0) | 2015.10.11 |
[Android] 메테리얼 디자인 색상 미리보기(MaterialPalette) (0) | 2015.10.11 |