我想使用javamail api向自己發送電子郵件。我已經正確地跟蹤了我在網上找到的代碼,但似乎無法使其正常工作。我得到的錯誤是:使用javamail發送郵件時出現問題
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 = "[email protected]";
String[] recipients = {"[email protected]"};
// 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類
你可以顯示你的代碼分辨率? – 2012-02-20 22:42:29
我不確定「SMTPSSLTransport解決了這個問題」的含義。你如何指定這個類(而不是其他類)?我們遇到了同樣的問題,並且在我們使用SSL連接到Gmail時會起作用。但連接到MS Exchange服務器,它不。 – fool4jesus 2012-02-21 15:52:21
@JPC剛剛通過谷歌搜索找到了你最初得到的同樣的錯誤。當你找到解決方案時,你應該提供更多的細節。如果您可以尋求幫助,您也應該準備好提供幫助。 – Greg 2012-03-06 19:38:15