如何从 Xpages 发送 Java 的电子邮件?
How to send email in Java from Xpages?
在我的 Xpages 应用程序中,我想从 Java 发送 (HTML) 封电子邮件。我在 OpenNTF 上找到了这个不错的 EmailBean 代码片段:
我将代码转换为普通电子邮件 class 并且没有将其用作托管 bean。
我还想以不同的方式使用代码:我不想使用从 XPage 创建的 DominoDocument,而是想直接使用来自另一个 java [=46] 的 class =].但是我面临以下问题:
该代码需要一个 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)
有没有人可以指导我如何更改此电子邮件 class 以便我不需要 DominoDocument?实际上我根本不需要文档。如果 setBodyHTML() 可以工作,我可以自己设置电子邮件对象的属性。
在 Domino 中发送邮件的方式是通过文档。最简单的方法是在路由器数据库 mail.box
中创建一个。在那里你只能保存一次,保存后会发送消息。
然而……
Tony 的 class 应该可以正常工作,无需您提到的任何转换工作。托管 bean 只是一个简单的 Java class,带有无参数构造函数和 get/set 方法。
所以从其他 Java 代码你应该可以只使用这个:
EmailBean email = new EmailBean();
email.set(...) // to, body, subject etc
email.send();
您需要更改的内容:
get/setHTMLBody
需要像 HTMLfooter 一样工作 -> 存储在局部变量中
- 添加一个
get/setTextBody
方法和局部变量
- 在
send()
方法中使用局部变量而不是从文档中提取
这对你有用吗?
为什么不删除那封电子邮件 class?
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;
}
}
然后从你的另一个 class 添加这样的内容:
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 电子邮件,没有图片或附件。我不确定这是否符合您的需求?
我对 OpenNTF Domino 进行了相同的转换过程 API。它有一个 DominoEmail
class,参见 https://stash.openntf.org/projects/ODA/repos/dominoapi/browse/domino/core/src/org/openntf/domino/email?at=30690a2ddccb024bae6fcf37cbdd42860c7e5ba6。这适用于基本电子邮件,我相信包括 HTML。但它还不完整,例如支持附件。我很高兴有人能够完成这方面的工作,如果有特定的需要,我很乐意提供建议以帮助取得进展。
在我的 Xpages 应用程序中,我想从 Java 发送 (HTML) 封电子邮件。我在 OpenNTF 上找到了这个不错的 EmailBean 代码片段:
我将代码转换为普通电子邮件 class 并且没有将其用作托管 bean。
我还想以不同的方式使用代码:我不想使用从 XPage 创建的 DominoDocument,而是想直接使用来自另一个 java [=46] 的 class =].但是我面临以下问题:
该代码需要一个 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)
有没有人可以指导我如何更改此电子邮件 class 以便我不需要 DominoDocument?实际上我根本不需要文档。如果 setBodyHTML() 可以工作,我可以自己设置电子邮件对象的属性。
在 Domino 中发送邮件的方式是通过文档。最简单的方法是在路由器数据库 mail.box
中创建一个。在那里你只能保存一次,保存后会发送消息。
然而…… Tony 的 class 应该可以正常工作,无需您提到的任何转换工作。托管 bean 只是一个简单的 Java class,带有无参数构造函数和 get/set 方法。
所以从其他 Java 代码你应该可以只使用这个:
EmailBean email = new EmailBean();
email.set(...) // to, body, subject etc
email.send();
您需要更改的内容:
get/setHTMLBody
需要像 HTMLfooter 一样工作 -> 存储在局部变量中- 添加一个
get/setTextBody
方法和局部变量 - 在
send()
方法中使用局部变量而不是从文档中提取
这对你有用吗?
为什么不删除那封电子邮件 class?
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;
}
}
然后从你的另一个 class 添加这样的内容:
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 电子邮件,没有图片或附件。我不确定这是否符合您的需求?
我对 OpenNTF Domino 进行了相同的转换过程 API。它有一个 DominoEmail
class,参见 https://stash.openntf.org/projects/ODA/repos/dominoapi/browse/domino/core/src/org/openntf/domino/email?at=30690a2ddccb024bae6fcf37cbdd42860c7e5ba6。这适用于基本电子邮件,我相信包括 HTML。但它还不完整,例如支持附件。我很高兴有人能够完成这方面的工作,如果有特定的需要,我很乐意提供建议以帮助取得进展。