我的代码:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "myemailid@gmail.com";
final String password = "myemailpassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemailid@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("myfriedemail@ymail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"+
"\n\n No spam to my email,please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我的错误:
线程“主”java.lang.RuntimeException 中的异常:javax.mail.MessagingException:
未知 SMTP 主机:smtp.gmail.com;
嵌套异常是:
java.net.UnknownHostException: smtp.gmail.com
at Mail.SendMailTLS.main(SendMailTLS.java:56)
原因:javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
嵌套异常是:
java.net.UnknownHostException: smtp.gmail.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1970)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java :642)
在 javax.mail.Service.connect(Service.java:317)
在 javax.mail.Service.connect(Service.java:176)
在 javax.mail.Service.connect(Service.java:125)
在 javax.mail.Transport.send0(Transport.java:194)
在 javax.mail.Transport.send(Transport.java:124)
在 Mail.SendMailTLS。 main(SendMailTLS.java:51)
引起:java.net.UnknownHostException: smtp.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java: 366)
在 java.net.Socket.connect(Socket.java:525)
在 java.net.Socket.connect(Socket.java:475)
在 com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319 )
在 com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
在 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 还有 7 个
Java 结果:1
我的本地代理为 172.17.0.4:8080
,用户名为:user1,密码为:user2
Solutions pls??