package jmail;
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 HtmlJavaSend {
public void sendHtmlEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message) throws AddressException,
MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.man.com", host);
properties.put("mail.25", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust","mail.man.com");
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// set plain text message
msg.setContent(message, "text/html");
// sends the e-mail
Transport.send(msg);
}
public static void main(String[] args) {
// SMTP server information
String host = "mail.man.com";
String port = "25";
String mailFrom = "admin@man.com";
String password = "Man";
// outgoing message information
String mailTo = "ji@man.com";
String subject = "Hello my friend";
// message contains HTML markups
String message = "<i>Greetings!</i><br>";
message += "<b>Wish you a nice day!</b><br>";
message += "<font color=red>Duke</font>";
HtmlJavaSend mailer = new HtmlJavaSend();
try {
mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,
subject, message);
System.out.println("Email sent.");
} catch (Exception ex) {
System.out.println("Failed to sent email.");
ex.printStackTrace();
}
} }
错误是:
错误运行:无法发送电子邮件。 com.sun.mail.util.MailConnectException:无法连接到主机, port:localhost,25;超时-1;嵌套异常是: java.net.ConnectException:拒绝连接:connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) 在javax.mail.Service.connect(Service.java:388)at javax.mail.Service.connect(Service.java:246)at javax.mail.Service.connect(Service.java:195)at javax.mail.Transport.send0(Transport.java:254)at javax.mail.Transport.send(Transport.java:124)at jmail.HtmlJavaSend.sendHtmlEmail(HtmlJavaSend.java:62)at jmail.HtmlJavaSend.main(HtmlJavaSend.java:85)引起: java.net.ConnectException:拒绝连接:connect at java.net.DualStackPlainSocketImpl.connect0(Native Method)at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)at java.net.Socket.connect(Socket.java:579)at java.net.Socket.connect(Socket.java:528)at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331) 在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) 在 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066) ... 8更多成功(总时间:1秒)
答案 0 :(得分:3)
这里有错误:
properties.put("mail.man.com", host);
properties.put("25", port);
应该是:
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
答案 1 :(得分:0)
根据:
properties.put("mail.25", port);
您正在连接到提供明文 SMTP 的端口。这得到了您在使用 telnet 时接收 202 service ready message
消息的观察的支持。
但是您还设置了使用 TLS 的属性:
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust","mail.man.com");
如果您使用端口 25,请不要启用 TLS,或者在启用 TLS 的情况下使用端口 465
(使用 TLS 的 SMTP 的标准端口)。