Hey gang!
Okay, I know this question has been asked many times before but here we go again. I've sifted through the forums and Googled myself blind but I still can't figure out why this won't work. I'm using Java Mail to send a message from a desktop application. I have extracted the part that sends the message to isolate it into it's own class and eliminate any other possible interference. When the code executes I get the classic javax.mail.AuthenticationFailedException along with numerous references to various points of code. I have checked the username and password by both logging in using another mail client and using it to log on to the domain directly. I am able to connect to the server using telnet. In general, the things I've read suggest that the problem is with how I authenticate but I can't seem to find the right combination. Any assistance would be appreciated!!
package simplemailtest;
import java.io.*;
import java.net.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;
import java.io.*;
import java.sql.*;
import javax.mail.PasswordAuthentication;
public class Main {
public static void main(String[] args) {
try
{
String server = "YOUR MAIL SERVER HERE";
String from = "SENDERS EMAIL";
String to = "RECEIVERS EMAIL";
String cc = "CC EMAIL";
String subject = "I just wish this would work!";
String username = "YOUR USERNAME";
String password = "YOUR PASSWORD";
//String fileAttachment = "/home/user/file.txt";
String messageBody =
"This email is sent automatically by a computer. Do NOT reply to it.\n\n" +
"Java Vendor: " + System.getProperty("java.vendor") + "\n" +
"Java vendor URL: " + System.getProperty("java.vendor.url" ) + "\n" +
"Java Version: " + System.getProperty("java.version" ) + "\n" +
"Operating System Architecture: " + System.getProperty("os.arch") + "\n" +
"Operating System Name: " + System.getProperty("os.name") + "\n" +
"Operating System Version: " + System.getProperty("os.version") +
"\n\n\n\n---------------------------\nEnd Of Message";
String messageBody2 =
"This is the second part";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", server);
props.put("mail.smtp.auth", "true");
new PasswordAuthentication(username, password);
// Get session
Session sess = Session.getInstance(props);
// Define message
MimeMessage message = new MimeMessage(sess);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
message.setSubject(subject);
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//fill message
messageBodyPart.setText(messageBody);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody2);
Multipart multipart2 = new MimeMultipart();
multipart2.addBodyPart(messageBodyPart);
//DataSource source = new FileDataSource(fileAttachment);
//messageBodyPart.setDataHandler(new DataHandler(source));
//messageBodyPart.setFileName(new File(fileAttachment).getName());
multipart2.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send(message);
System.out.println("Message sent successfully");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}