邮件开发项目的总结

这个月主要是做邮件开发项目,熟悉了javamail的一些应用,了解了邮件Mime格式。还有制作邮件内容的CKEditor编辑器(搞了很头疼。。)。算是做下总结了,以避免忘记。

1,javaMail的使用,用一个发送邮件的demo吧。

public   class  MailSendAction {
    
private   static   final  Logger logger  =  Logger.getLogger(MailSendAction. class );

    
public   static   void  main(String[] args) {
        
try  {
            String hostName 
=   " smtp.qq.com " ;
            String EMAIL_NAME 
=   " 258072761 " ;
            String EMAIL_PASSWORD 
=   " ********* " ;
            MailSendAction mailAction 
=   new  MailSendAction();
            Properties props 
=  System.getProperties();  //  获得系统属性对象
             props.put( " mail.smtp.host " , hostName);  //  设置SMTP主机
            props.put( " mail.smtp.auth " " true " );
            Session session 
=  Session.getDefaultInstance(props);
            
            
//  发送开始
             Transport transport  =  session.getTransport( " smtp " );
             transport.connect((String) props.get(
" mail.smtp.host " ),
             EMAIL_NAME, EMAIL_PASSWORD);
    
            
//  transport.sendMessage(message,
            
//  message.getRecipients(Message.RecipientType.TO));
             transport.close();
        } 
catch  (Exception e) {
            e.printStackTrace();
        }

    }

    
public  MimeBodyPart createAttachment(String fileName)  throws  Exception {
        MimeBodyPart attachementPart 
=   new  MimeBodyPart();
        FileDataSource fds 
=   new  FileDataSource(fileName);
        attachementPart.setDataHandler(
new  DataHandler(fds));
        String[] strs 
=  fds.getName().split( " _ " , 2 );
        
if (strs.length > 1 )
            attachementPart.setFileName(strs[
1 ]);
        
else
            attachementPart.setFileName(fds.getName());
        
return  attachementPart;
    }

    
public  MimeBodyPart createContent(String body, String fileName)
            
throws  Exception {
        MimeBodyPart contentBody 
=   new  MimeBodyPart();
        MimeMultipart contentMulti 
=   new  MimeMultipart( " related " );
        
//  正文文本部分
        MimeBodyPart textBody  =   new  MimeBodyPart();
        textBody.setContent(body, 
" text/html;charset=gbk " );
        contentMulti.addBodyPart(textBody);

        
//  正文图片部分
        MimeBodyPart jpgBody  =   new  MimeBodyPart();
        FileDataSource fds 
=   new  FileDataSource(fileName);
        jpgBody.setDataHandler(
new  DataHandler(fds));
        jpgBody.setContentID(
" logo_jpg " );
        contentMulti.addBodyPart(jpgBody);

        contentBody.setContent(contentMulti);
        
return  contentBody;
    }

    
/**
     * 
     * 
@param  body
     * 
@param  fileName
     * 
@return
     * 
@throws  Exception
     
*/
    
public  MimeBodyPart createContent(String body, List < String >  imgFileNames)
            
throws  Exception {
        MimeBodyPart relatedBodyPart 
=   new  MimeBodyPart();
        MimeMultipart relatedMulti 
=   new  MimeMultipart( " related " );
        
//  正文文本部分
        MimeBodyPart alternativeBodyPart  =   new  MimeBodyPart();
        MimeMultipart alternativeMulti 
=   new  MimeMultipart( " alternative " );
        
        MimeBodyPart textBody 
=   new  MimeBodyPart();
        textBody.setContent(body, 
" text/html;charset=gbk " );
        alternativeMulti.addBodyPart(textBody);
        alternativeBodyPart.setContent(alternativeMulti);
        relatedMulti.addBodyPart(alternativeBodyPart);
        
        
//  内置资源部分
         int  i  =   0 ;
        
for  (String imgFileName : imgFileNames) {
            MimeBodyPart jpgBody 
=   new  MimeBodyPart();
            FileDataSource fds 
=   new  FileDataSource(imgFileName);
            jpgBody.setDataHandler(
new  DataHandler(fds));
            jpgBody.setContentID(
" img "   +  i);
            relatedMulti.addBodyPart(jpgBody);
            i
++ ;
        }
        relatedBodyPart.setContent(relatedMulti);
        
return  relatedBodyPart;
    }

    
/**
     * 
     * 
@param  session
     * 
@return
     * 
@throws  Exception
     
*/
    
public  MimeMessage createMessage(String subject, String body,
            Session session, List
< String >  imgFileNames,
            List
< String >  attacheFilePaths)  throws  Exception {


        MimeMessage msg 
=   new  MimeMessage(session);
        msg.setSubject(subject, 
" gb2312 " );

        
//  创建邮件的各个MimeBodyPart部份

        
//  创建附件
        List < MimeBodyPart >  attachmentList  =   new  ArrayList < MimeBodyPart > ();

        
for  (String attachFilePath : attacheFilePaths) {
            attachmentList.add(createAttachment(attachFilePath));
        }

        MimeBodyPart content 
=  createContent(body, imgFileNames);

        
//  将邮件中各个部份组合到一个"mixed"型的MimeMultipart对象
        MimeMultipart allpart  =   new  MimeMultipart( " mixed " );
        
for  (MimeBodyPart mimeBodyPart : attachmentList) {
            allpart.addBodyPart(mimeBodyPart);
        }
        allpart.addBodyPart(content);

        
//  将上面混合型的 MimeMultipart对象作为邮件内容并保存
        msg.setContent(allpart);
        
return  msg;

    }
    

    
    
public   static  String sendMail(String emlPath, String to){
        
try  {
            String hostName 
=   " smtp.qq.com " ;
            String EMAIL_NAME 
=   " 258072761 " ;
            String EMAIL_PASSWORD 
=   " hhysbyj123456 " ;
            String from 
=   " [email protected] " ;
            Properties props 
=  System.getProperties();  //  获得系统属性对象
            props.put( " mail.smtp.host " , hostName);  //  设置SMTP主机
            props.put( " mail.smtp.auth " " true " );
            Session session 
=  Session.getDefaultInstance(props);
            InputStream fis 
=   new  FileInputStream(emlPath);
            MimeMessage message 
=   new  MimeMessage(session, fis);
            message.setRecipients(Message.RecipientType.TO, (Address[])
                     InternetAddress.parse(to));
            message.setFrom(
new  InternetAddress(from));
    
            
//  发送开始
            Transport transport  =  session.getTransport( " smtp " );
            transport.connect((String) props.get(
" mail.smtp.host " ),
            EMAIL_NAME, EMAIL_PASSWORD);
    
            transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
            transport.close();
            
return   " 发送邮件成功 " ;
        
    } 
catch  (Exception e) {
        e.printStackTrace();
    }
    
return   "" ;
    }
}

 Mime邮件的格式请见:

