0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

KotlinでJavamail4android(添付ファイルを添えて)

Posted at

きっかけ

よく使うので備忘録

Gradle

    //javamail
    implementation 'com.sun.mail:android-mail:1.6.7'
    implementation 'com.sun.mail:android-activation:1.6.7'

コード

private fun sendMail(
        smtpServer: String,     //SMTPサーバ
        srcMailAddress: String, //自分のメールアドレス
        password: String,       //メールアドレスのパスワード
        dstMailAddress: String, //相手先のメールアドレス
        displayName: String,    //相手先の表示名
        subject: String,        //メール件名
        body: String,           //メール本文
        attachedPath: String    //添付ファイルのパス
    ) {
        thread {
            val properties = Properties()
            properties["mail.smtp.host"] = smtpServer
            properties["mail.smtp.auth"] = "true"
            properties["mail.smtp.port"] = "465"
            properties["mail.smtp.socketFactory.post"] = "465"
            properties["mail.smtp.socketFactory.class"] = "javax.net.ssl.SSLSocketFactory"
            properties["mail.smtp.connectiontimeout"] = 2000
            properties["mail.smtp.timeout"] = 2000

            val message: MimeMessage =
                MimeMessage(Session.getDefaultInstance(properties, object : Authenticator() {
                    override fun getPasswordAuthentication(): PasswordAuthentication {
                        return PasswordAuthentication(srcMailAddress, password)
                    }
                }))
            message.setFrom(InternetAddress(dstMailAddress, displayName))
            message.setRecipient(
                MimeMessage.RecipientType.TO,
                InternetAddress(dstMailAddress, displayName)
            )
            message.subject = subject

            // 本文
            val multipart = MimeMultipart("mixed")

            // 本文テキスト
            val textPart = MimeBodyPart()
            textPart.setText(body, "UTF-8")

            // 添付ファイル
            val attachedPart = MimeBodyPart()
            val myFile: File = File(attachedPath)
            // 実ファイルを利用する場合は実ファイルでDataSourceを作成
            val dataSource: DataSource = FileDataSource(myFile)
            attachedPart.dataHandler = DataHandler(dataSource)
            attachedPart.fileName = myFile.name

            multipart.addBodyPart(textPart)
            multipart.addBodyPart(attachedPart)

            message.setHeader("X-Mailer", "kotlin de mail ver. 0.9b [ja]")
            message.sentDate = Date()
            message.setContent(multipart)
            Transport.send(message)
        }
    }

メモ

電波悪かったりしたら,エラー吐くので,そのあたりの処理を忘れない。

  • SMTPSendFailedException
  • MessagingException

がよく出ると思います。

免責

javaから,AndroidStudioの機能を用いてkotlinへ変換したので,動作保証は致しかねます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?