在我的Xpages应用程序中,我想从Java发送(HTML)电子邮件。我在OpenNTF上找到了这段不错的EmailBean代码作为片段:
我将代码转换为通用的Email类,而不将其用作托管bean。
我还想以不同的方式使用代码:与其使用从XPage创建的DominoDocument,不如直接使用anotehr java类中的类。但是我面临以下问题:
代码需要一个DominoDocument和该文档上的字段作为电子邮件内容。
所以在我的sendMail方法中,我尝试过:
Database db = DominoUtils.getCurrentDatabase();
DominoDocument fakeMail = (DominoDocument) db.createDocument();
但是从未创建此DominoDocument(代码在此处中断)
Database db = DominoUtils.getCurrentDatabase();
Document fakeMail = db.createDocument();
工作正常,但
Email email = new Email();
email.setDocument(fakeMail);
说明应该使用DominoDocument而不排除Document。
我接下来的想法是在尝试尝试跳过中间文档的地方
email.setBodyHTML("Hello World");
我在控制台中收到以下消息:
[1728:0016-08FC] 2018-09-15 16:18:14 HTTP JVM:不允许使用方法setBodyHTML(string)
有没有人可以指导我如何更改此电子邮件类别,因此我不需要DominoDocument?实际上,我根本不需要任何文档。如果setBodyHTML()可以工作,我可以自己设置电子邮件对象的属性。
答案 0 :(得分:2)
在Domino中发送邮件的方法是通过文档。最简单的方法是在mail.box
中创建一个路由器数据库。在那里您只能保存一次,保存将发送消息。
但是... 在没有您提到的任何转换工作的情况下,托尼的课程应该可以正常工作。托管bean只是一个简单的Java类,具有无参数的构造函数和get / set方法。
因此,从其他Java代码中,您应该只能使用此代码:
EmailBean email = new EmailBean();
email.set(...) // to, body, subject etc
email.send();
您需要更改的内容:
get/setHTMLBody
的工作方式类似于HTMLfooter->存储在本地变量中get/setTextBody
方法和局部变量send()
方法中,而不是从文档中提取,请使用局部变量对您有用吗?
答案 1 :(得分:1)
为什么不精简该电子邮件课程?
package com.ibm.xsp.utils;
/**
* @author Tony McGuckin, IBM
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.MIMEEntity;
import lotus.domino.MIMEHeader;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.Stream;
import com.ibm.domino.xsp.module.nsf.NotesContext;
public class Email {
private ArrayList<String> sendTo;
private ArrayList<String> ccList;
private ArrayList<String> bccList;
private String senderEmail;
private String senderName;
private String subject;
private String fieldName;
private String bannerHTML;
private String bodyHTML;
private String footerHTML;
private boolean debugMode = true;
private static final Pattern imgRegExp = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
public Email(){
this.subject = "";
this.sendTo = new ArrayList<String>();
this.ccList = new ArrayList<String>();
this.bccList = new ArrayList<String>();
}
public String getSendTo(){
if(this.isDebugMode()){
System.out.println("getSendTo() : " + this.sendTo.toString());
}
return this.sendTo.toString().replace("[", "").replace("]", "");
}
public void setSendTo(final String sendTo){
this.sendTo.add(sendTo);
}
public String getCcList(){
if(this.isDebugMode()){
System.out.println("getCcList() : " + this.ccList.toString());
}
return this.ccList.toString().replace("[", "").replace("]", "");
}
public void setCcList(final String ccList){
this.ccList.add(ccList);
}
public String getBccList(){
if(this.isDebugMode()){
System.out.println("getBccList() : " + this.bccList.toString());
}
return this.bccList.toString().replace("[", "").replace("]", "");
}
public void setBccList(final String bccList){
this.bccList.add(bccList);
}
public String getSenderEmail(){
return this.senderEmail;
}
public void setSenderEmail(final String senderEmail){
this.senderEmail = senderEmail;
}
public String getSenderName(){
return this.senderName;
}
public void setSenderName(final String senderName){
this.senderName = senderName;
}
public String getSubject(){
return this.subject;
}
public void setSubject(final String subject){
this.subject = subject;
}
public boolean isDebugMode(){
return this.debugMode;
}
public void setDebugMode(final boolean debugMode){
this.debugMode = debugMode;
}
private Session getCurrentSession(){
NotesContext nc = NotesContext.getCurrentUnchecked();
return (null != nc) ? nc.getCurrentSession() : null;
}
private Database getCurrentDatabase(){
NotesContext nc = NotesContext.getCurrentUnchecked();
return (null != nc) ? nc.getCurrentDatabase() : null;
}
public void send() throws NotesException, IOException, Exception {
Session session = getCurrentSession();
Database database = getCurrentDatabase();
if (null != session && null != database &&
null != this.sendTo && null != this.subject &&
null != this.senderEmail
) {
try {
if (this.isDebugMode()) {
System.out.println("Started send()");
}
session.setConvertMime(false);
Document emailDocument = database.createDocument();
MIMEEntity emailRoot = emailDocument.createMIMEEntity("Body");
if (null != emailRoot) {
MIMEHeader emailHeader = emailRoot.createHeader("Reply-To");
emailHeader.setHeaderVal(this.getSenderEmail());
emailHeader = emailRoot.createHeader("Return-Path");
emailHeader.setHeaderVal(this.getSenderEmail());
final String fromSender = (null == this.getSenderName()) ?
this.getSenderEmail() :
"\"" + this.getSenderName() + "\" <" + this.getSenderEmail() + ">";
emailHeader = emailRoot.createHeader("From");
emailHeader.setHeaderVal(fromSender);
emailHeader = emailRoot.createHeader("Sender");
emailHeader.setHeaderVal(fromSender);
emailHeader = emailRoot.createHeader("To");
emailHeader.setHeaderVal(this.getSendTo());
if (!this.ccList.isEmpty()) {
emailHeader = emailRoot.createHeader("CC");
emailHeader.setHeaderVal(this.getCcList());
}
if (!this.bccList.isEmpty()) {
emailHeader = emailRoot.createHeader("BCC");
emailHeader.setHeaderVal(this.getBccList());
}
emailHeader = emailRoot.createHeader("Subject");
emailHeader.setHeaderVal(this.getSubject());
MIMEEntity emailRootChild = emailRoot.createChildEntity();
if (null != emailRootChild) {
final String boundary = System.currentTimeMillis() + "-" + "ABC";
emailHeader = emailRootChild.createHeader("Content-Type");
emailHeader.setHeaderVal("multipart/alternative; boundary=\"" + boundary + "\"");
MIMEEntity emailChild = emailRootChild.createChildEntity();
if (null != emailChild) {
Stream stream = session.createStream();
emailChild = emailRootChild.createChildEntity();
stream = session.createStream();
stream.writeText(this.getHTML());
emailChild.setContentFromText(stream, "text/html; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
stream.close();
stream.recycle();
stream = null;
}
}
}
emailDocument.send();
session.setConvertMime(true);
if (this.isDebugMode()) {
System.out.println("Completed send()");
}
} catch (NotesException e) {
if (this.isDebugMode()) {
System.out.println("Failed send() with NotesException" + e.getMessage());
}
throw e;
} catch (Exception e) {
if (this.isDebugMode()) {
System.out.println("Failed send() with Exception" + e.getMessage());
}
throw e;
}
}
}
public String getFieldName(){
return this.fieldName;
}
public void setFieldName(final String fieldName){
this.fieldName = fieldName;
}
public String getHTML(){
StringBuffer html = new StringBuffer();
html.append(getBannerHTML());
html.append(getBodyHTML());
html.append(getFooterHTML());
return html.toString();
}
public String getBannerHTML(){
return this.bannerHTML;
}
public void setBannerHTML(final String bannerHTML){
this.bannerHTML = bannerHTML;
}
public String getFooterHTML(){
return this.footerHTML;
}
public String getBodyHTML() {
return bodyHTML;
}
public void setBodyHTML(String bodyHTML) {
this.bodyHTML = bodyHTML;
}
public void setFooterHTML(final String footerHTML){
this.footerHTML = footerHTML;
}
}
然后在您的其他班级添加如下内容:
private void sendMail(String msg){
try{
Email email = new Email();
email.setSendTo("yourname@acme.org");
email.setSubject("Mail from Java");
email.setSenderEmail("no-rely@acme.org");
email.setSenderName("No-reply");
email.setBodyHTML(msg);
email.setBannerHTML("<p>Hi " + email.getSendTo() + ",</p>");
email.setFooterHTML("<p><b>Kind regards,</b><br/>" + email.getSenderName() + "<br/>0044 1234 5678</p>");
email.send();
} catch (Exception e) {
OpenLogUtil.logError(e);
}
}
不再读取DominoDocument。我添加了一个字段:private String bodyHTML。我已将setBodyHTML方法更改为标准的setter方法。
我精简了send()方法,主要是这一部分:
MIMEEntity emailRootChild = emailRoot.createChildEntity();
if (null != emailRootChild) {
final String boundary = System.currentTimeMillis() + "-" + "ABC";
emailHeader = emailRootChild.createHeader("Content-Type");
emailHeader.setHeaderVal("multipart/alternative; boundary=\"" + boundary + "\"");
MIMEEntity emailChild = emailRootChild.createChildEntity();
if (null != emailChild) {
Stream stream = session.createStream();
emailChild = emailRootChild.createChildEntity();
stream = session.createStream();
stream.writeText(this.getHTML());
emailChild.setContentFromText(stream, "text/html; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
stream.close();
stream.recycle();
stream = null;
}
}
最后,您只会得到基本的HTML电子邮件,没有图像或附件。我不确定这是否符合您的需求?
答案 2 :(得分:0)
我对OpenNTF Domino API进行了相同的转换过程。它具有DominoEmail
类,请参见https://stash.openntf.org/projects/ODA/repos/dominoapi/browse/domino/core/src/org/openntf/domino/email?at=30690a2ddccb024bae6fcf37cbdd42860c7e5ba6。这适用于基本的电子邮件,包括我相信的HTML。但这还不足以支持例如附件。如果有特殊需要,我很高兴有人能够完成这方面的工作,并乐意提供建议以帮助您取得进步。