Unable to send mail through java application when ran with Gradle script

I am using a Gradle script to run a java program with the Java Mail API. When run in eclipse, the program sends the email and runs successfully. However, when I run the exact same program with the Gradle script I get a java exception.

Here is the build.gradle file:

apply plugin: "java"

sourceSets.main.java.srcDirs = ['src/java']

repositories {
dependencies {
   compile 'javax.mail:mail:1.4.1'

task runApp(type: JavaExec, dependsOn: classes){
	main 'MailPlugin'
	classpath sourceSets.main.runtimeClasspath
	args "email@host.com", "1", "email.txt"

Here is the java program:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MailPlugin {

	public static void main(String[] args) {
		String from = "myEmail@host.com";     // Sender's email
		String to = args[0];						// Recepient's email
		String user = "username";               		// Sender's username
		String pass = "password";              				// Password for sender's email
		String version = args[1];					// Branch version
		String path = args[2];						// Path to email message text file
		String subject = "Server Status Update";    // Subject line of email
		String contents = "";                       // Contents of email
		BufferedReader br = null;					// BufferReader for reading from message file

		//Get contents of email from file
			br = new BufferedReader(new FileReader(path));
			String currentLine;
			while((currentLine = br.readLine()) != null) {
		catch (IOException e) {
			System.out.println("Could not open email file.");
		sendEmail(from, pass, to, subject, contents, user);
	private static void sendEmail(String from, final String pass, String to, String subject, String body, final String user) {
		Properties props = System.getProperties();
		Session session = Session.getDefaultInstance(props);
		String host = "smtp.host.com";
		props.put("mail.smtp.host", host);
		session = Session.getInstance(props,
    	        new javax.mail.Authenticator() {
    	            protected PasswordAuthentication getPasswordAuthentication() {
    	                return new PasswordAuthentication(user,pass);
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
				   new InternetAddress(to));
			Transport transport = session.getTransport("smtp");
			transport.connect(host, from, pass);
			System.out.println("Email has been sent!");
		catch (AddressException ae) {
		catch (MessagingException me) {

I have run other Java programs successfully with this Gradle script, and this java program runs just fine when ran though eclipse. I’m not really sure what the cause of this issue would be.

Here is the error I am getting:

javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1294)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:635)
        at MailPlugin.sendEmail(MailPlugin.java:62)
        at MailPlugin.main(MailPlugin.java:35)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to rel

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1145)
        ... 3 more

For whatever reason it thinks I am not giving it a valid address. I have tried many diffrent emails for the ‘to’ field, and the ‘from feild’, but had no luck. Not sure what else to try at this point. Any advice would be greatly appreciated.

What’s your OS?

Could your println the arguments you’re getting in your application to see if they’re different between Eclipse and Gradle?

If you run with --info, I think Gradle will print the command line it’s using.

Set the “mail.debug” system property to true. This will show more logging from smtp.