给你个现成的例子:
<%@ page contentType="text/html;charset=gb2312" language="java" import="javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*" errorPage="error.html" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
String host="smtp.163.com";
//        String host = request.getParameter("server");
String username ="用户名"; //request.getParameter("user");
        String password ="密码"; //request.getParameter("pass");
String from = request.getParameter("from");
        String to = request.getParameter("to");
String subject = request.getParameter("subject");
String text = request.getParameter("text");        // Get system properties
        // Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
        Properties props = new Properties();
Session sendMailSession;
sendMailSession = Session.getInstance(props, null);        // Setup mail server
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true"); //这样才能通过验证        // Get session
        // watch the mail commands go by to the mail server
        sendMailSession.setDebug(true);        // Define message
        MimeMessage message = new MimeMessage(sendMailSession);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO,
        new InternetAddress(to));
        message.setSubject(subject);
        message.setText(text);        // Send message
        message.saveChanges();
        Transport transport = sendMailSession.getTransport("smtp");
        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
out.println("<p>&nbsp;</p><p>&nbsp;</p><p align='center'>ok!邮件发送成功!</p>");
%>

解决方案 »

  1.   

    先谢谢上面的几位朋友了!!!! 
       你提供的是用JSP写的啊,可是这怎么样才能和我用JAVA编的服务器和客户那部分联系在一起啊.
      

  2.   

    你能否给我发个JAVA编的源程序呢
      这里面包括了验证那部分啊
      先谢谢了啊!!!1
      

  3.   

    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Date;
    import javax.activation.*;
    import java.io.*;public class sendMail
    {
          public MimeMessage mimeMsg;       public Session session; 
          public Properties props;
          public boolean needAuth = false;      public static String username = ""; 
          public static String password = "";      public Multipart mp; 
       
      
       public sendMail(String smtp)
       {
          setSmtpHost(smtp);
          createMimeMessage();
       }   
       public void setSmtpHost(String hostName)
       {
          System.out.println("Set System Property mail.smtp.host = "+hostName);
          if(props == null)props = System.getProperties(); 
          props.put("mail.smtp.host",hostName); 
       }   
       public boolean createMimeMessage()
       {
          try{
             System.out.println("Ready for Obtain Mail Session Object!");
             session = Session.getDefaultInstance(props,null); 
          }catch(Exception e)
           {
              System.err.println("Obtain Mail Session Object Error!"+e);
              return false;
           }
          System.out.println("Reading for Carete MIME Mail Object!");
          try{
             mimeMsg = new MimeMessage(session); 
             mp = new MimeMultipart();         return true;
       }catch(Exception e)
        {
           System.err.println("Carete MIME Mail Object Failure!"+e);
           return false;
        }
    }
       public void setNeedAuth(boolean need) 
       {
          System.out.println("Set smtp AuthAuthenticator: mail.smtp.auth = "+need);
          if(props == null)
          {
             props = System.getProperties();
          }
          if(need)
          {
             props.put("mail.smtp.auth","true");
          }
          else
          {
             props.put("mail.smtp.auth","false");
          }
       }
       public void setNamePass(String name,String pass) 
       {
          username = name;
          password = pass;
       }
       public boolean setSubject(String mailSubject) 
       {
          System.out.println("Set Mail Subject");
          try{
             mimeMsg.setSubject(mailSubject);
             return true;
          }catch(Exception e) 
           {
              System.err.println("Setting Mail Subject Error!");
              return false;
           }
       }
       public boolean setBody(String mailBody) 
       {
          try
          {
             BodyPart bp = new MimeBodyPart();
             bp.setContent("<meta http-equiv=Content-Type content=text/html;charset=gb2312>"+mailBody,"text/html;charset=GB2312");
             mp.addBodyPart(bp);
             return true;
          }catch(Exception e)
           {   
              System.err.println("Setting Mail Content Error!"+e);
              return false;
           }
       }
       public boolean addFileAffix(String filename) 
       {
          System.out.println("Add attach:"+filename);
          try{
             BodyPart bp = new MimeBodyPart();
             FileDataSource fileds = new FileDataSource(filename);
             bp.setDataHandler(new DataHandler(fileds));
             bp.setFileName(fileds.getName());         mp.addBodyPart(bp);         return true;
          }catch(Exception e)
           {
              System.err.println("Add attach :"+filename+" Failure"+e);
              return false;
           }
       }
       public boolean setFrom(String from) 
       {
          System.out.println("Set Mail Sender!");
          try{
             mimeMsg.setFrom(new InternetAddress(from));
             return true;
          }catch(Exception e)
           { 
             return false; 
           }
       }
       public boolean setTo(String to)
       {
          if(to == null)return false;      try{
             mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
             return true;
          }catch(Exception e)
           { 
              return false; 
           }
       }
       public boolean setCopyTo(String copyto)
       {
          if(copyto == null)return false;
          try{
             mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
             return true;
          }catch(Exception e)
           {
              return false; 
           }
       }
       public boolean sendout()
       {
          try{
             mimeMsg.setContent(mp);
             mimeMsg.saveChanges();
             System.out.println("Sending Mail......");         Session mailSession = Session.getInstance(props,null);
             Transport transport = mailSession.getTransport("smtp");
             transport.connect((String)props.get("mail.smtp.host"),username,password);
             transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
             //transport.send(mimeMsg);         System.out.println("Mail send Sucess!");
             transport.close();         return true;
          }catch(Exception e)
           {
              System.err.println("Mail send failure!\r\n"+e);
              return false;
           }
       }
       public static void main(String[] args) 
       {
          String mailbody = new String("You Got a Mail");      sendMail themail = new sendMail("smtp.sohu.com");
          themail.setNeedAuth(true);      String mailto,from; //设置这里
           
          if(themail.setSubject("title") == false) return;
          if(themail.setBody(mailbody) == false) return;
          if(themail.setTo(mailto) == false) return; 
          if(themail.setFrom(from) == false) return;
          //if(themail.addFileAffix("") == false) return;
          themail.setNamePass(username,password);      if(themail.sendout() == false) 
             return;
       }
    }