我想使用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