Javamail use ical4j and send meeting invitations to send e-mail alerts to achieve

Based JavaMail send calendar (conference) Mail

/ ** 
     * 
     * @param senderAccount sender account 
     * @param senderPassword sender's password 
     * @param toAddress recipient email address 
     * @param CCAddress Cc address 
     * @param title noticeTitle subject of 
     * @param noticeContent of Subject content 
     * @param criticalityClass message importance level 
     * @param workShop workshop 
     * @param line line body 
     * @param attachmentPath attachment path 
     * @return 
     * /
    private String sendEmailUtil(String senderAccount, String senderPassword,
                                 String toAddress, String ccAddress, String noticeTitle,String noticeContent,String criticalityClass,String workShop,String line,String attachmentPath) {
        String from=senderAccount+"@sunwoda.com";
        String to=toAddress;
        String location="车间:"+workShop+"  线体:"+line;
        final String sendAccount=senderAccount;
        final String sendPwd=senderPassword;
        //Get email server                                                                                                                                                   
        the Properties props = new new the Properties (); 
        props.put ( "mail.transport.protocol", "the SMTP"); // mail protocol                                                                                                        
        props.put ( "mail.smtp.host", "smtp.sunwoda.com "); // server domain name                                                                                                    
        props.put (" mail.smtp.auth "," true          ");// set the authentication mode             

        Authenticator auth = new newThe Authenticator () { 
            @Override 
            protected PasswordAuthentication the getPasswordAuthentication () { 
                String username = sendAccount; // Most of your message before @                                                                                                    
                String pwd = sendPwd;
                 return  new new PasswordAuthentication (username, pwd); 
            } 
        }; 
        the Session the mailSession = the Session. getInstance (props, auth);
         //Get Message Object                                                                                                                                               
        Message MSG = new new the MimeMessage (the mailSession);
         the try {
             // set the mail basic information                                                                                                                                              
            msg.setFrom ( new new the InternetAddress (from)); 
            msg.setRecipients (Message.RecipientType.TO, InternetAddress.parse (the toAddress));                                                                                   
                / * IF (the toAddress! = null &&! toAddress.isEmpty ()) {
                     InternetAddress[] cc = new InternetAddress().parse(toAddress);
                     msg.setRecipients(Message.RecipientType.TO,cc);
                 }*/

            if (ccAddress != null && !ccAddress.isEmpty()){
                InternetAddress[] cc = new InternetAddress().parse(ccAddress);
                msg.setRecipients(Message.RecipientType.CC,cc);
            }
            msg.setSentDate(new java.util.Date());
            msg.setSubject(noticeTitle);
            msg.addHeader("ReturnReceipt", "1");
            if(criticalityClass.equals ( "High" )) { 
                msg.addHeader ( "X--the Priority", ". 1" ); 
            } the else  IF (criticalityClass.equals ( "in" )) { 
                msg.addHeader ( "X--the Priority", ". 3" ); 
            } the else { 
                msg.addHeader ( "X--the Priority", ". 5" ); 
            } 

            // Get message content of the message types                                                                                                                                        
            Multipart MP = getContentText (from, to, LOCATION, noticeTitle, noticeContent,criticalityClass,attachmentPath); 
            msg.setContent (MP); 
            MSG.saveChanges();
            Transport.send(msg);

            //发送完后删除临时文件
            int index=attachmentPath.indexOf('/');
            String savePath = StringUtils.getStr(new String[] { this.importFilePath});
            String fileName=attachmentPath.substring(index+1);
            File file = new File(savePath+File.separator+fileName);
            file.delete();

            return "OK";
        } catch (Exception ex) {
            ex.printStackTrace();
            String errorInfoEx.getMessage = () the substring (0,3. );
             IF (errorInfo.equals ( "535" )) {
                 return "the sender's mailbox or account password is wrong!" ; 
            } The else {
                 return "or Cc ! The email address error " ; 
            } 
        } 
    } 

    Private Multipart getContentText (from String, String to, LOCATION1 String, String noticeTitle, noticeContent String, 
                                     String criticalityClass, String attachmentPath) throws   Exception { 
        String Subject = noticeTitle +" - "+ noticeContent;// theme inner tube contents 
        String content = noticeContent;
        //取附件路径和名称
        int index=attachmentPath.indexOf('/');
        String filePath="/"+attachmentPath.substring(0, index);
        String fileName=attachmentPath.substring(index+1);
        // 时区                                                                                                                                                       
        TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
        TimeZone timezone = registry.getTimeZone("Asia/Shanghai");
        // 会议地点                                                                                                                                                     
        String location = location1;
        // 会议时间                                                                                                                                                     
        java.util.Calendar cal = java.util.Calendar.getInstance();
        int year=cal.get(java.util.Calendar.YEAR);
        int month=cal.get(java.util.Calendar.MONTH) + 1;
        int day=cal.get(java.util.Calendar.DAY_OF_MONTH);
        int hour=cal.get(java.util.Calendar.HOUR_OF_DAY);
        int minute=cal.get(java.util.Calendar.MINUTE)+5;

        cal.setTimeZone(timezone);
        cal.set(year, month - 1, day, hour, minute); // 月份是要早一个月                                                                                                               
        DateTime start = new DateTime(cal.getTime());
        cal.set(year, month - 1, day, hour+8, minute);
        DateTime end = new DateTime(cal.getTime());

        VEvent vevent = new VEvent(start, end, subject);
        vevent.getProperties () the Add (timezone.getVTimeZone () getTimeZoneId ().);. // time zone                                                                                    
        vevent.getProperties () the Add (. new new the Location (LOCATION)); // venue         
        . vevent.getProperties () the Add ( new new Description ( "announcement:" + "\ the n-" + content)); // message content                                                                                                 
        vevent.getProperties (.) the Add ( new new . UidGenerator ( "uidGen") generateUid ()); // set uid                                                                               
         //组织者                                                                                                                                                       
        vevent.getProperties().add(new Organizer(URI.create("mailto:" + from)));
        // 与会人                                                                                                                                                      
        Set<String> emailSet = new HashSet<String>();
        emailSet.add(from);
        emailSet.add(to);
        int i = 1;
        for (String email : emailSet) {
            Attendee attendee = new Attendee(URI.create("mailto:" + email));
            if (1 == i) {
                attendee.getParameters().add(Role.REQ_PARTICIPANT);
            } else {
                attendee.getParameters().add(Role.OPT_PARTICIPANT);
            }
            attendee.getParameters().add(new Cn("Developer" + i));
            vevent.getProperties().add(attendee);
            i++;
        }
        // --------VEvent Over----------                                                                                                                            
        // -------- ---------- Start VAlarm                                                                                                                           
         // ti_xing, ti_qian 5 fen_zhong                                                                                                                                                  
        VAlarm valarm = new VAlarm ( new Dur (0, 0, -5, 0 )); 
        valarmgetProperties (). add ( new Repeat (10 )); 
        valarmgetProperties (). add ( new Duration ( new Dur (0, 0, 5, 0 )));
        //Reminder window displays text messages                                                                                                                                               
        valarm.getProperties () the Add (. New new the Summary ( "Alarm Event" )); 
        . Valarm.getProperties () the Add (Action.DISPLAY); 
        . Valarm.getProperties () the Add ( new new the Description ( "Conference remind description, to be determined, uncertain use " )); 
        . vevent.getAlarms () the Add (VALARM); // will VAlarm join VEVENT                                                                                                           
         // --------VAlarm Over-------------                                                                                                                         
        // --------日历对象 Start---------------                                                                                                                        
        Calendar icsCalendar = new Calendar();
        icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
        icsCalendar.getProperties().add(Version.VERSION_2_0);
        icsCalendar.getProperties().add(CalScale.GREGORIAN);
        icsCalendar.getProperties().add(timezone.getVTimeZone().getTimeZoneId());
        icsCalendar.getComponents () the Add (VEVENT);. // The Calendar VEvent added    

        // converts a Calendar object to a binary stream                                                                                                                                              
        CalendarOutputter = CO.'S new new CalendarOutputter ( to false ); 
        ByteArrayOutputStream OS = new new ByteArrayOutputStream (); 
        co.output (icsCalendar, OS); 
        byte [] = mailbytes os.toByteArray (); 

        // . 9. Create Annex "node" 
        the MimeBodyPart attachment = new new the MimeBodyPart ();
         // download from the FTP file to a local       
        StringUtils.getStr savepath = String ( new new String [] { the this .importFilePath}); 

        downFileFroFTP ( "192.168.x.xxx", 21 is, "xxxxxx", "XXXXX" , filePath, fileName, savepath); 

        // read local file 
        the DataHandler DH2 = new new the DataHandler ( new new the FileDataSource (savepath the File.separator + + fileName));
         // add to the attachment data "node" 
        attachment.setDataHandler (DH2);
         // set the attachment filename (encoding requires) 
        attachment.setFileName (MimeUtility.encodeText (dh2.getName ())); 


        // -------- ------------------ calendar object Over              
        MimeMultipart mm = new new MimeMultipart (); 
        the MimeBodyPart iCalAttachment = new new MimeBodyPart();
        iCalAttachment.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(mailbytes),
                "text/calendar;method=REQUEST;charset=UTF-8")));
        mm.addBodyPart(iCalAttachment);
        mm.addBodyPart(attachment);     // 添加附件
        mm.setSubType("related");
        return mm;
    }

 

Guess you like

Origin www.cnblogs.com/xidianlxf/p/11032982.html