class MyAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication ( "username ", "psw ");
}
}
username 和psw是一个电子邮件的用户名和密码吗?我试了一下不好用啊,达人给指点一下
------解决方案--------------------
J2EE需要下载mail和smtp的包。去java的官方网上下载。
下面贴上一段代码,但是没有用户名和密码验证。
Properties props = System.getProperties();
// SMTP服务器地址指定
props.put( "mail.smtp.host ", "aaaa@bbb.com ");
Session session=Session.getDefaultInstance(props,null);
MimeMessage mimeMessage = new MimeMessage(session);
String encodedBody = MimeUtility.encodeText(mailText, "iso-2022-jp ", "B ");
mimeMessage.setContent(encodedBody, "text/plain; charset=iso-2022-jp ");
//发信人的地址和用户名
mimeMessage.setFrom(new InternetAddress(senderUrl,senderName, "iso-2022-jp "));
//收信者的地址
mimeMessage.setRecipients(Message.RecipientType.TO, mailAddress);
//信的标题
mimeMessage.setSubject(mailTitle, "iso-2022-jp ");
//信的内容
mimeMessage.setText(mailText);
//信的格式
mimeMessage.setHeader( "Content-Type ", "text/plain ");
//发信日
mimeMessage.setSentDate(new Date());
//发信
Transport.send(mimeMessage);