我尝试从域发送邮件但收到一些错误。
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)
答案 0 :(得分:1)
在您的服务器允许您登录之前,您很可能需要启用SSL / TLS。将属性mail.smtp.ssl.enable
或mail.smtp.starttls.enable
设置为true
,具体取决于服务器的要求。
此外,您还需要清理其中一些common JavaMail mistakes。
答案 1 :(得分:-1)
这表现更好吗?这是我的一个Java应用程序的简化示例,希望我没有介绍一个copypaste语法错误。它看起来与您的代码类似,但几乎没有额外的smtp道具。
Properties prop = new Properties();
prop.put("mail.smtp.allow8bitmime", "true");
prop.put("mail.smtp.timeout", "60000");
prop.put("mail.smtp.connectiontimeout", "60000");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "465");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.from", "myname@gmail.com");
final String username = "myname";
final String pwd = "mypwd";
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, pwd);
}
}
);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress( (String)prop.get("mail.smtp.from") ));
((MimeMessage)msg).setSubject("My email title ÅÄÖ", "UTF-8");
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("other@world.com", false));
Multipart mp = new MimeMultipart();
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText("This is a body text ÅÄÖ", "UTF-8");
mp.addBodyPart(mbp1);
msg.setContent(mp);
Transport tr = session.getTransport("smtp");
tr.connect();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();