为了账号安全,请及时绑定邮箱和手机立即绑定

java Mail 运行javamail 入门级代码出错

java Mail 运行javamail 入门级代码出错

慕沐林林 2018-12-06 13:32:53
源代码: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)
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

需要授权。

查看完整回答
反对 回复 2018-12-16
  • 1 回答
  • 0 关注
  • 554 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号