源代码:public class SendEmail {
public static void main(String[] args) { // 收件人电子邮箱 String to = "a@xx.com.cn";
// 发件人电子邮箱 String from = "b@xx.com.cn";
// 指定发送邮件的主机为 localhost String host = "hikml.xxxx.com.cn";
// 获取系统属性 Properties properties = System.getProperties();
// 设置邮件服务器 properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", "25"); properties.setProperty("mail.transport.protocol", "smtp"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.user", "a@xx.com.cn"); properties.put("mail.smtp.password", "*******");
// 获取默认session对象 Session session = Session.getDefaultInstance(properties); session.setDebug(true); try { // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from));
// Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: 头部头字段 message.setSubject("This is the Subject Line!");
// 设置消息体 message.setText("This is actual message");
// 发送消息 Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } }
}
就是网上的一个初级教程,但是一直调不通。hikml.xxxx.com.cn这是公司内部邮件服务器。能平通,使用25端口能telnet连通。 错误: javax.mail.AuthenticationFailedException: failed to connect, no password specified? at javax.mail.Service.connect(Service.java:329) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at com.support.mail.SendEmail.main(SendEmail.java:70)
需要授权。