【发布时间】:2017-03-02 17:08:58
【问题描述】:
我尝试从域发送邮件,但遇到了一些错误。
代码:
package SendingClass;
import java.util.*;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail
{
public static void main(String [] args)
{
String host="chnmail.hcl.com";
final String user="allwinjayson.m@hcl.com";
final String password="*******";
String to="allwinjayson.m@hcl.com";
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
//new
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,password);
}
});
//Compose the message
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Praise the Lord");
message.setText("This is simple program of sending email using JavaMail API");
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}
错误:
javax.mail.AuthenticationFailedException:服务器和客户端均不支持身份验证机制
在 com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
在 javax.mail.Service.connect(Service.java:313)
在 javax.mail.Service.connect(Service.java:172)
在 javax.mail.Service.connect(Service.java:121)
在 javax.mail.Transport.send0(Transport.java:190)
在 javax.mail.Transport.send(Transport.java:120)
在 SendingClass.SendEmail.main(SendEmail.java:63)
【问题讨论】:
-
客户端和服务器都支持的认证机制吗?尝试使用连接方法而不使用用户名和密码
标签: java jakarta-mail