全部代码如下:
package emailPack;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.net.Authenticator;
import java.util.*;
public class sendEmail {
public void sendMessage(){
Authenticator auth = new PopupAuthenticator();--------------------------------(1)
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.163.com");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "pyrgz");
mailProps.put("password", "123456789");
Session mailSession = Session.getDefaultInstance(mailProps, auth);------------(2)
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"[email protected]"));
message.setSubject("Mail Test");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("Hello World!");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
}
}为什么标记为(1)的代码报错:PopupAuthenticator can not be resolved to a type
请各位指点迷津。
package emailPack;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.net.Authenticator;
import java.util.*;
public class sendEmail {
public void sendMessage(){
Authenticator auth = new PopupAuthenticator();--------------------------------(1)
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.163.com");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "pyrgz");
mailProps.put("password", "123456789");
Session mailSession = Session.getDefaultInstance(mailProps, auth);------------(2)
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"[email protected]"));
message.setSubject("Mail Test");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("Hello World!");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
}
}为什么标记为(1)的代码报错:PopupAuthenticator can not be resolved to a type
请各位指点迷津。
你要自己写这个类然后继承Authenticator
例如这样import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* Smtp认证
* @author Administrator
*
*/
public class MyAuthenticator extends Authenticator {
String username = null;
String password = null;
/**
* SMTP身份验证
* @param username
* @param password
*/
public MyAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}
/**
* @return pass
*/
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
里面有源码
http://blog.csdn.net/voyage_mh1987/archive/2010/08/03/5784191.aspx