如何在联系表单上发送电子邮件提交按钮点击..?

时间:2015-03-03 12:45:39

标签: adobe osgi cq5 aem

我现在使用FormComponent使用默认组件创建组件提交按钮单击我想向最近提交的用户发送电子邮件,我为邮件服务器配置SMTP:

http://localhost:4502/system/console/configMgr

现在我发现了如何发送电子邮件,有人可以帮助我找出我在这里失踪的内容。提前谢谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

编写电子邮件服务以发送电子邮件..我正在编写以下代码供您参考。代码包含一个界面和实现。

  • 请在console / configMgr中配置Day cq邮件服务,包括,smtp服务器,用户ID和密码。
  • 您可以使用本地smtp测试您的电子邮件功能。将Day CQ电子邮件服务中的smpt服务器设置为localhsot并从here下载smtp4dev。此工具对于测试您本地的电子邮件功能非常有用。
    希望这是有帮助的
    接口



import java.util.Map;

import org.apache.commons.mail.EmailException;

/**
 *
 * @author Dilip Boddula
 *
 */
public abstract interface EmailService {

  /**
   *
   * @param subject
   * @param message
   * @param to
   * @param from
   * @param cc
   * @throws EmailException
   */
  public void sendSimpleEmail(String subject, String message, String fromAddress, String[] to, String[] cc) throws EmailException;
}




实施



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.mailer.MailService;

@Component(label = "E-mail Service", description = "desc.")
@Service
public class EmailServiceImpl implements EmailService {
    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)
    private MailService mailService;    
    @Override
    public void sendSimpleEmail(String subject, String message, String fromAddress,String[] to,String[] cc) throws EmailException {
        try {
            List<String> toList = new ArrayList<>(Arrays.asList(to));
            List<String> ccList = new ArrayList<>(Arrays.asList(cc));            
            validateToOrCCAddresses(toList, ccList);

            Email email = new SimpleEmail();
            email.setSubject(subject);
            email.setMsg(message);
            if(null != fromAddress){
            	email.setFrom(fromAddress);
            }
            for (String toAddress : toList) {
                email.addTo(toAddress);
            }

            for (String ccAddress : ccList) {
                email.addCc(ccAddress);
            }

            this.mailService.send(email);
        } catch (Exception e) {
            throw new EmailException(e);
        }
    }
      private void validateToOrCCAddresses(List<String> toList, List<String> ccList) {
        if ((toList == null) || (toList.isEmpty())) {
            throw new IllegalArgumentException("Invalid To Addresses.");
        }
        if (ccList == null) {
            throw new IllegalArgumentException("Invalid CC Addresses.");
        }

        for (Iterator<String> iterator = toList.iterator(); iterator.hasNext();) {
            String recipient = "";
            try {
                recipient = iterator.next();
                new InternetAddress(recipient);
            } catch (AddressException e) {
                LOGGER.error("Invalid To email address '{}' passed to sendEmail(). Skipping." + recipient, e);
                iterator.remove();
            }
        }

        if (toList.isEmpty()) {
            throw new IllegalArgumentException("Invalid To Addresses.");
        }

        for (Iterator<String> iterator = ccList.iterator(); iterator.hasNext();) {
            String recipient = "";
            try {
                recipient = iterator.next();
                new InternetAddress(recipient);
            } catch (AddressException e) {
                LOGGER.error("Invalid CC email address '{}' passed to sendEmail(). Skipping." + recipient, e);
                iterator.remove();
            }
        }
    }
}
&#13;
&#13;
&#13;