搜索一下,javamail。有的是现成的例子。

解决方案 »

  1.   

    搜索javamail就有很多相关资料了http://community.csdn.net/Expert/topic/3502/3502948.xml?temp=.1997492
    http://community.csdn.net/Expert/topic/3464/3464123.xml?temp=.9870417
      

  2.   

    import javax.mail.*;
    import javax.activation.*;
    import java.util.*;
    import javax.mail.internet.*;public class SendMail 
    {
       public static void main(String[] args) 
       {
          try 
          {
             Properties props = new Properties();
             Session sendMailSession;
             Store store;
             Transport transport;
             String host,user,pass,from,to;//都要赋值(主机地址,用户名,密码,寄信者,收信者)
             SmtpAuthenticator sa=new SmtpAuthenticator(user,pass); 
             props.put("mail.smtp.host", host);
             props.put("mail.smtp.port", "25");
             props.put("mail.smtp.auth","true");
    //         props.put("mail.smtp.user", user);
    //         props.put("mail.smtp.password", pass);
    //         sendMailSession = Session.getInstance(props, null);
             sendMailSession = Session.getInstance(props, sa);
             Message newMessage = new MimeMessage(sendMailSession);
             newMessage.setFrom(new InternetAddress(from);
             newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
             newMessage.setSubject("测试发信");
             newMessage.setSentDate(new Date());
             newMessage.setText("测试发信成功,欢喜之中");
             transport = sendMailSession.getTransport("smtp");
             transport.send(newMessage);
             System.out.println ("发送成功");
          }
          catch(Exception e) 
          {
             e.printStackTrace();
          }
       }
    }
     class SmtpAuthenticator extends Authenticator
    {    String username;
        String password;    public SmtpAuthenticator(String s, String s1)
        {
            username = s;
            password = s1;
        }    protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(username, password);
        }
    }
      

  3.   

    String address = "";
    String address_display = "no";
    HashMap sendadd_hashmap = new HashMap();
    sendadd_hashmap = this.sendAdd(request);
    address = (String) sendadd_hashmap.get("ADDRESS");if (address != null && address.length() > 0) {

    address_display = "yes";
    OperationBo opBo = new OperationBo();

    String from = "[email protected]";
    String mail_subject =request.getParameter("subject");
    String mail_text = request.getParameter("message");

    mail_subject = opBo.JPToUnicode(mail_subject);
    mail_text = opBo.JPToUnicode(mail_text);
    InternetAddress[] add = new InternetAddress[1];
    add[0] = new InternetAddress(address);Properties props = new Properties();
    props.put("mail.smtp.host", "mail.langchao.com");Session sess =Session.getDefaultInstance(props, null);try {
    message = new MimeMessage(sess);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(
    Message.RecipientType.TO,
    add);
    message.setSubject(mail_subject);
    message.setSentDate(new Date());
    message.setText(mail_text);
    Transport.send(message);
    } catch (Exception e) {
    e.getMessage();
    System.out.println(e.getMessage());
    }