我现在使用FormComponent使用默认组件创建组件提交按钮单击我想向最近提交的用户发送电子邮件,我为邮件服务器配置SMTP:
http://localhost:4502/system/console/configMgr
现在我发现了如何发送电子邮件,有人可以帮助我找出我在这里失踪的内容。提前谢谢。
答案 0 :(得分:0)
无论如何,就我理解你的问题而言,似乎你不知道如何使用电子邮件API。请查看此链接http://labs.sixdimensions.com/blog/2012-08-20/sending-email-adobe-cq-api/。
答案 1 :(得分:0)
编写电子邮件服务以发送电子邮件..我正在编写以下代码供您参考。代码包含一个界面和实现。
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;