程序可以运行但是不能把邮件发到我的邮箱

解决方案 »

  1.   

    给你个源码,在我这里已经成功了的
    import java.io.*;
    import java.util.*;
    import java.util.Date;
    import javax.activation.DataHandler;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.swing.JOptionPane;public class Mail
    {
    String myEmail="*****";
    public boolean sendMail(String subject,String body,String to)
    {
    try 
    {
    SmtpAuth sa=new SmtpAuth(); // 密码验证
    sa.getuserinfo(user,pass);
    Properties props=System.getProperties();
    props.put("mail.smtp.auth","true");
    props.put("mail.smtp.host","smtp.sina.com.cn");
    Session sess=Session.getInstance(props,sa); Message msg = new MimeMessage(sess);
    msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312")); msg.setFrom(new InternetAddress(myEmail,user));
    msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(body);
    Transport.send(msg);
    JOptionPane.showMessageDialog(null,"发送成功","成功",JOptionPane.OK_OPTION);
    return true;
    }
    catch (Exception e)
    {
    return false;
    }
    } public static void main(String[] args){
    Mail m=new  Mail();
    m.sendMail("测试","test javamail api",myEmail);
    System.exit(0);
    }
    }
    class SmtpAuth extends javax.mail.Authenticator{ //SMTP身份验证
            private String user,password;
            public void getuserinfo(String getuser,String getpassword){
                      user=getuser;
                      password=getpassword;
            }        protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
                      return new javax.mail.PasswordAuthentication(user,password);
            }
    }