代码如下,错误如题,是书上的例子,代码本身应该没有问题,也用过不少网上抄来的方法,始终没法成功发出这么小小的一封邮件,网上总是说什么包有冲突,我的项目是SSH网站,只引入了JAVA EE5和一些Spring,Hibernate,Struts的包,怎么可能有冲突呢?!希望高人能为我解决这个问题,或者干脆给我一段能顺利执行的代码,而不是又报一堆“找不到XXX方法的错误”,感激不尽!
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;public class zztest {
public static void main(String[] args) {
String smtphost = "smtp.qq.com"; // 发送邮件服务器
String user = "32729223"; // 邮件服务器登录用户名
String password = "cctv";  // 邮件服务器登录密码
String from = "[email protected]"; // 发送人邮件地址
String to = "[email protected]"; // 收件人邮件地址
String subject = "测试test!"; // 邮件标题
String body = "测试test!"; // 邮件内容
// 以下为发送程序,用户无需改动
try {
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
Session ssn = Session.getInstance(props, null); MimeMessage message = new MimeMessage(ssn); InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject("UTF-8");
message.setText(body); Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
transport.close(); } catch(Exception m) {
m.printStackTrace();
                  }
}

解决方案 »

  1.   

    你试试换一个smtphost,qq不一定能给你这样发邮件啊
      

  2.   

    我都换过无数个smtphost了,163,sohu,sina,貌似现在程序根本走不到发送那块,直接在Session ssn = Session.getInstance(props, null);就崩掉了
      

  3.   

    ssn.getTransport("smtp");
    把smtp大写试试
      

  4.   

    package com.east.email;import java.util.Date;
    import java.util.Properties;import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    /**
     *张栋芳
     **/public class SendMail {
     private static MailAuthenticator autherticator=null;
     public static void main(String[] args) {
      for (int i=0;i<5;i++){
     
    String from="[email protected]";
    String to="[email protected]";
    String smtpServer="smtp.163.com";
    String subject="Hello ,this is a email Test!!";
    String content ="Welcome to you!!";
    Properties props = System.getProperties();

    props.put("mail.smtp.host", smtpServer);
    props.put("mail.smtp.auth","true");
    autherticator = new MailAuthenticator("[email protected]","xtsaiyy");
        Session session = Session.getDefaultInstance(props,autherticator);
        MimeMessage msg = new MimeMessage(session);
        try{
           msg.setFrom(new InternetAddress(from));
           msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
           msg.setSubject(subject);
           msg.setSentDate(new Date());
           msg.setText(content);
           Transport.send(msg);
           System.out.println("成功发送邮件......");
        }catch(Exception se){
         se.printStackTrace();
        }
        }
    }
    }
    //现在的大部分的邮件服务器都要求有身份验证,所以需要此类实现验证功能
    class MailAuthenticator extends Authenticator{

        private String username = null;
        private String userpasswd = null;

        public MailAuthenticator(){}
        public MailAuthenticator(String username,String userpasswd){
            this.username = username;
            this.userpasswd = userpasswd;
        }
        
        public void setUserName(String username){
            this.username = username;
        }

        public void setPassword(String password){
            this.userpasswd = password;
        }

        public PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(username,userpasswd);
        }
    }