获取“javax.mail.AuthenticationFailedException:连接失败”错误 [英] Getting the "javax.mail.AuthenticationFailedException: failed to connect" Error
问题描述
我想要做什么:发送带有smtp.live.com主机的基本电子邮件,以使用以下代码向使用Java程序的人发送电子邮件:
What I Want To Do: Send a basic email with the smtp.live.com host to email to someone using a Java Program using this code below:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPTransport;
public class emailTest {
public static void main(String[] args) {
String to = "example@live.com";
String from = "example@live.com";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.live.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Java");
message.setText("Java");
Transport trans = session.getTransport("smtp");
trans.connect("smtp.live.com", 587, "example@live.com", "password");
Transport.send(message);
System.out.println("Message Sent!");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
问题:当我尝试发送电子邮件时,我收到此错误。
The Problem: When I try to send the email, I get this error.
javax.mail.AuthenticationFailedException: failed to connect
at javax.mail.Service.connect(Service.java:322)
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 emailTest.main(emailTest.java:39)
我的问题: / strong>我只是想知道我做错了什么,因为我花了几个小时试图找到不同的方法来解决这个问题。
My Question: I just want to know what I'm doing wrong as I've spent a few hours trying to find different ways on how to fix this.
推荐答案
好的,我很快就想出了我做错了什么。我不能使用Transport.send(message)发送消息。
Ok, I soon figured out what I was doing wrong. I can't use Transport.send(message) to send the message.
需要更改:相反,我必须使用trans.sendMessage(message,message.getAllRecipients())。
Changes Needed: Instead, I have to use trans.sendMessage(message, message.getAllRecipients()).
这是固定代码:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPTransport;
public class emailTest {
public static void main(String[] args) {
String to = "example@live.com";
String from = "example@live.com";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.live.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Java");
message.setText("Java");
Transport trans = session.getTransport("smtp");
trans.connect("smtp.live.com", 587, "example@live.com", "password");
trans.sendMessage(message, message.getAllRecipients());
System.out.println("Message Sent!");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
这篇关于获取“javax.mail.AuthenticationFailedException:连接失败”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!