补充一下,上面的例子中提到的from字段,可以是一个任意的email地址
随便添都可以。to必须是一个固定服务器中的真实存在的email!!!

解决方案 »

  1.   

    Need smtp authentication. props.put("mail.host", host);
    props.put("mail.transport.protocol", "smtp");
    //authentication is needed
    props.put("mail.smtp.auth", true);
    Transport transport;
    transport = mailSession.getTransport("smtp");
    transport.send(msg);
    Tansport.send(msg); //this code is enough.
      

  2.   

    不行啊,我加上props.put("mail.smtp.auth", true);
    后还是出错
    org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: -1 in the jsp file: nullGenerated servlet error:
        [javac] Since fork is true, ignoring compiler setting.
        [javac] Compiling 1 source file
        [javac] Since fork is true, ignoring compiler setting.
        [javac] D:\Tomcat\work\Standalone\localhost\_\mail_jsp.java:71: cannot resolve symbol
        [javac] symbol  : method put (java.lang.String,boolean)
        [javac] location: class java.util.Properties
        [javac] props.put("mail.smtp.auth",true) ;
        [javac]      ^
        [javac] 1 error
      

  3.   

    http://www.csdn.net/develop/Read_Article.asp?Id=14929
      

  4.   

    props.put("mail.smtp.auth", "true");
      

  5.   

    光添加这一句吗????还是不行啊,我希望有个jsp的程序段,不是application的bruni可以提供你的qq或者msn吗我的是10445104,[email protected]感激不尽
      

  6.   

    不会的,我觉得,你说错了,应该是发信人必须用相应服务器的SMTP。发给谁当然没有关系了
    我调试成功的代码如下:
    你试试看:
    package bean;
    import java.io.*;
    import java.net.InetAddress;
    import java.util.Properties;
    import java.util.Date;import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;public class sendMail
    { String host ="";
        String from="";
    String to="";
        String subject="";
    String content="";
    String username ="";
    String password ="";
    String fileAttachment="";

        public void setFrom(String  s)
        {
            from = s;
        }    public void setTo(String s)
        {
            to= s;
        }    public void setSubject(String s)
        {
            subject = s;
        }
        
        public void setContent(String s)
        {
            content= s;
        }
        
        public void setFileAttachment(String s)
        {
            fileAttachment= s;
        }
        
        public void send ()throws Exception
        {        host = "smtp.163.com";
            from =  "[email protected]";
            to = "[email protected]";
            username = "test";
            password = "test";
            subject = "你好";
            content = "测试JAVAMAIL";
            fileAttachment = "C:\\12.gif";
            
            // Get system properties
            Properties props = new Properties();        // Setup mail server
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true"); //这样才能通过验证        // Get session
            Session session = Session.getDefaultInstance(props);        // watch the mail commands go by to the mail server
            session.setDebug(true);        // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
            message.setSubject(subject);
            message.setText(content);
            
            
            // 第二部分信息
            MimeBodyPart mbp2 = new MimeBodyPart();        // 在第二部分信息中附加一个文件
            FileDataSource fds = new FileDataSource( fileAttachment );
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());        // 创建 Multipart 并放入每个 MimeBodyPart
            Multipart mp = new MimeMultipart();
            mp.addBodyPart( mbp2 );        // 增加 Multipart 到信息体
          message.setContent( mp );        // Send message
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(host, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
    }
     
      

  7.   


    props.put("mail.smtp.auth", "true");不是
    props.put("mail.smtp.auth", true);
      

  8.   

    Session session = Session.getInstance(SMTPProps.getSMTPProps(MSG_TO), 
                    new Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    }
                    );
      

  9.   

    错了,把SMTPProps.getSMTPProps(MSG_TO)改成你的props
      

  10.   

    感谢bruni!!!!!!!!!!!!!
      

  11.   

    首先要下载javamailapi的.jar包,然后在  
    控制面版-->系统-->高级-->环境变量-->系统变量-->classpath  
    添加相应的.jar路径,好象有mail.jar,smtp.jar,pop3.jar等等