C
Camel
Hi,
When I ran my email sending program, I got AuthenticationFailedException. I
am very sure my username and password are correct. What else could be the
problem?
Thank you very much in advance.
-------------code begin-----------------
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.io.*;
import java.util.*;
class MailTest{
private String
sender,recipient1,mailHost,mailPort,mailProtocol,mailUser,mailPassword,mailContent,mailSubject;
Authenticator auth;
public MailTest() {
sender = "(e-mail address removed)";
recipient1 = (e-mail address removed);
mailHost = "myhost.com";
mailPort = "26";
mailProtocol = "smtp";
mailUser = "username";
mailPassword = "mypass";
mailSubject = "Hello";
mailContent = "How are you";
auth = new MyAuthenticator();
}
public void sendMail() {
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", mailProtocol);
props.put("mail.transport.protocol", mailProtocol);
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.port", mailPort);
props.put("mail.smtp.user", mailUser);
props.put("mail.smtp.password", mailPassword);
props.put( "mail.smtp.auth", "true" );
Session mailSession = Session.getInstance(props, auth);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent(mailContent, "text/plain");
message.setSubject(mailSubject);
message.setSender(new InternetAddress(sender));
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(recipient1));
transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch(Exception ex) {
System.out.println(ex.toString());
}
}
public static void main(String[] args) {
MailTest mail = new MailTest();
mail.sendMail();
}
class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailUser, mailPassword);
}
}
}
When I ran my email sending program, I got AuthenticationFailedException. I
am very sure my username and password are correct. What else could be the
problem?
Thank you very much in advance.
-------------code begin-----------------
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.io.*;
import java.util.*;
class MailTest{
private String
sender,recipient1,mailHost,mailPort,mailProtocol,mailUser,mailPassword,mailContent,mailSubject;
Authenticator auth;
public MailTest() {
sender = "(e-mail address removed)";
recipient1 = (e-mail address removed);
mailHost = "myhost.com";
mailPort = "26";
mailProtocol = "smtp";
mailUser = "username";
mailPassword = "mypass";
mailSubject = "Hello";
mailContent = "How are you";
auth = new MyAuthenticator();
}
public void sendMail() {
try {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", mailProtocol);
props.put("mail.transport.protocol", mailProtocol);
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.port", mailPort);
props.put("mail.smtp.user", mailUser);
props.put("mail.smtp.password", mailPassword);
props.put( "mail.smtp.auth", "true" );
Session mailSession = Session.getInstance(props, auth);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent(mailContent, "text/plain");
message.setSubject(mailSubject);
message.setSender(new InternetAddress(sender));
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(recipient1));
transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch(Exception ex) {
System.out.println(ex.toString());
}
}
public static void main(String[] args) {
MailTest mail = new MailTest();
mail.sendMail();
}
class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailUser, mailPassword);
}
}
}