加邮箱验证给你一个例子import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.xindeco.common.inc.ExtendString;public class SendMail
{
    private String errMsg = "";
    private ExtendString ExStr = new ExtendString();    private String sender = "";//发件人地址
    private String smtpHost = "";//邮件发送服务器(smtp)
    private String user = ""; //登录用户名
    private String password = "";//登录密码    private String subject = "";//mail主题    public SendMail()
    {
        this.setPropertiesAttri();
    }    private void setPropertiesAttri()
    {
        try
        {            InputStream is = getClass().getResourceAsStream("MailServer.properties");
            Properties prop = new Properties();
            prop.load(is);            this.setSmtpHost(prop.get("SmtpHost").toString());
            //System.err.println("getSmtpHost:"+this.getSmtpHost());
            this.setUser(prop.get("User").toString());
            //System.err.println("getUser:"+this.getUser());
            this.setPassword(prop.get("Password").toString());
            //System.err.println("getPassword:"+this.getPassword());
            this.setSender(prop.get("Sender").toString());
            //System.err.println("getSender:"+this.getSender());
            this.setSubject(ExStr.CS(prop.get("Subject").toString()));
            //System.err.println("getSubject:"+this.getSubject());
        }
        catch(Exception ex)
        {
            System.err.println("ex1 in sendmail.java:"+ex.toString());
        }
    }    /** 设置发件人地址 */    public void setSender(String sender)
    {
        this.sender = sender;
    }
    public String getSender()
    {
        return sender;
    }    /** 设置邮件发送服务器(smtp) */
    public void setSmtpHost(String smtpHost) {this.smtpHost = smtpHost;}
    public String getSmtpHost() {return smtpHost;}    /** 设置登录用户名 */
    public void setUser(String user)
    {
        this.user = user;
    }
    public String getUser()
    {
        return user;
    }    /** 设置登录密码 */
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getPassword()
    {
        return password;
    }    /** 设置mail主题 */
    public void setSubject(String subject)
    {
        this.subject = subject;
    }
    public String getSubject()
    {
        return subject;
    }    /**
     * 使用smtp发送邮件 主程序
     * @throws MessagingException mail发送失败
     */
    public void smtp(String receiver,String content) throws MessagingException
    {
        if (smtpHost == null) throw new MessagingException("smtpHost not found");
        if (user == null) throw new MessagingException("user not found");
        if (password == null) throw new MessagingException("password not found");        Properties properties = new Properties();
        properties.put("mail.smtp.host", smtpHost);//设置smtp主机
        properties.put("mail.smtp.auth", "true");//使用smtp身份验证        Session session = Session.getDefaultInstance(properties, new Authenticator()
        {
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(user, password);
            }
        }
        );        //获得邮件会话对象
        MimeMessage mimeMsg = new MimeMessage(session);//创建MIME邮件对象
        if (sender != null)//设置发件人地址
            {
                mimeMsg.setFrom(new InternetAddress(sender));
            }
        if (receiver != null)//设置收件人地址
           {
               mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));
           }
        if (subject != null)//设置邮件主题
           {
               mimeMsg.setSubject(subject, "GBK");
           }
        MimeBodyPart part = new MimeBodyPart();//mail内容部分
        part.setText(content == null ? "" : content, "GBK");        //设置邮件格式为html cqc
        part.setContent(content.toString(),"text/html;charset=GBK");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(part);//在 Multipart 中增加mail内容部分
        mimeMsg.setContent(multipart);//增加 Multipart 到信息体
        mimeMsg.setSentDate(new Date());//设置发送日期
        Transport.send(mimeMsg);//发送邮件
        //System.err.println("邮件发送成功!");
    }    /** 解析地址集合字符串 */
    private InternetAddress[] parse(String addressSet) throws AddressException
    {
        ArrayList list = new ArrayList();
        StringTokenizer tokens = new StringTokenizer(addressSet, ";");
        while (tokens.hasMoreTokens())
        {
            list.add(new InternetAddress(tokens.nextToken().trim()));
        }
        InternetAddress[] addressArray = new InternetAddress[list.size()];
        list.toArray(addressArray);
        return addressArray;
    }    //供外部调用的接口
    public boolean sendMails(String mail,String content)
    {
        int mailLen = 0 ;
        int contentLen= 0;
        if (mail == null||content==null)
        {
            return false;
        }            try
            {
                this.smtp(mail,content);
            }
            catch(Exception ex)
            {
                System.err.println("ex2 in sendmail.java:"+ex.toString());
            }        return true;
    }     // for test
/*
         public static void main (String[] args)
     {
         SendMail mail = new SendMail();
         String email = "[email protected]";
         String content = "账号:123 密码:123 <br>感谢您注册使用福建省大中专毕业生就业创业公共服务网!<br><a href='http://www.xindeco.com.cn' target='_blank'>www.xindeco.com.cn</a><br>此致<br>陈庆传 <br>即日";
         try
         {
             mail.sendMails(email,content);
         }
         catch (Exception ex)
         {
            System.err.println("ex33:"+ex.toString());
         }
     }
*/ }

