gradle - 使用 Gradle 发送电子邮件

标签 gradle task

我写了一个任务(实际上是从 Internet 复制的),它将电子邮件发送到给定的电子邮件。但是当我运行它时,我得到 java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage 异常。我在依赖项中包含了 compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.1' 但仍然得到这个。这是任务

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){}

最佳答案

您错误地将 javax.mail 添加到 compile 配置中。由于这是在构建时需要的,因此您需要将其添加到 buildscript 依赖项中。

例如:

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

更多信息 here

关于gradle - 使用 Gradle 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293194/

相关文章:

c# - 正在为 C# 中的新任务处理免费资源

java - 如何修复 "error: package android.support.v7.widget does not exist"问题?

gradle - 如何在 Gradle 中按特殊字符 "\"拆分字符串

groovy - 从没有 Runtime.exec() 的 groovy 调用 gradle 任务

c# - 在 C# 中从已运行的任务中初始化新任务

c# - 在 ContinueWith() block 中使用 await

C# 任务忽略取消超时

android - 将特定 Artifact 添加到Gradle项目(Android)

spring-boot - Spring Boot + Kotlin + Gradle - 错误 : Main method not found in class

Android low memory killer & application back stack