Kotlin

[Kotlin] Android 12 이상 메일 보내는 방법

eulBlue 2023. 8. 7. 23:54

📱테스트 환경

Samsung Galaxy Android 13 • Android 10


😢 내가 겪은 문제

Android 10 에서 테스트 할 때는 문제가 없었는데 , 13 에서 보내려고 하니까 메일 전송하기 시스템 모달창이 나오지 않았다.

10에서는 되는데 13에서 안되니까 , 너무 답답해서 찾아봤더니

android.intent.action.SENDTO

Android 12 부터 위의 코드를 넣어야 한다고 하더라.

<queries>
    <intent>
        <action android:name="android.intent.action.SENDTO" />

        <data android:scheme="*" />
    </intent>
</queries>

 

이메일을 보내기 위해 정보를 찾아보니까 두 가지를 찾아 볼 수 있었다.

1. android.intent.action.SEND

or

2. android.intent.action.SENDTO

1번보통 데이터를 공유하거나 다른 앱으로 보내는 데 사용하고

 

2번은 특정 유형의 데이터 ( mailto 형식 이메일 주소 ) 를 처리하도록 다른 앱에 작업을 넘겨줄 때 사용한다고 한다.

 

사실 어떤걸 사용해도 상관은 없는데 찾아보니 SENDTO 를 추천해줬었다.

밑에는 내가 메일 보내기를 사용할 때 사용한 코드이다.

val emailAddress = "사용할 이메일"
val title = "타이틀 내용"

val intent = Intent(Intent.ACTION_SENDTO) // 메일 전송 설정
    .apply {
        type = "text/plain" // 데이터 타입 설정
        data = Uri.parse("mailto:") // 이메일 앱에서만 인텐트 처리되도록 설정

        putExtra(Intent.EXTRA_EMAIL, arrayOf<String>(emailAddress)) // 메일 수신 주소 목록
        putExtra(Intent.EXTRA_SUBJECT, title) // 메일 제목 설정
        putExtra(Intent.EXTRA_TEXT, "") // 메일 본문 설정
    }
if (intent.resolveActivity(packageManager) != null) {
    startActivity(Intent.createChooser(intent, "메일 전송하기"))
} else {
    Toast.makeText(this, "메일을 전송할 수 없습니다", Toast.LENGTH_LONG).show()
}

 

 

 


https://developer.android.com/guide/components/intents-common?hl=ko

 

공통 인텐트  |  Android 개발자  |  Android Developers

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object. This type of intent is called an implicit intent because…

developer.android.com