我写的,发送文字,html,附件,图片 等等...自己看吧try{
Properties props = new Properties();
props.put("mail.smtp.host","ip或域名"); //服务器
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props);
      SMTPTransport tr = new SMTPTransport(session, null);
            tr.connect("61.152.125.84","name", "password"); //用户密码 !!!
MimeMessage message = new MimeMessage(session);
         //   message.setContent("Hello","text/plain");
            message.setSubject(mail_subject);
            String htmltext="<h1>hello</h1>"+"<a href=\"http://www.sina.com.cn\" target=\"_blank\">click me please!</a>";
//message.setContent(htmltext,"text/html");//
            message.setHeader(mail_head_name,mail_head_value);//原理:可以将邮件看成有多个部分组成的,如正文是一个部分,附件也是一个部分,所以用BodyPart来设置邮件的格式
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(mail_to));
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(htmltext,"text/plain") ;
           messageBodyPart.setText("testtext");
           Multipart multipart = new MimeMultipart("related");
            multipart.addBodyPart(messageBodyPart);            messageBodyPart = new MimeBodyPart();
            String htmlimg = "<h1>hello</h1>"+"<img src=\"cid:memememe\">";//
            messageBodyPart.setContent(htmlimg,"text/html") ;
            multipart.addBodyPart(messageBodyPart);            messageBodyPart = new MimeBodyPart();
            DataSource fds = new FileDataSource("./test.jpg");   //图片
            messageBodyPart.setDataHandler(new DataHandler(fds));
            messageBodyPart.setHeader("Content-ID","memememe");
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource("./jiveDbLog.log");//此处设置邮件的附件
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("test.txt");            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
message.setSentDate(new java.util.Date());
 message.setFrom(new InternetAddress("[email protected]"));
Address toAddress = new InternetAddress(mail_to,"[email protected]");
message.addRecipient(Message.RecipientType.TO,toAddress);//tr.send(message);
 Address add[] = new Address[1];
                add[0] = toAddress;
 tr.sendMessage(message,add) ;
System.out.println("send ok!");
}
catch(Exception ex)
{
ex.printStackTrace() ;
}

解决方案 »

  1.   

    public class YpwTmEngineMailService
    {
      private static String MAILBOX=YpwConstants.TM_ENGINE_MAILBOX;
      private static String MAILSERVER =YpwConstants.TM_ENGINE_MAILSERVER;
      private static String MAIL_HOST =YpwConstants.TM_ENGINE_MAIL_HOST;
      Vector recipients = new Vector();
      String subject_ = new String();
      String message_ = new String();
      String fromUsr_ = new String();
      String mailBox_ = new String();
      String userID_ = new String();
      public YpwTmEngineMailService()
      {  }
      /**
       * Send the mail to user.
       */
      public void send()
      {
            String from = fromUsr_ + YpwCode.TM_ENGINE_MAIL_BOX;
            String host = YpwCode.TM_ENGINE_MAIL_SERVER;
            Properties props = new Properties();
            if ( host == null || host.trim().length() == 0 )
            {
              YpwWarningHandle.getInstance().logYpwError( this.getClass(), YpwConstants.YPW_LOG_TM_ENGINE_MAIL_SERVER_NULL );
              return;
            }
            props.put(MAIL_HOST, host);
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);
            try
            {
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress( from ));
                InternetAddress address[] = new InternetAddress[recipients.size()];
                for(int i=0;i<recipients.size();i++){
                  address[i]=new InternetAddress((String)recipients.elementAt(i)
                             +YpwCode.TM_ENGINE_MAIL_BOX );            }            msg.setRecipients(Message.RecipientType.TO, address);
                msg.setSubject(subject_ );
                msg.setSentDate(new Date());
                msg.setText(message_);
                Transport.send(msg);
            }
            catch (Exception mex) {
            }
      }
        /**
         * The method is used to send the attribute of Mail.
         * @param mail  a entity of YpwTmMail.
         * @return retrun true if the operation is successful.
         */
        public boolean doMail (YpwTmMail mail)
        {
            boolean doMail = false;
            message_ = mail.getMessage();
            Vector usrID = mail.getUsr();
            if( usrID == null || usrID.size() == 0 )
            {
                YpwWarningHandle.getInstance().logYpwError( this, MessageCode.ERROR_TM_ENGINE_USER_ID_IS_NULL );
                return false;
            }
            for( int i = 0; i < usrID.size(); i ++ )
            {
                userID_ = ( String )usrID.get( i );
                recipients.add( userID_ );
            }
            subject_ = mail.getSubject( );
            mailBox_ = mail.getMailBox( );
            fromUsr_ = mail.getFromUser();
            send();
            return true;
        }
    }
      

  2.   

    public class YpwTmMail
    {
      private Vector userId_ = new Vector( );
      private String fromUser_ = new String( );
      private String message_ = new String( );
      private String subject_ = new String( );
      private String mailBox_ = new String( );
      /**
       * <p>Description: This is construct method.</p>
       */
      public void YpwMail() {
      }
      /**
       * <p>Description: This method is to set mail box.</p>
       * @param mailBox mail box.
       */
      public void setMailBox( String mailBox )
      {
          this.mailBox_ = mailBox;
      }
      /**
       * <p>Description: This method is to get mail box.</p>
       * @return String mail box.
       */
      public String getMailBox( )
      {
          return mailBox_;
      }
      /**
       * <p>Description: This method is to set user.</p>
       * @param userId user id.
       */
      public void setUsr(Vector userId){
          this.userId_ = userId;
      }
      /**
       * <p>Description: This method is to set subject.</p>
       * @param subject string of subject.
       */
      public void setSubject( String subject )
      {
          this.subject_ = subject;
      }
      /**
       * <p>Description: This method is to set from user.</p>
       * @param fromUser from user.
       */
      public void setFromUser( String fromUser )
      {
          this.fromUser_ = fromUser;
      }
      /**
       * <p>Description: This method is to set message.</p>
       * @param message contents of message.
       */
      public void setMessage( String message )
      {
          this.message_ = message;
      }
      /**
       * <p>Description: This method is to get subject.</p>
       * @return String subject content.
       */
      public String getSubject( )
      {
          return subject_;
      }
      /**
       * <p>Description: This method is to get user.</p>
       * @return Vector user information.
       */
      public Vector getUsr( )
      {
          return userId_;
      }
      /**
       * <p>Description: This method is to get from user.</p>
       * @return String from user.
       */
      public String getFromUser()
      {
          return fromUser_;
      }
      /**
       * <p>Description: This method is to get message.</p>
       * @return String contents of message.
       */
      public String getMessage()
      {
          return message_;
      }}
      

  3.   

    http://expert.csdn.net/Expert/topic/1788/1788894.xml?temp=.2226068绝对可用!
    我正在使用。