<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    姿姿霸霸~~!
    貴在堅持!
    posts - 106,  comments - 50,  trackbacks - 0

    1.用spring的mail發(fā)郵件需要將j2ee包里的mail.jar和activation.jar引入
    2.遇見的異常可能會有
       (1)java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
       (2)java.lang.NoClassDefFoundError: com/sun/activation/registries/LogSupport
    這2個異常都是由于JavaEE版本和JavaMail的版本不一致所造成的.如javaMail1.3以下的如果在javaEE5上就會出現(xiàn)上面的錯誤,因為javaEE5中包含有javaMail的類但是卻不全面,所以造成與本身的JavaMail包沖突。而activation1.0.2與1.1版本也不同,LogSupport在1.0.2中沒有。
    3.各個郵件服務(wù)器的驗證可能不一定都能通過,多換幾個試試。
    4.發(fā)送簡單郵件可以使用SimpleMailMessage
    5.發(fā)送帶附件的郵件可以使用MimeMessage+MimeMessageHelper
    6.如果要發(fā)送html格式的內(nèi)容,MimeMessageHelper中的方法setText("需要發(fā)送的html格式的內(nèi)容",true)
    7.如果在容器中使用spring發(fā)送郵件的話,在讀取配置文件的時候,因為容器的特殊性,不需要使用 ApplicationContext ctx = new FileSystemXmlApplicationContext( "src/mail-config.xml") ,可以使用ApplicationContext ctx = WebApplicationContextUtils .getWebApplicationContext(this.getServletContext())來獲取ctx。在容器初始化的時候?qū)⑦@個ctx獲取之后存在某個靜態(tài)變量中去。在使用的時候再根據(jù)這個ctx去獲取相應(yīng)的bean。

    代碼如下:
    1.SendMail類

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    import javax.mail.internet.MimeMessage;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;

    import org.springframework.core.io.FileSystemResource;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;

    import com.sureframe.BeanManager;

    /**
     * 
    @author zpruan
     * @mail  <xrzp_dh@yahoo.com.cn>
     
    */

    public class SendMail {

        
    // 將郵件頁面的參數(shù)按照map的形式放入
        private Map<String, String> parameters = new HashMap<String, String>();

        
    // 分隔符
        private final static String fileSeparator = System
            .getProperty(
    "file.separator");

      
    /**
         * 發(fā)送帶附件的郵件
         * 
    @param request
         * 
    @param response
         * 
    @throws ServletException
         * 
    @throws IOException
         
    */

        
    public void sendMail(HttpServletRequest request,
            HttpServletResponse response) 
    throws ServletException, IOException {
        
        
    //因為直接是在容器中.故使用BeanManager將相應(yīng)的bean獲取,再造型成JavaMailSender
        JavaMailSender sender = (JavaMailSender) BeanManager
            .getBean(
    "mailSender");

        request.setCharacterEncoding(
    "UTF-8");
        
        
    //添加附件到服務(wù)器
        File file = this.doAttachment(request);

        MimeMessage msg 
    = sender.createMimeMessage();
        
    try {
            MimeMessageHelper helper 
    = new MimeMessageHelper(msg, true,
                
    "GB2312");
            
    //發(fā)送到哪兒
            helper.setTo(parameters.get("to"));
            
    //誰發(fā)送的
            helper.setFrom(parameters.get("from"));
            
    //發(fā)送的主題
            helper.setSubject(parameters.get("subject"));
            
    //發(fā)送的內(nèi)容
            helper.setText(parameters.get("content"),true);
            
    if (file != null{
            FileSystemResource fileSource 
    = new FileSystemResource(file
                .getPath());
            helper.addAttachment(file.getName(), fileSource);
            }


            sender.send(msg);
        }
     catch (Exception e) {
            e.printStackTrace();
        }


        }


      
    /**
         * 發(fā)送簡單郵件
         * 
    @param request
         * 
    @param response
         * 
    @throws ServletException
         * 
    @throws IOException
         
    */

        
    public void sendMail1(HttpServletRequest request,
            HttpServletResponse response) 
    throws ServletException, IOException {

        JavaMailSender sender 
    = (JavaMailSender) BeanManager
            .getBean(
    "mailSender");

        SimpleMailMessage mail 
    = new SimpleMailMessage();
        
    try {
            mail.setTo(
    "xxxx@qq.com");
            mail.setFrom(
    "xxxx@163.com");
            mail.setSubject(
    "dosth by xxx");
            mail.setText(
    "springMail的簡單發(fā)送測試");
            sender.send(mail);
        }
     catch (Exception e) {
            e.printStackTrace();
        }

        }


      
    /**
         * 添加附件
         * 在添加附件的時候,可以將表格想對應(yīng)的參數(shù)放到一個map中去
         * 在此使用了Jakarta commons的fileupload組件
         * 
    @param request
         * 
    @return
         * 
    @throws ServletException
         * 
    @throws IOException
         
    */

        @SuppressWarnings(
    "unchecked""deprecation" })
        
    public File doAttachment(HttpServletRequest request)
            
    throws ServletException, IOException {
        File file 
    = null;
        DiskFileItemFactory factory 
    = new DiskFileItemFactory();
        ServletFileUpload upload 
    = new ServletFileUpload(factory);

        
    try {
            List items 
    = upload.parseRequest(request);
            Iterator it 
    = items.iterator();
            
    while (it.hasNext()) {
            FileItem item 
    = (FileItem) it.next();
            
    if (item.isFormField()) {
                parameters.put(item.getFieldName(), item.getString(
    "UTF-8"));
            }
     else {
                
    if (item.getName() != null && !item.getName().equals("")) {
                File tempFile 
    = new File(item.getName());
                String path 
    = request.getRealPath(fileSeparator)
                    
    + "uploads" + fileSeparator;
                file 
    = new File(path);
                
    //建立個文件夾
                if(!file.exists()){
                    file.mkdir();                
                }

                file 
    = new File(path, tempFile.getName());
                
    //將附件上傳到服務(wù)器
                item.write(file);
                }

            }

            }

        }
     catch (Exception e) {
            e.printStackTrace();
        }

        
    return file;
        }

        
    }


    2.BeanManager類

    import org.springframework.context.ApplicationContext;

    /**
     * 
    @author zpruan
     * @mail  <xrzp_dh@yahoo.com.cn>
     
    */

    public class BeanManager {

        
    // 應(yīng)用上下文環(huán)境對象
        private static ApplicationContext ac = null;

       
    /**
         * 利用Spring實現(xiàn)聲明式依賴注入,便于直接獲取bean對象
         
    */

        
    public static ApplicationContext getApplicationContext() {
        
    return ac;
        }


      
    /**
         * 返回Spring的ApplicationContext對象
         * 
         * 
    @return
         
    */

        
    public static void setApplicationContext(ApplicationContext acObj) {
        ac 
    = acObj;
        }


      
    /**
         * 根據(jù)指定的bean名字來獲取bean
         * 
         * 
    @param key
         * 
    @return
         
    */

        
    public static Object getBean(String key) {
        
    return ac.getBean(key);
        }


    }


     3.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop
    ="http://www.springframework.org/schema/aop"
        xmlns:tx
    ="http://www.springframework.org/schema/tx"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
    >

        
    <bean id="mailSender"
            class
    ="org.springframework.mail.javamail.JavaMailSenderImpl">
            
    <property name="host">
                
    <value>smtp.163.com</value>
            
    </property>
            
    <property name="javaMailProperties">
                
    <props>
                    
    <prop key="mail.smtp.auth">true</prop>
                    
    <prop key="mail.smtp.timeout">25000</prop>
                
    </props>
            
    </property>
            
    <property name="username">
                
    <value><!-- 用戶名 --></value>
            
    </property>
            
    <property name="password">
                
    <value><!-- 密碼 --></value>
            
    </property>
        
    </bean>
    </beans>
    posted on 2008-10-18 16:18 xrzp 閱讀(2722) 評論(4)  編輯  收藏 所屬分類: JAVA

    FeedBack:
    # re: 使用spring發(fā)送郵件
    2008-10-19 23:36 | 楊愛友
    用你的辦法我沒有得到ApplicationContext對象,我改用new ClassPathXmlApplicationContext("applicationContext.xml")獲取。  回復(fù)  更多評論
      
    # re: 使用spring發(fā)送郵件
    2008-10-19 23:58 | 楊愛友
    你xml文件里的用戶名和密碼指的是發(fā)送者郵箱,可當(dāng)一個系統(tǒng)投入使用時,這個發(fā)送者肯定是當(dāng)前用戶,所以我覺得發(fā)送者的用戶名和密碼不應(yīng)該設(shè)置在xml文件,應(yīng)該是從前臺獲取,然后再設(shè)置到JavaMailSender 對象。  回復(fù)  更多評論
      
    # re: 使用spring發(fā)送郵件
    2008-10-20 23:37 | sure_xx
    @楊愛友
    ApplicationContext ctx = WebApplicationContextUtils .getWebApplicationContext(this.getServletContext()) 是對容器的上下文環(huán)境的獲取.我是重寫了DispatcherServlet類,放到init方法中去獲取,然后再set到beanManager中去的.  回復(fù)  更多評論
      
    # re: 使用spring發(fā)送郵件
    2008-10-20 23:40 | sure_xx
    @楊愛友
    你說的很對哈.如果是個系統(tǒng)的話,登陸之后就有自己的用戶名和密碼.可以將JavaMailSender sender = (JavaMailSender) BeanManager.getBean("mailSender");改為JavaMailSenderImpl sender = (JavaMailSenderImpl)BeanManager.getBean("mailSender");再通過sender的setUsername和setPassword來將用戶名和密碼搞進(jìn)去.
      回復(fù)  更多評論
      

    <2008年10月>
    2829301234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    好友的blog

    搜索

    •  

    積分與排名

    • 積分 - 117345
    • 排名 - 500

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲经典在线中文字幕| 永久免费A∨片在线观看| 亚洲三级电影网站| 国产精品久久香蕉免费播放| 日本视频一区在线观看免费| 一级特黄aaa大片免费看| 亚洲色中文字幕在线播放| 亚洲视频在线免费看| 久久精品亚洲福利| 国产成人综合久久精品免费| 欧美男同gv免费网站观看| 色欲色香天天天综合网站免费| 国产乱妇高清无乱码免费| 亚洲av午夜电影在线观看| 亚洲国产系列一区二区三区| 久久精品国产亚洲AV无码麻豆| 国产亚洲综合久久系列| 久久久久久A亚洲欧洲AV冫| 免费永久在线观看黄网站| 热久久精品免费视频| 国产在线国偷精品产拍免费| 国产卡一卡二卡三免费入口| 亚洲免费视频网址| 3d成人免费动漫在线观看| 久久国产免费观看精品3| 国产成人精品一区二区三区免费| 羞羞视频免费网站在线看| 五月天婷婷免费视频| 一级毛片免费播放视频| 一区二区三区视频免费| 免费看美女午夜大片| 一本久久免费视频| caoporn成人免费公开| www成人免费观看网站| 一级人做人a爰免费视频 | 99久久免费精品国产72精品九九 | 最新亚洲卡一卡二卡三新区| 亚洲冬月枫中文字幕在线看| 久久精品国产99国产精品亚洲| 亚洲五月综合网色九月色| 亚洲乱理伦片在线观看中字|