Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.30 KB | None | 0 0
  1. Unknown member 'setUNID' in Java class 'com.scoular.utls.Email'
  2.  
  3. <faces-config>
  4. <managed-bean>
  5. <managed-bean-name>Email</managed-bean-name>
  6. <managed-bean-class>com.scoular.utls.Email</managed-bean-class>
  7. <managed-bean-scope>request</managed-bean-scope>
  8. <managed-property>
  9. <property-name>debugMode</property-name>
  10. <value>true</value>
  11. </managed-property>
  12. </managed-bean>
  13. </faces-config>
  14.  
  15. //Need to get email document
  16. var emlView = database.getView("xpViewEmailsAll");
  17. var emlDoc = emlView.getFirstDocument();
  18. if (emlDoc != null) {
  19.  
  20.  
  21. //try{
  22. var subject = ""
  23. var senderEmail = supEml
  24. var senderName = supNme
  25.  
  26. Email.setSendTo("John");
  27. Email.setSubject("subject");
  28. Email.setSenderEmail("John@gmal.com");
  29. Email.setUNID = emlDoc.getUniversalID();
  30. Email.setSenderName("Sender");
  31. //Email.setBackEndDocument(emlDoc);
  32. Email.setFieldName("Body");
  33. Email.send();
  34. //}catch(e){
  35. //print(e.getMessage());
  36. //}
  37. }
  38.  
  39. package com.scoular.utls;
  40.  
  41. import java.io.IOException;
  42. import java.io.InputStream;
  43. import java.util.ArrayList;
  44. import java.util.List;
  45. import java.util.regex.Matcher;
  46. import java.util.regex.Pattern;
  47.  
  48. import javax.faces.context.FacesContext;
  49.  
  50. import lotus.domino.Database;
  51. import lotus.domino.Document;
  52. import lotus.domino.EmbeddedObject;
  53. import lotus.domino.MIMEEntity;
  54. import lotus.domino.MIMEHeader;
  55. import lotus.domino.NotesException;
  56. import lotus.domino.Session;
  57. import lotus.domino.Stream;
  58.  
  59. import com.ibm.commons.util.NotImplementedException;
  60. import com.ibm.domino.xsp.module.nsf.NotesContext;
  61. import com.ibm.xsp.model.FileRowData;
  62. import com.ibm.xsp.model.domino.wrapped.DominoDocument;
  63. import com.ibm.xsp.model.domino.wrapped.DominoRichTextItem;
  64. import com.ibm.xsp.model.domino.wrapped.DominoDocument.AttachmentValueHolder;
  65. import com.ibm.xsp.persistence.PersistedContent;
  66.  
  67. public class Email {
  68.  
  69. private ArrayList<String> sendTo;
  70. private ArrayList<String> ccList;
  71. private ArrayList<String> bccList;
  72. private String senderEmail;
  73. private String senderName;
  74. private String subject;
  75. private DominoDocument document;
  76. private String fieldName;
  77. private String bannerHTML;
  78. private String footerHTML;
  79. private String unid;
  80. private boolean debugMode = false;
  81. private static final Pattern imgRegExp = Pattern.compile("<img[^>]+src\s*=\s*['"]([^'"]+)['"][^>]*>");
  82.  
  83. // -------------------------------------------------------------------------
  84.  
  85. public Email() {
  86. this.subject = "";
  87. this.sendTo = new ArrayList<String>();
  88. this.ccList = new ArrayList<String>();
  89. this.bccList = new ArrayList<String>();
  90. }
  91.  
  92. // -------------------------------------------------------------------------
  93.  
  94. public String getSendTo() {
  95. if (this.isDebugMode()) {
  96. System.out.println("getSendTo() : " + this.sendTo.toString());
  97. }
  98. return this.sendTo.toString().replace("[", "").replace("]", "");
  99. }
  100.  
  101. public void setSendTo(final String sendTo) {
  102. this.sendTo.add(sendTo);
  103. }
  104.  
  105. // -------------------------------------------------------------------------
  106.  
  107. public String getCcList() {
  108. if (this.isDebugMode()) {
  109. System.out.println("getCcList() : " + this.ccList.toString());
  110. }
  111. return this.ccList.toString().replace("[", "").replace("]", "");
  112. }
  113.  
  114. public void setCcList(final String ccList) {
  115. this.ccList.add(ccList);
  116. }
  117.  
  118. // -------------------------------------------------------------------------
  119.  
  120. public String getBccList() {
  121. if (this.isDebugMode()) {
  122. System.out.println("getBccList() : " + this.bccList.toString());
  123. }
  124. return this.bccList.toString().replace("[", "").replace("]", "");
  125. }
  126.  
  127. public void setBccList(final String bccList) {
  128. this.bccList.add(bccList);
  129. }
  130.  
  131. // -------------------------------------------------------------------------
  132.  
  133. public String getSenderEmail() {
  134. return this.senderEmail;
  135. }
  136.  
  137. public void setSenderEmail(final String senderEmail) {
  138. this.senderEmail = senderEmail;
  139. }
  140.  
  141. // -------------------------------------------------------------------------
  142.  
  143. public String getSenderName() {
  144. return this.senderName;
  145. }
  146.  
  147. public void setSenderName(final String senderName) {
  148. this.senderName = senderName;
  149. }
  150.  
  151. // -------------------------------------------------------------------------
  152.  
  153. public String getSubject() {
  154. return this.subject;
  155. }
  156.  
  157. public void setSubject(final String subject) {
  158. this.subject = subject;
  159. }
  160.  
  161. // -------------------------------------------------------------------------
  162.  
  163. public boolean isDebugMode() {
  164. return this.debugMode;
  165. }
  166.  
  167. public void setDebugMode(final boolean debugMode) {
  168. this.debugMode = debugMode;
  169. }
  170.  
  171. // -------------------------------------------------------------------------
  172.  
  173. private Session getCurrentSession() {
  174. NotesContext nc = NotesContext.getCurrentUnchecked();
  175. return (null != nc) ? nc.getCurrentSession() : null;
  176. }
  177.  
  178. // -------------------------------------------------------------------------
  179.  
  180. private Database getCurrentDatabase() {
  181. NotesContext nc = NotesContext.getCurrentUnchecked();
  182. return (null != nc) ? nc.getCurrentDatabase() : null;
  183. }
  184.  
  185. // -------------------------------------------------------------------------
  186.  
  187. public void send() throws NotesException, IOException, Exception {
  188. Session session = getCurrentSession();
  189. Database database = getCurrentDatabase();
  190. if (null != session && null != database && null != this.sendTo && null != this.subject
  191. && null != this.senderEmail) {
  192. try {
  193. if (this.isDebugMode()) {
  194. System.out.println("Started send()");
  195. }
  196. session.setConvertMime(false);
  197. Document emailDocument = database.createDocument();
  198.  
  199. MIMEEntity emailRoot = emailDocument.createMIMEEntity("Body");
  200. if (null != emailRoot) {
  201. MIMEHeader emailHeader = emailRoot.createHeader("Reply-To");
  202. emailHeader.setHeaderVal(this.getSenderEmail());
  203.  
  204. emailHeader = emailRoot.createHeader("Return-Path");
  205. emailHeader.setHeaderVal(this.getSenderEmail());
  206.  
  207. final String fromSender = (null == this.getSenderName()) ? this.getSenderEmail() : """
  208. + this.getSenderName() + "" <" + this.getSenderEmail() + ">";
  209.  
  210. emailHeader = emailRoot.createHeader("From");
  211. emailHeader.setHeaderVal(fromSender);
  212.  
  213. emailHeader = emailRoot.createHeader("Sender");
  214. emailHeader.setHeaderVal(fromSender);
  215.  
  216. emailHeader = emailRoot.createHeader("To");
  217. emailHeader.setHeaderVal(this.getSendTo());
  218.  
  219. if (!this.ccList.isEmpty()) {
  220. emailHeader = emailRoot.createHeader("CC");
  221. emailHeader.setHeaderVal(this.getCcList());
  222. }
  223.  
  224. if (!this.bccList.isEmpty()) {
  225. emailHeader = emailRoot.createHeader("BCC");
  226. emailHeader.setHeaderVal(this.getBccList());
  227. }
  228.  
  229. emailHeader = emailRoot.createHeader("Subject");
  230. emailHeader.setHeaderVal(this.getSubject());
  231.  
  232. MIMEEntity emailRootChild = emailRoot.createChildEntity();
  233. if (null != emailRootChild) {
  234. final String boundary = System.currentTimeMillis() + "-" + this.document.getDocumentId();
  235. emailHeader = emailRootChild.createHeader("Content-Type");
  236. emailHeader.setHeaderVal("multipart/alternative; boundary="" + boundary + """);
  237.  
  238. MIMEEntity emailChild = emailRootChild.createChildEntity();
  239. if (null != emailChild) {
  240. final String contentAsText = this.document.getRichTextItem(this.fieldName)
  241. .getContentAsText();
  242. Stream stream = session.createStream();
  243. stream.writeText(contentAsText);
  244. emailChild.setContentFromText(stream, "text/plain; charset="UTF-8"", MIMEEntity.ENC_NONE);
  245. stream.close();
  246.  
  247. emailChild = emailRootChild.createChildEntity();
  248. stream = session.createStream();
  249. stream.writeText(this.getHTML());
  250. emailChild.setContentFromText(stream, "text/html; charset="UTF-8"", MIMEEntity.ENC_NONE);
  251. stream.close();
  252. stream.recycle();
  253. stream = null;
  254. }
  255.  
  256. // add embedded images....
  257. final List<FileRowData> embeddedImages = this.getEmbeddedImagesList();
  258. if (null != embeddedImages && !embeddedImages.isEmpty()) {
  259. if (this.isDebugMode()) {
  260. System.out.println("Adding Embedded Images...");
  261. }
  262. for (FileRowData embeddedImage : embeddedImages) {
  263. emailRootChild = emailRoot.createChildEntity();
  264. if (null != emailRootChild && embeddedImage instanceof AttachmentValueHolder) {
  265. InputStream is = null;
  266. try {
  267. String persistentName = ((AttachmentValueHolder) embeddedImage)
  268. .getPersistentName();
  269. String cid = ((AttachmentValueHolder) embeddedImage).getCID();
  270. emailHeader = emailRootChild.createHeader("Content-Disposition");
  271. emailHeader.setHeaderVal("inline; filename="" + persistentName + """);
  272. emailHeader = emailRootChild.createHeader("Content-ID");
  273. emailHeader.setHeaderVal("<" + cid + ">");
  274. is = this.getEmbeddedImageStream(persistentName);
  275. Stream stream = session.createStream();
  276. stream.setContents(is);
  277. emailRootChild.setContentFromBytes(stream, embeddedImage.getType(),
  278. MIMEEntity.ENC_IDENTITY_BINARY);
  279. if (this.isDebugMode()) {
  280. System.out.println("Added Embedded Image : " + persistentName);
  281. }
  282. } catch (IOException e) {
  283. if (this.isDebugMode()) {
  284. System.out.println("Adding Embedded Image failed : " + e.getMessage());
  285. }
  286. throw e;
  287. } finally {
  288. if (null != is) {
  289. is.close();
  290. is = null;
  291. }
  292. }
  293. }
  294. }
  295. if (this.isDebugMode()) {
  296. System.out.println("Completed Adding Embedded Images");
  297. }
  298. }
  299.  
  300. // add attachments....
  301. final List<FileRowData> attachments = this.getDocument().getAttachmentList(this.getFieldName());
  302. if (null != attachments && !attachments.isEmpty()) {
  303. if (this.isDebugMode()) {
  304. System.out.println("Adding Attachments...");
  305. }
  306. for (FileRowData attachment : attachments) {
  307. emailRootChild = emailRoot.createChildEntity();
  308. if (null != emailRootChild && attachment instanceof AttachmentValueHolder) {
  309. InputStream is = null;
  310. try {
  311. String persistentName = ((AttachmentValueHolder) attachment)
  312. .getPersistentName();
  313. String cid = ((AttachmentValueHolder) attachment).getCID();
  314. EmbeddedObject eo = this.getDocument().getDocument().getAttachment(
  315. persistentName);
  316. if (null != eo) {
  317. emailHeader = emailRootChild.createHeader("Content-Disposition");
  318. emailHeader.setHeaderVal("attachment; filename="" + persistentName + """);
  319. emailHeader = emailRootChild.createHeader("Content-ID");
  320. emailHeader.setHeaderVal("<" + cid + ">");
  321. is = eo.getInputStream();
  322. Stream stream = session.createStream();
  323. stream.setContents(is);
  324. emailRootChild.setContentFromBytes(stream, attachment.getType(),
  325. MIMEEntity.ENC_IDENTITY_BINARY);
  326. if (this.isDebugMode()) {
  327. System.out.println("Added Attachment : " + persistentName);
  328. }
  329. }
  330. } catch (Exception e) {
  331. if (this.isDebugMode()) {
  332. System.out.println("Adding Attachment failed : " + e.getMessage());
  333. }
  334. throw e;
  335. } finally {
  336. if (null != is) {
  337. is.close();
  338. is = null;
  339. }
  340. }
  341. }
  342. }
  343. if (this.isDebugMode()) {
  344. System.out.println("Completed Adding Attachments");
  345. }
  346. }
  347. }
  348. }
  349. emailDocument.send();
  350. session.setConvertMime(true);
  351. if (this.isDebugMode()) {
  352. System.out.println("Completed send()");
  353. }
  354. } catch (NotesException e) {
  355. if (this.isDebugMode()) {
  356. System.out.println("Failed send() with NotesException" + e.getMessage());
  357. }
  358. throw e;
  359. } catch (IOException e) {
  360. if (this.isDebugMode()) {
  361. System.out.println("Failed send() with IOException" + e.getMessage());
  362. }
  363. throw e;
  364. } catch (Exception e) {
  365. if (this.isDebugMode()) {
  366. System.out.println("Failed send() with Exception" + e.getMessage());
  367. }
  368. throw e;
  369. }
  370. }
  371. }
  372.  
  373. // -------------------------------------------------------------------------
  374.  
  375. public DominoDocument getDocument() {
  376. return this.document;
  377. }
  378.  
  379. public void setDocument(final DominoDocument document) {
  380. this.document = document;
  381. }
  382.  
  383. // -------------------------------------------------------------------------
  384.  
  385. // -------------------------------------------------------------------------
  386.  
  387. public String getFieldName() {
  388. return this.fieldName;
  389. }
  390.  
  391. public void setFieldName(final String fieldName) {
  392. this.fieldName = fieldName;
  393. }
  394.  
  395. // -------------------------------------------------------------------------
  396.  
  397. public List<FileRowData> getEmbeddedImagesList() throws NotesException {
  398. if (null != document && null != fieldName) {
  399. return document.getEmbeddedImagesList(fieldName);
  400. }
  401. return null;
  402. }
  403.  
  404. // -------------------------------------------------------------------------
  405.  
  406. private InputStream getEmbeddedImageStream(final String fileName) throws NotesException, IOException {
  407. if (null != document && null != fieldName && null != fileName) {
  408. final DominoRichTextItem drti = document.getRichTextItem(fieldName);
  409. if (null != drti) {
  410. final PersistedContent pc = drti.getPersistedContent(FacesContext.getCurrentInstance(), fieldName,
  411. fileName);
  412. if (null != pc) {
  413. return pc.getInputStream();
  414. }
  415. }
  416. }
  417. return null;
  418. }
  419.  
  420. // -------------------------------------------------------------------------
  421.  
  422. public String getHTML() {
  423. StringBuffer html = new StringBuffer();
  424. html.append(getBannerHTML());
  425. html.append(getBodyHTML());
  426. html.append(getFooterHTML());
  427. return html.toString();
  428. }
  429.  
  430. // -------------------------------------------------------------------------
  431.  
  432. public String getBannerHTML() {
  433. return this.bannerHTML;
  434. }
  435.  
  436. public void setBannerHTML(final String bannerHTML) {
  437. this.bannerHTML = bannerHTML;
  438. }
  439.  
  440. // -------------------------------------------------------------------------
  441.  
  442. public String getBodyHTML() {
  443. if (null != document && null != fieldName) {
  444. if (this.isDebugMode()) {
  445. System.out.println("Started getBodyHTML()");
  446. }
  447. final DominoRichTextItem drti = document.getRichTextItem(fieldName);
  448. if (null != drti) {
  449. try {
  450. String html = drti.getHTML();
  451. if (null != html) {
  452. final List<FileRowData> fileRowDataList = document.getEmbeddedImagesList(fieldName);
  453. if (null != fileRowDataList) {
  454. final Matcher matcher = imgRegExp.matcher(html);
  455. while (matcher.find()) {
  456. String src = matcher.group();
  457. final String srcToken = "src="";
  458. final int x = src.indexOf(srcToken);
  459. final int y = src.indexOf(""", x + srcToken.length());
  460. final String srcText = src.substring(x + srcToken.length(), y);
  461. for (FileRowData fileRowData : fileRowDataList) {
  462. final String srcImage = fileRowData.getHref();
  463. final String cidImage = ((AttachmentValueHolder) fileRowData).getCID();
  464. if (srcText.endsWith(srcImage)) {
  465. final String newSrc = src.replace(srcText, "cid:" + cidImage);
  466. html = html.replace(src, newSrc);
  467. if (this.isDebugMode()) {
  468. System.out.println("CID referenced image: " + srcText + " with CID:"
  469. + cidImage);
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. if (this.isDebugMode()) {
  477. System.out.println("Completed getBodyHTML() : " + html);
  478. }
  479. return html;
  480. } catch (Exception e) {
  481. if (this.isDebugMode()) {
  482. System.out.println("Failed getBodyHTML() : " + e.getMessage());
  483. }
  484. }
  485. }
  486. }
  487. return null;
  488. }
  489.  
  490. public void setBodyHTML(final String bodyHTML) throws NotImplementedException {
  491. if (this.isDebugMode()) {
  492. System.out.println("Method setBodyHTML(string) is not permitted");
  493. }
  494. throw new NotImplementedException();
  495. }
  496.  
  497. // -------------------------------------------------------------------------
  498.  
  499. public String getFooterHTML() {
  500. return this.footerHTML;
  501. }
  502.  
  503. public void setFooterHTML(final String footerHTML) {
  504. this.footerHTML = footerHTML;
  505. }
  506.  
  507. public String getUnid() {
  508. return unid;
  509. }
  510.  
  511. public void setUnid(final String unid) {
  512. this.unid = unid;
  513. }
  514.  
  515. // -------------------------------------------------------------------------
  516.  
  517. } // end EmailBean
  518.  
  519. Email.setUnid(emlDoc.getUniversalID());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement