I'm trying to send an email to myself using the javamail api. I've followed the code correctly that I found online but I can't seem to get it to work. The error I'm getting is:
Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
Does that mean my computer doesn't support authentication?
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 I开发者_如何学GonternetAddress(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;
}
}
}
When I send emails from this server, what I have to do is, login via ssh to the server (login.server.com etc.) then I can send emails from the mail server (smtp.server.com etc.). I need to mimic this in java
RESOLVED: Use the SMTPSSLTransport class
I had the same problem and for once did not find the answer on stackoverflow. @JPC's answer however provides a (very small) clue.
Starting with javamail 1.4.3 (? see refs), javamail will refuse to send plain text credentials over an unencrypted channel. So enabling TLS transport is one solution. Another solution is to try another authentication method, if supported by the server, such as NTLM or SASL. Another solution is to revert to JavaMail 1.4.1, which has no problem sending credentials in the clear. The advantage to this is that your code does not have to change. The code in the question would work without a problem. The disadvantage is obviously that you are sending credentials in the clear, but if your Exchange admin allows it...In my case, the mail server does not support TLS, so I had no other choice.
References:
- Release notes: http://www.oracle.com/technetwork/java/javamail145changes-1562616.html
- JavaMail versions download: http://www.oracle.com/technetwork/java/javamail/javamail145-1904579.html
- The clue: https://confluence.atlassian.com/display/JIRAKB/No+authentication+mechansims+supported+by+both+server+and+client+when+Configuring+Microsoft+Exchange+SMTP+Server
Make sure you have an smtp server (smtp.examplehost.com) to actually send the email. Make sure your smtp server allows "myemail@email.com" to send out emails.
These are some of the security checks put in place by standard smtp servers.
This is probably you knock to wrong port. I suppose you're using IMAP protocol: For IMAP over SSL you have to connect to Port 993
SMTPSSLTransport solved the problem
精彩评论