如何使用Java从Xpages发送电子邮件?
在我的Xpages应用程序中,我想从Java发送(HTML)电子邮件。我在OpenNTF上发现了这个漂亮的EmailBean代码片段: 我将代码转换为一个公共电子邮件类,而不是将其用作托管bean 我还想以不同的方式使用代码:我不想使用从XPage创建的DominoDocument,而是直接使用anotehr java类中的类。然而,我面临以下问题: 代码需要一个DominoDocument和该文档上的一个字段作为电子邮件的内容 因此,在我的sendMail方法中,我尝试了:如何使用Java从Xpages发送电子邮件?,java,xpages,html-email,Java,Xpages,Html Email,在我的Xpages应用程序中,我想从Java发送(HTML)电子邮件。我在OpenNTF上发现了这个漂亮的EmailBean代码片段: 我将代码转换为一个公共电子邮件类,而不是将其用作托管bean 我还想以不同的方式使用代码:我不想使用从XPage创建的DominoDocument,而是直接使用anotehr java类中的类。然而,我面临以下问题: 代码需要一个DominoDocument和该文档上的一个字段作为电子邮件的内容 因此,在我的sendMail方法中,我尝试了: Database
Database db = DominoUtils.getCurrentDatabase();
DominoDocument fakeMail = (DominoDocument) db.createDocument();
但此DominoDocument从未创建(代码在此中断)
很好,但是
Email email = new Email();
email.setDocument(fakeMail);
投诉预期会有DominoDocument,但文档也不例外
我的下一个想法是跳过中间文档的创建,但是当我尝试
email.setBodyHTML("Hello World");
我在控制台中收到以下消息:
[1728:0016-08FC]2018-09-15 16:18:14 HTTP JVM:不允许使用方法setBodyHTML(字符串)
是否有人可以指导我如何更改此电子邮件类,以便我不需要DominoDocument?实际上我根本不需要文件。如果setBodyHTML()可以工作,我可以自己设置电子邮件对象的属性。在Domino中发送邮件的方法是通过文档。最简单的方法是在路由器数据库
mail.box
中创建一个。在那里,您只能保存一次,保存将发送消息
然而。。。。
托尼的课应该很好,没有你提到的任何转换工作。托管bean只是一个简单的Java类,具有无参数构造函数和get/set方法
因此,从其他Java代码中,您应该能够使用以下内容:
EmailBean email = new EmailBean();
email.set(...) // to, body, subject etc
email.send();
您需要更改的内容:
需要像HTMLfooter->存储在局部变量中一样工作get/setHTMLBody
- 添加
方法和局部变量get/setTextBody
- 在
方法中,使用局部变量,而不是从文档中提取send()
这对您有用吗?在Domino中发送邮件的方式是通过文档。最简单的方法是在路由器数据库
mail.box
中创建一个。在那里,您只能保存一次,保存将发送消息
然而。。。。
托尼的课应该很好,没有你提到的任何转换工作。托管bean只是一个简单的Java类,具有无参数构造函数和get/set方法
因此,从其他Java代码中,您应该能够使用以下内容:
EmailBean email = new EmailBean();
email.set(...) // to, body, subject etc
email.send();
您需要更改的内容:
需要像HTMLfooter->存储在局部变量中一样工作get/setHTMLBody
- 添加
方法和局部变量get/setTextBody
- 在
方法中,使用局部变量,而不是从文档中提取send()
这对你有用吗?为什么不取消电子邮件课程
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;
}
}
最后你只会收到一封基本的HTML电子邮件,没有图片或附件。我不确定这是否符合您的需要?为什么不取消电子邮件课程
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;
}
}
最后你只会收到一封基本的HTML电子邮件,没有图片或附件。我不确定这是否符合您的需要?我对OpenNTF Domino API进行了相同的转换过程。它有一个
DominoEmail
类,请参阅。这适用于基本的电子邮件,包括我相信的HTML。但对于支持附件等而言,它并不完整。如果有特殊需要,我很高兴有人能够完成这方面的工作,并很高兴提供建议以帮助进步。我也经历了相同的OpenNTF Domino API转换过程。它有一个DominoEmail
类,请参阅。这适用于基本的电子邮件,包括我相信的HTML。但对于支持附件等而言,它并不完整。我很高兴有人能够完成这方面的工作,如果有特殊需要,我也很高兴提供建议以帮助进步。你是指通过文档(而不是DominoDocument)你是指通过文档(而不是DominoDocument)谢谢你,Patrick,我使用了你的代码,现在我可以发送电子邮件了!谢谢你,帕特里克,我使用了你的密码,现在我可以发送电子邮件了!
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;
}
}