package mail;
import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
public class Mail {
//定义发件人、收件人、主题等
String to="";
String from="";
String host="";
String filename="";
String subject="";
//用于保存发送附件的文件名的集合
Vector file = new Vector();
/**
 * 做一个可以传发件人等参数的构造
 * @param to
 * @param from
 * @param smtpServer
 * @param subject
 */
public Mail (String to,String from,String smtpServer,String subject){
//初始化发件人、收件人、主题等
this.to=to;
this.from=from;
this.host=smtpServer;
this.subject=subject;
}
/**
 * 该方法用于收集附件名
 * @param fname
 */
public void attachfile(String fname){
file.addElement(fname);
}

/**
 * 开始发送信件的方法
 * @return
 */
public boolean startSend(){
//创建Properties对象
Properties props = System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "false"); 

//得到默认的对话对象
Session session=Session.getDefaultInstance(props, null); 
try {
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//利用枚举器方便的遍历集合
Enumeration efile=file.elements();
//检查序列中是否还有更多的对象
while(efile.hasMoreElements()){
MimeBodyPart mbp=new MimeBodyPart();
//选择出每一个附件名
filename=efile .nextElement().toString();
//得到数据源
FileDataSource fds=new FileDataSource(filename);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
//移走集合中的所有元素
file.removeAllElements();
//Multipart加入到信件
msg.setContent(mp);
//设置信件头的发送日期
msg.setSentDate(new Date());
//发送信件
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
return false;
}
return true;
}
public boolean send(){
// 创建Properties对象
Properties props = System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", host);
//得到默认的对话对象
Session session=Session.getDefaultInstance(props, null); 
try {
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setContent("Hello", "text/html;charset=gb2312");
//设置信件头的发送日期
msg.setSentDate(new Date());
//发送信件
Transport.send(msg);
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
return false;
}
return true;

}
public static void main(String args[]){

Mail m = new Mail("[email protected]","[email protected]","smtp.126.com","Test");
System.out.println("sending......");
m.send();
if(m.send()){
System.out.println("sent!");
}
else System.out.println("未发送成功!");
}
}控制台显示:
sending......
javax.mail.SendFailedException: Sending failed;
  nested exception is:
class javax.mail.MessagingException: 553 authentication is required,smtp3,wKjSjbCrUwKao6hGB4TZKQ==.57715S2 1185457050 at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at mail.Mail.send(Mail.java:111)
at mail.Mail.main(Mail.java:127)
javax.mail.MessagingException: 553 authentication is required,smtp3,wKjSjbCrUwKao6hGB4TZKQ==.57715S2 1185457050 at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
at javax.mail.Transport.send0(Transport.java:163)
at javax.mail.Transport.send(Transport.java:80)
at mail.Mail.send(Mail.java:111)
at mail.Mail.main(Mail.java:127)
javax.mail.SendFailedException: Sending failed;
  nested exception is:
class javax.mail.MessagingException: 553 authentication is required,smtp3,wKjSjbDL0Zmao6hG6MXiKQ==.57731S2 1185457051 at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at mail.Mail.send(Mail.java:111)
at mail.Mail.main(Mail.java:128)
javax.mail.MessagingException: 553 authentication is required,smtp3,wKjSjbDL0Zmao6hG6MXiKQ==.57731S2 1185457051 at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
at javax.mail.Transport.send0(Transport.java:163)
at javax.mail.Transport.send(Transport.java:80)
at mail.Mail.send(Mail.java:111)
at mail.Mail.main(Mail.java:128)
未发送成功!

解决方案 »

  1.   

    邮件服务器需要认证。改成这样
    props.put("mail.smtp.auth", "true"); 

    //得到默认的对话对象
    Session session=Session.getDefaultInstance(props, new 认证);
      

  2.   

    楼主先把上一个javamail的帖子结了~再说
      

  3.   

    结帖是一种美德~ ^o^这是javamail的验证方法中的一种
    改写Authenticator的getPasswordAuthentication()方法
    private class Authenticator extends javax.mail.Authenticator
    {
        public PasswordAuthentication getPasswordAuthentication()
        {
            String un = user;
            String pw = password;
            return new PasswordAuthentication(un, pw);
        }
    }应用这个之前需要设置
    props.put("mail.smtp.auth", "true"); 然后
    Authenticator auth = new Authenticator();
    Session mailSession = Session.getInstance(props,auth);还有一种方法是在Transport中设置
    Transport transport = mailSession.getTransport("smtp");
    transport.connect("mail.smtp.auth","username","password");
    transport.sendMessage(mimeMsg,maillist);
    transport.close();
      

  4.   

    谢谢楼上kjah,可以发了
    transport.connect( "mail.smtp.auth ", "username ", "password "); “mail.smtp.auth”应改为"smtp.163.com"或者类似的,其实写成这样就可以了transport.connect( "username ", "password "); mail.smtp.host在propperties中就设置了