您的位置:程序门 -> java -> web 开发



javamail的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


javamail的问题
发表于:2007-06-09 18:15:33 楼主
这个是我的代码:
//created   by   myeclipse   struts
//   xsl   source   (default):   platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.1/xslt/javaclass.xsl

package   com.yourcompany.struts.action;

import   javax.servlet.http.httpservletrequest;
import   javax.servlet.http.httpservletresponse;

import   org.apache.struts.action.action;
import   org.apache.struts.action.actionform;
import   org.apache.struts.action.actionforward;
import   org.apache.struts.action.actionmapping;

import   tools.myauthenticator;

import   com.yourcompany.struts.form.sendmailform;
import   java.util.*;
import   javax.mail.*;
import   javax.mail.internet.*;

/**
  *   myeclipse   struts   creation   date:   06-08-2007
  *  
  *   xdoclet   definition:
  *  
  *   @struts.action   path= "/sendmail "   name= "sendmailform "
  *                                 input= "/form/sendmail.jsp "   scope= "request "   validate= "true "
  */
public   class   sendmailaction   extends   action   {

//   ---------------------------------------------------------   instance
//   variables

//   ---------------------------------------------------------   methods

/**
  *   method   EXECute
  *  
  *   @param   mapping
  *   @param   form
  *   @param   request
  *   @param   response
  *   @return   actionforward
  */
public   actionforward   EXECute(
actionmapping   mapping,
actionform   form,
httpservletrequest   request,
httpservletresponse   response)  
{

try{
sendmailform   sendmailform   =   (sendmailform)   form;

string   host=sendmailform.gethost();
string   fromname=sendmailform.getfromname();
string   frombox=sendmailform.getfrombox();
string   frompwd=sendmailform.getfrompwd();
string   toname=sendmailform.gettoname();
string   subject=sendmailform.getsubject();
string   mcontent=sendmailform.getmcontent();
string   smtpauth   =   "true ";



                //获得属性,并生成session对象    
  properties   props   =   new   properties();  
             
          session   sendmailsession   =   session.getinstance(props,   null);  


 
                     
          //向属性中写入smtp服务器的地址
          props.put( "mail.smtp.host ",host);
                  //设置smtp服务器需要权限认证      
          props.put( "mail.smtp.auth ",   smtpauth);
            if   ( "true ".equals(smtpauth))   {
                //smtp服务器需要验证,用myauthertiactor来创建mail   session
                myauthenticator   myauth   =   new   myauthenticator(fromname,   frompwd);
                  sendmailsession   =   session.getinstance(props,   myauth);
            }
            else   {
              sendmailsession   =   session.getinstance(props);
            }
        //   props.put( "mail.smtp.auth ", "true ");
          //设置输出调试信息      

                sendmailsession.setdebug(true);
          //根据session生成message对象      

  message   newmessage   =   new   mimemessage(sendmailsession);
 

  newmessage.setfrom(new   internetaddress(frombox));   //设置发信人地址

  newmessage.setrecipient(message.recipienttype.to,new   internetaddress(toname));     //设置收信人地址
                  newmessage.setsubject(subject);   //设置e-mail标题      
  newmessage.setsentdate(new   date());   //设置e-mail发送时间      

          newmessage.settext(mcontent);     //设置e-mail内容      

         
          bodypart   mdp   =   new   mimebodypart();//新建一个存放信件内容的bodypart对象
          mdp.setcontent(mcontent, "text/html;charset=gb2312 ");//设置bodypart对象的内容和格式
          multipart   mm   =   new   mimemultipart();//新建一个multipart来存放bodypart对象
          mm.addbodypart(mdp);//将bodypart加入到mimemultipart对象中(可以加入多个bodypart)
          newmessage.setcontent(mm);//
         
         
          newmessage.savechanges();//存储邮件信息
          transport   transport   =   sendmailsession.gettransport( "smtp ");//用javamail   session对象的gettransport方法来初始化javax.mail.transport类,并申明协议smtp
          transport.connect(host,fromname,frompwd);//以smtp方式登陆邮箱,需填写自己的邮箱smtp地址和邮箱用户名密码
          //transport.sendmessage(newmessage,newmessage.getallrecipients());//发送邮件
          transport   =   sendmailsession.gettransport( "smtp ");
          transport.send(newmessage,   newmessage.getallrecipients());
          transport.close();
 
                      }  

  catch(messagingexception   m)  

          {

  system.out.println(m.tostring());

          }
  request.setattribute( "message ", "邮件已经发出 ");
  return   mapping.findforward( "mailsuccess ");
}
}

执行以后出现的debug信息是:
debug:   setdebug:   javamail   version   1.4ea
debug:   getprovider()   returning   javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,sun   microsystems,   inc]
debug   smtp:   useehlo   true,   useauth   true
debug   smtp:   trying   to   connect   to   host   "smtp.163.com ",   port   25,   isssl   false
220   163.com   anti-spam   gt   for   coremail   system   (163com[20050206])
debug   smtp:   connected   to   host   "smtp.163.com ",   port:   25
哪个高手帮忙解决一下啊


快速检索

最新资讯
热门点击