消息传送要点 messaging-essentials
本页记录了有关使用消息传送组件在网站上包含消息传送功能的详细信息。
客户端要点 essentials-for-client-side
撰写消息
消息列表 (用于收件箱、已发送和垃圾桶)
另请参阅 客户端自定义
服务器端要点 essentials-for-server-side
setInboxPath()setSentItemsPath()
| code language-none | 
|---|
                      
                     | 
                  
社区站点 community-site
使用向导创建的社区站点结构将在选择时包含消息传送功能。 请参阅 User Management 设置 社区站点控制台.
示例代码:消息接收通知 sample-code-message-received-notification
例如,社交消息功能引发操作事件 send, marking read, marking delete. 可以捕获这些事件并对事件中包含的数据执行相应操作。
以下示例是一个事件处理程序,用于侦听 message sent 事件,并使用 Day CQ Mail Service.
要尝试服务器端示例脚本,您需要开发环境并能够构建翱厂骋颈包。
- 
                  
以管理员身份登录
[CRXDE|Lite](http://localhost:4502/crx/de) - 
                  
创建
bundle nodein/apps/engage/install具有任意名称,例如- 符号名称:com.engage.media.social.messaging.messagingNotification
 - 名称:入门教程消息通知
 - 描述:向用户发送电子邮件通知的示例服务
 - 包: 
com.engage.media.social.messaging.notification 
 - 
                  
导航至
/apps/engage/install/com.engage.media.social.messaging.MessagingNotification/src/main/java/com/engage/media/social/messaging/notification- 删除 
Activator.java自动创建的类 - 创建类 
MessageEventHandler.java - 将下面的代码复制/粘贴到 
MessageEventHandler.java 
 - 删除 
 - 
                  
单击 全部保存
 - 
                  
导航到
/apps/engage/install/com.engage.media.social.messaging.MessagingNotification/com.engage.media.social.messaging.MessagingNotification.bnd并按照MessageEventHandler.java代码。 - 
                  
构建包
 - 
                  
确保
Day CQ Mail Service翱厂骋颈服务已配置 - 
                  
以演示用户身份登录,并向其他用户发送电子邮件
 - 
                  
收件人应收到一封对于新消息的电子邮件
 
MessageEventHandler.java messageeventhandler-java
package com.engage.media.social.messaging.notification;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.HtmlEmail;
import java.util.List;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.social.messaging.api.Message;
import com.adobe.cq.social.messaging.api.MessagingEvent;
import com.day.cq.mailer.MessageGatewayService;
import com.day.cq.mailer.MessageGateway;
@Component(immediate=true)
@Service(EventHandler.class)
@Properties({
        @Property(name = "event.topics", value = "com/adobe/cq/social/message")
})
public class MessagingEventHandler implements EventHandler {
    private Logger logger = LoggerFactory.getLogger(MessagingEventHandler.class);
    @Reference
    ResourceResolverFactory resourceResolverFactory;
    @Reference
    private MessageGatewayService messageGatewayService;
    ResourceResolver resourceResolver=null;
    MessageGateway messageGateway=null;
    public void sendMail(String from, String to,String subject, String content){
        Email email = new SimpleEmail();
        messageGateway = messageGatewayService.getGateway(SimpleEmail.class);
        try {
         email.addTo(to);
            email.addReplyTo(from);
            email.setFrom(from);
            email.setMsg(content);
            email.setSubject(subject);
         messageGateway.send(email);
        } catch(EmailException ex) {
            logger.error("MessageNotificaiton : Error sending email : "+ex.getMessage());
        }
        logger.info("**** MessageNotification **** Mail sent to " + to);
    }
    public void handleEvent(Event event) {
        //Get Message Path and originator User's ID from event
        String messagePath = (String) event.getProperty("path");
        String senderId = (String) event.getProperty("userId");
        MessagingEvent.MessagingActions action = (MessagingEvent.MessagingActions) event.getProperty("action");
        try{
            if(MessagingEvent.MessagingActions.MessageSent.equals(action)){
                resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
                //Read message
                Resource resource = resourceResolver.getResource(messagePath);
                Message msg = resource.adaptTo(Message.class);
                //Get list of recipient Ids from message
                //For Getting Started Tutorial, Id is same as email. If that is not the case in your site,
                //an additional step is needed to retrieve the email for the Id
                List<String> reclist = msg.getRecipientIdList();
                for(int i=0;i<reclist.size();i++){
                    //Send Email using Mailing Service
                    sendMail("admin@cqadmin.qqq",reclist.get(i),"New message on Getting Started Tutorial", "Hi\nYou have received a new message from  " +  senderId + ". To read it, sign in to Getting Started Tutorial.\n\n-Engage Admin");
                }
            }
        } catch(Exception ex){
            logger.error("Error getting message info : " + ex.getMessage());
        } finally {
            resourceResolver.close();
        }
    }
}