Sending email using Gradle

2019-07-06 21:31发布

I have written a task (actually copied from Internet), it sends email to given email. But when I run it, I get java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage exception. I have included compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.1' in dependencies but still getting this. Here is the task

apply plugin: 'com.android.application'

class MailSender extends DefaultTask {

    @TaskAction
    def sendMail(){
        def mailParams = [
                mailhost: "smtp.gmail.com",
                mailport:"465",
                subject: "Email Recieved",
                messagemimetype: "text/plain",
                user: "allaudinqazi@gmail.com",
                password:"", // 
                enableStartTLS:"true",
                ssl:"true"
        ]
        ant.mail (mailParams) {
            from (address:'allaudinqazi@gmail.com')
            to (address:'allaudinqazi@gmail.com')
        }
    }
}

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.1'

    defaultConfig {
        applicationId "uk.org.sportscotland.app"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 3
        versionName "1.1.1"
    }

    dexOptions {
        javaMaxHeapSize "2g"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile files('libs/org.apache.http.legacy.jar')
    compile fileTree(dir: 'libs', include: 'Parse-1.7.0.jar')
    compile 'com.koushikdutta.ion:ion:2.+'
    compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.1'
}

ext {
    fileName = "SSV01"
}

task copyToDropbox(type: Copy){
    from "build/outputs/apk/app-debug.apk"
    into "F:/folder/Dropbox/Builds/SS"
    rename {
        fileName + ".apk"
    }
}

task mail(type: MailSender){}

标签: gradle task
4条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-06 21:44

Reading the docs for the mail task it says

Note: This task may depend on external libraries that are not included in the Ant distribution. See Library Dependencies for more information.

I seem to remember that ant was a bit wierd like this and expected you to drop jar files into %ANT_HOME%/lib. So, I think the mail task will need to be loaded by the same classloader as the mail/activation jars. To me that means two options:

  1. Add jars to ant's classloader as specified here. This approach feels very hacky to me and will likely break in Java 9 when URLClassloader is NOT guaranteed.

  2. Use ant.taskdef to define another mail task (eg mail2) with all the required jars in the classpath. This would be my preferred approach.

Eg:

configurations { antMail }
dependencies {
   antMail 'ant:ant-javamail:1.6.5'
   antMail 'javax.activation:activation:1.1.1'
   antMail 'javax.mail:mail:1.4.7'
}
ant.taskdef(
    name: 'mail2',
    classname: 'org.apache.tools.ant.taskdefs.email.EmailTask',
    classpath: configurations.antMail.asPath
)
ant.mail2(mailParams) { ... }
查看更多
别忘想泡老子
3楼-- · 2019-07-06 21:48

Looks like you need to put

classpath 'javax.mail:javax.mail-api:1.5.5'

in your buildscript dependencies

查看更多
成全新的幸福
4楼-- · 2019-07-06 21:55

You have incorrectly added javax.mail to the compile configuration. Since this is needed at build time you will need to add it to the buildscript dependencies.

Eg:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'javax.mail:javax.mail-api:1.5.1'
    }
}

More info here

查看更多
Fickle 薄情
5楼-- · 2019-07-06 22:04

It seems groupId change by the time (javax >> javax.mail): You need to replace by:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'javax.mail:javax.mail-api:1.5.6'
    }
}
查看更多
登录 后发表回答