package util;
import javax.mail.*;
import javax.activation.*;
import java.util.*;
import javax.mail.internet.*;
public class Mail { /**
 * 实现自动发邮件功能
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String send="[email protected]";
String get="[email protected]";
String pass="qiujintao520luod";
String host="smtp.qq.com";
     Properties props=System.getProperties();  //获取系统Properties
     props.put("mail.smtp.com",host);//设置smtp服务器
    props.put("mail.smtp.auth","true");
     SMTPAuthenticator smtpAuthenticator = new SMTPAuthenticator(send,pass); 
     Session se=Session.getDefaultInstance(props,smtpAuthenticator);
     se.setDebug(true);//查看运行时的信息
     MimeMessage mmg=new MimeMessage(se); //邮件会话新建一个消息对象
      try {
InternetAddress from=new InternetAddress(send); //发件人
mmg.setFrom(from);
InternetAddress to=new InternetAddress(get,false); //收件人
  mmg.setRecipient(Message.RecipientType.TO, to);
 mmg.setSubject("自己编写的发送邮件功能");//邮件主题
 mmg.setText("胖子真丑");
//邮件类容,可以是纯文本text/html
 mmg.saveChanges();
 System.out.println("正在发送中、、、");
 
  // smtp验证,就是你用来发邮件的邮箱用户名密码   
    //发送
 Transport.send(mmg); 
      System.out.println("邮件已顺利传送");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

 
}}package util;import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SMTPAuthenticator extends Authenticator {
private String name;
private String password;
public SMTPAuthenticator(){

}
public SMTPAuthenticator(String name,String password) {
this.name=name;
this.password=password;
}

public PasswordAuthentication getPasswordAuthentication(String name,String password) {
return new PasswordAuthentication(name,password);
}
}

解决方案 »

  1.   

    报错为:
    DEBUG: setDebug: JavaMail version 1.3.2
    正在发送中、、、
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:264)
    at javax.mail.Service.connect(Service.java:134)
    at javax.mail.Service.connect(Service.java:86)
    at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
    at javax.mail.Transport.send0(Transport.java:150)
    at javax.mail.Transport.send(Transport.java:80)
    at util.Mail.main(Mail.java:38)
      

  2.   

    应该是验证没通过。
    楼主参考下这篇blog,看看能否解决。
    http://blog.csdn.net/tianting_wx/archive/2008/06/13/2543015.aspx
      

  3.   

    可能你要发送或接受的邮件地址没有开通POP3 或STMP 协议吧。给你发个我自己可以实现的。
    public ActionForward getPassword(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    String username = request.getParameter("username");
    String from = "[email protected]";
    GpsUser gpsUser = (GpsUser) BaseDao
    .getUniqueResult("FROM GpsUser user WHERE user.username='"
    + username + "'");
    if (gpsUser != null) {
    request.setAttribute("username", gpsUser.getUsername());
    request.setAttribute("user", gpsUser);
    String subject = "过程监控系统取回密码!";
    String messageText = "您的密码是" + gpsUser.getPassword();
    String password = "jinjinray";
    Properties props = new Properties();
    //声明要通过身份验证
    props.setProperty("mail.smtp.auth", "true");
    //根据属性新建一个邮件会话
    Session s = Session.getInstance(props);
    s.setDebug(true);//可以看到后台信息
    //由邮件会话新建一个消息对象
    MimeMessage message = new MimeMessage(s);
    //设置邮件
    InternetAddress to_from = new InternetAddress(from);
    //设置发件人
    message.setFrom(to_from);
    InternetAddress to_to = new InternetAddress(username);
    //设置收件人
    message.setRecipient(Message.RecipientType.TO, to_to);
    //设置主题
    message.setSubject(subject);
    //设置信件内容
    message.setText(messageText);
    //设置发信时间
    message.setSentDate(new Date());
    //发送邮件
    //存储邮件信息
    message.saveChanges();
    Transport transport = s.getTransport("smtp");
    //以smtp方式登录邮箱  注意这一步需要你的用户名和密码,务必正确,才能正常发送
    transport.connect("smtp.qq.com", from, password);
    //发送邮件,其中第二个参数是所有已设好的收件人地址
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    request.setAttribute("msg", "邮件发送成功,请注意查收邮件!");
    return mapping.findForward("success");
    } else {
    request.setAttribute("msg", "邮件发送失败,请检查您输入的用户名是否正确!");
    return mapping.findForward("error");
    } }
      

  4.   

    使用JavaMail收发邮件  
      

  5.   

    EBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false
    220 esmtp7.qq.com Esmtp QQ Mail Server
    DEBUG SMTP: connected to host "smtp.qq.com", port: 25EHLO ibm-70039bfc7fe
    250-esmtp7.qq.com
    250-PIPELINING
    250-SIZE 52428800
    250-AUTH LOGIN
    250-AUTH=LOGIN
    250 8BITMIME
    DEBUG SMTP: Found extension "PIPELINING", arg ""
    DEBUG SMTP: Found extension "SIZE", arg "52428800"
    DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
    DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Attempt to authenticate
    AUTH LOGIN
    334 VXNlcm5hbWU6
    MjY2NTc5MThAcXEuY29t
    454 Error: authentication failed
    javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:264)
    at javax.mail.Service.connect(Service.java:134)
    at util.Mail.main(Mail.java:40)
      

  6.   

    建议ls使用spring mail,对java mail进行了封装,使用非常简单