質問編集履歴
1
内容の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -53,7 +53,64 @@
|
|
53
53
|
できるのでしたら、そちらの方法もご教示頂けると幸いです。
|
54
54
|
宜しくお願い致します。
|
55
55
|
|
56
|
+
### 試してみたこと
|
57
|
+
[https://medium.com/@himanshu017/using-javamail-api-in-android-with-kotlin-887410406566](https://medium.com/@himanshu017/using-javamail-api-in-android-with-kotlin-887410406566)
|
58
|
+
上記のWebページを参考にして、下記のようなコードを書きました。
|
56
59
|
|
60
|
+
```Kotlin
|
61
|
+
|
62
|
+
bt_soshin.setOnClickListener {
|
63
|
+
|
64
|
+
Thread {
|
65
|
+
val properties = Properties()
|
66
|
+
properties.put("mail.smtp.host", "smtp.gmail.com")
|
67
|
+
properties.put("mail.smtp.socketFactory.port", "465")
|
68
|
+
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
|
69
|
+
properties.put("mail.smtp.auth", "true")
|
70
|
+
properties.put("mail.smtp.port", "465")
|
71
|
+
val session = Session.getDefaultInstance(properties,
|
72
|
+
object : Authenticator() {
|
73
|
+
override fun getPasswordAuthentication(): PasswordAuthentication {
|
74
|
+
return PasswordAuthentication("my account name", "password")
|
75
|
+
}
|
76
|
+
}
|
77
|
+
)
|
78
|
+
val mimeMessage = MimeMessage(session)
|
79
|
+
|
80
|
+
try {
|
81
|
+
mimeMessage.setRecipients(Message.RecipientType.TO, "xxxx@gmail.com")
|
82
|
+
mimeMessage.setFrom(InternetAddress("yyyy@gmail.com"))
|
83
|
+
mimeMessage.setSubject("テスト", "iso-2022-jp")
|
84
|
+
mimeMessage.setText(et_soshin_comment.text.toString(), "iso-2022-jp")
|
85
|
+
mimeMessage.saveChanges()
|
86
|
+
Transport.send(mimeMessage)
|
87
|
+
} catch (e: MessagingException) {
|
88
|
+
Log.e("MessageException!!", e.toString())
|
89
|
+
}
|
90
|
+
}.start()
|
91
|
+
|
92
|
+
finish()
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
このコードで実行してみたら、エラー文が以下のように変わりました。
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
|
104
|
+
E/MessageException!!: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
|
105
|
+
535 5.7.8 https://support.google.com/mail/?p=BadCredentials q13sm19381323pfk.8 - gsmtp
|
106
|
+
|
107
|
+
```
|
108
|
+
|
109
|
+
[リンク先の記事](https://support.google.com/mail/answer/7126229?p=BadCredentials&visit_id=637279636406320529-2327953086&rd=2#cantsignin)を読んで、パスワードが正しいことを確認し、設定したグーグルアカウントの「安全性の低いアプリのアクセスの設定」を有効にしてまた再実行してみましたが、エラーは変わりませんでした。
|
110
|
+
|
111
|
+
ここから、どうすればアプリからメールを送信できるようになるでしょうか。
|
112
|
+
アドバイスを頂けると助かります。宜しくお願い致します。
|
113
|
+
|
57
114
|
開発環境
|
58
115
|
Windows 10
|
59
116
|
Android Studio 4.0
|