解决方案 »

  1.   

    to:bibiye(布什的老哥)
    你的ExtendString类好像是你自己包的把,我编译不过去
      

  2.   

    现在大部分的邮件服务器需要身份验证,防止恶意的垃圾邮件给你一段代码,我收藏的<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>撰写邮件</title>
    </head><body>
    <form name="form1" method="post" action="testmail.jsp">
    <table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
    <tr bgcolor="#FFFFFF"> 
    <td width="24%">收信人地址:</td>
    <td width="76%"> 
    <input name="to" type="text" id="to"></td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
    <td>主题:</td>
    <td> 
    <input name="title" type="text" id="title"></td>
    </tr>
    <tr> 
    <td height="107" colspan="2" bgcolor="#FFFFFF"> 
    <textarea name="content" cols="50" rows="5" id="content"></textarea></td>
    </tr>
    <tr align="center"> 
    <td colspan="2" bgcolor="#FFFFFF"> 
    <input type="submit" name="Submit" value="发送">
    <input type="reset" name="Submit2" value="重置">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
      

  3.   

    接着,我们再写一个处理程序testmail.jsp,如下:
    -----------------------------------------------------------------------------------------
    <%@ page contentType="text/html;charset=GB2312" %>
    <%request.setCharacterEncoding("gb2312");%><!--中文处理代码--><!--引入要用到的类库-->
    <%@ page import="java.util.*,javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>发送成功</title>
    </head><body>
    <%
    try{//从html表单中获取邮件信息
    String tto=request.getParameter("to");
    String ttitle=request.getParameter("title");
    String tcontent=request.getParameter("content");Properties props=new Properties();//也可用Properties props = System.getProperties(); 
    props.put("mail.smtp.host","smtp.163.net");//存储发送邮件服务器的信息
    props.put("mail.smtp.auth","true");//同时通过验证
    Session s=Session.getInstance(props);//根据属性新建一个邮件会话
    s.setDebug(true);MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象//设置邮件
    InternetAddress from=new InternetAddress("[email protected]");
    message.setFrom(from);//设置发件人
    InternetAddress to=new InternetAddress(tto);
    message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
    message.setSubject(ttitle);//设置主题
    message.setText(tcontent);//设置信件内容
    message.setSentDate(new Date());//设置发信时间//发送邮件
    message.saveChanges();//存储邮件信息
    Transport transport=s.getTransport("smtp");
    transport.connect("smtp.163.net","boy","iloveyou");//以smtp方式登录邮箱
    transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
    //已设好的收件人地址
    transport.close();%>
    <div align="center">
    <p><font color="#FF6600">发送成功!</font></p>
    <p><a href="recmail.jsp">去看看我的信箱</a><br>
    <br>
    <a href="index.htm">再发一封</a> </p>
    </div>
    <%
    }catch(MessagingException e){
    out.println(e.toString());
    }
    %>
    </body>
    </html>
      

  4.   

    可以直接运行props.put("mail.smtp.host","smtp.163.net");//存储发送邮件服务器的信息
    这段选择邮件服务器transport.connect("smtp.163.net","boy","iloveyou");//以smtp方式登录邮箱这段设置帐户密码
      

  5.   

    在你的代码中加上props.put("mail.smtp.auth", true);
    Authenticator auth = new MyAuthenticator();(自己写的类)把Session session = Session.getDefaultInstance(props, null);改为Session session = Session.getDefaultInstance(props, auth);
    MyAuthenticator类:
    import javax.mail.*;public class MyAuthenticator extends Authenticator {
        public MyAuthenticator() {
        }
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("[email protected]", "123456");
        }}就可以了发送了,其实你的问题是认证问题。
      

  6.   

    to:alaal(穷街)
    你好,我按你的方法改了一下我的代码,可还是运行不成功,请帮我看看
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;public class MailExample {
      public static void main (String args[])  {
        try{
          String host = "smtp.tom.com";
          String from = "[email protected]";
          String to = "[email protected]";      // Get system properties
          Properties props = System.getProperties();
          
          
          // Setup mail server
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.auth","true");
         
          // Get session
         // Session session = Session.getDefaultInstance(props, null);
         Session session=Session.getInstance(props);      // Define message
          MimeMessage message = new MimeMessage(session);      // Set the from address
          message.setFrom(new InternetAddress(from));      // Set the to address
          message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));      // Set the subject
          message.setSubject("Hello JavaMail");      // Set the content
          message.setText("Welcome to JavaMail");
          Transport transport=session.getTransport("smtp");
          transport.connect("smtp.tom.com","用户名","密码");//
          transport.send(message);
          // Send message
        //  Transport.send(message);
          System.out.println("ok");
        }catch(Exception e){
          e.printStackTrace();
        }
      }
     
    }
      

  7.   

    props.put("mail.smtp.host", host);-->props.put("mail.smtp.host","smtp.tom.com");
    再加一句message.saveChanges();
    transport.send(message);-->transport.sendMessage(message,message.getAllRecipients());
      

  8.   

    我的问题解决了,我用transport.sendMessage()这个方法就可以了,谢谢 alaal(穷街) 和bibiye(布什的老哥) 和dongfeng190(枫)