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 {
mavenCentral()
}
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
try{
br = new BufferedReader(new FileReader(path));
String currentLine;
while((currentLine = br.readLine()) != null) {
contents+=currentLine;
}
br.close();
}
catch (IOException e) {
System.out.println("Could not open email file.");
e.printStackTrace();
}
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));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println("Email has been sent!");
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}
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
ay
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.