这段非常简单的JavaMail程序怎么会有错?

姓庄滴 2006-05-18 10:38:49
Properties mailpro = new Properties();
mailpro.setProperty("mail.smtp.host", "smtp.163.com");
Session session = Session.getDefaultInstance(mailpro);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("lizongbo@gmail.com"));
msg.setRecipient(Message.RecipientType.TO,new InternetAddress("zhuangnet@163.com"));
msg.setSubject("test");
msg.setText("this is a test");
Transport.send(msg);
System.out.println("send succeed!");

以上程序段,是用来发送邮件的.
我是在网上搜的例子,但我运行,没有编译错误.但不能成功发送邮件.怎么回事?
...全文
168 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rickhunterchen 2006-05-19
  • 打赏
  • 举报
回复
如果你利用外网的邮件服务器发邮件,就肯定要身份验证,要不163知道你是谁呀。

sole_lodestar 2006-05-19
  • 打赏
  • 举报
回复
建议楼主在这里搜一下邮件相关的主题
linuxbing 2006-05-19
  • 打赏
  • 举报
回复
需要向服务器提供你的用户名和密码
mailpro.put("mail.smtp.auth","true");
Session session = Session.getInstance(mailpro,new Authenticator(username,password){
private String userName,password;

/**
* To store a userName and pasword to the authenticator
* @param userName user's name
* @param password user's passwored
*/
public SimpleAuthenticator(String userName,String password){
this.userName = userName;
this.password = password;
}

/**
* Overide Authenticator method
*/
protected PasswordAuthentication getPasswordAuthentication(){
PasswordAuthentication pass = new PasswordAuthentication(userName,password);
return pass;
}
})

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