使用java发送邮件
我正在尝试从域发送邮件,但遇到一些错误
代码:
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: No authentication mechansims supported by both server and client at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669) at javax.mail.Service.connect(Service.java:313) at javax.mail.Service.connect(Service.java:172) at javax.mail.Service.connect(Service.java:121) at javax.mail.Transport.send0(Transport.java:190) at javax.mail.Transport.send(Transport.java:120) at SendingClass.SendEmail.main(SendEmail.java:63)
# 1 楼答案
这有更好的表现吗?这是我的一个Java应用程序中的一个简化示例,希望我没有引入copypaste语法错误。它看起来与您的代码类似,但没有多少额外的smtp道具
# 2 楼答案
在服务器允许您登录之前,您很可能需要启用SSL/TLS。根据服务器的要求,将属性^{} 或^{} 设置为
true
此外,您还需要清理其中一些common JavaMail mistakes