Я пытаюсь отправить электронное письмо себе, используя javamail api. Я правильно следовал коду, который нашел в Интернете, но не могу заставить его работать. Я получаю ошибку:
Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
Значит ли это, что мой компьютер не поддерживает аутентификацию?
public class Emails
{
public static void main(String[] args) throws MessagingException
{
Emails e = new Emails();
e.sendMail();
}
public void sendMail() throws MessagingException
{
boolean debug = false;
String subject = "Testing";
String message = "Testing Message";
String from = "myemail@email.com";
String[] recipients = {"myemail@email.com"};
// create some properties and get the default Session
Session session = getSession();
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
private Session getSession()
{
Authenticator authenticator = new Authenticator();
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", "smtp.examplehost.com");
properties.setProperty("mail.smtp.port", "25");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator
{
private PasswordAuthentication authentication;
public Authenticator()
{
String username = "myusername";
String password = "mypassword";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}
}
}
Когда я отправляю электронные письма с этого сервера, мне нужно войти в систему через ssh на сервер (login.server.com и т. Д.), Затем я могу отправлять электронные письма с почтового сервера (smtp.server.com и т. Д. ). Мне нужно имитировать это в Java
РАЗРЕШЕНО: Используйте класс SMTPSSLTransport