http://dev.csdn.net/htmls/18/18448.html


2,关于ckeditor编辑器的使用

< tr >
                    
< th width = " 15% " > 邮件内容 th >
                    
< td id= "mailContent" width = " 85% "  colspan = " 3 " >
                        
< textarea id = " content "  name = " content "  style = " display:none " > textarea >
                        
< script type = " text/javascript " >  
                        
                          CKEDITOR.replace(
' content ' ,{filebrowserUploadUrl :  ' /MailManager/ckeditor/uploader?Type=File ' ,
filebrowserImageUploadUrl : 
' /MailManager/ckeditor/uploader?Type=Image ' ,
filebrowserFlashUploadUrl : 
' /MailManager/ckeditor/uploader?Type=Flash '
          });
                            
script >

                        
< span id = " sendTimeTip " > span >
                    
td >
                
tr >

 jsp页面导入:

<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>

在制作邮件内容时,动态删除了mailContent节点,然后重新生成一个ckeditor编辑器,怎么都生成不出来,后来知道是没有销毁对象的原因,正确的销毁对象:

if (CKEDITOR.instances[ ' content ' ])//content为代替textarea的id名称
  
delete  CKEDITOR.instances[ ' content ' ];

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/talk/archive/2011/06/07/2074441.html

你可能感兴趣的:(邮件开发项目的总结)