我是Java世界的新手,并试图创建一个小应用程序来发送电子邮件到我的Gmail帐户。但我收到了一些我试图解决的错误,但失败了。我的完整代码如下:
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
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 SendMyEmail {
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("Hello");
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String msgBody = "...";
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("tinyNina@gmail.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("tinyNina@gmail.com", "Mr. User"));
msg.setSubject("Your Example.com account has been activated");
msg.setText(msgBody);
Transport.send(msg);
System.out.println("Done");
} catch (AddressException e) {
System.out.println(e);
// ...
} catch (MessagingException e) {
System.out.println(e);
// ...
}
我收到以下错误:
Hello
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
}
}
有人可以告诉我我在做错什么吗?
注意:我尝试了很多方法然后找到了一个谷歌链接https://cloud.google.com/appengine/docs/java/mail/,然后我尝试了这个错误。