List of usage examples for javax.mail Message setFrom
public abstract void setFrom(Address address) throws MessagingException;
From source file:org.kuali.test.utils.Utils.java
/** * //from w w w. j a v a 2s .c o m * @param configuration * @param overrideEmail * @param testSuite * @param testHeader * @param testResults * @param errorCount * @param warningCount * @param successCount */ public static void sendMail(KualiTestConfigurationDocument.KualiTestConfiguration configuration, String overrideEmail, TestSuite testSuite, TestHeader testHeader, List<File> testResults, int errorCount, int warningCount, int successCount) { if (StringUtils.isNotBlank(configuration.getEmailSetup().getFromAddress()) && StringUtils.isNotBlank(configuration.getEmailSetup().getMailHost())) { String[] toAddresses = getEmailToAddresses(configuration, testSuite, testHeader); if (toAddresses.length > 0) { Properties props = new Properties(); props.put("mail.smtp.host", configuration.getEmailSetup().getMailHost()); Session session = Session.getDefaultInstance(props, null); try { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(configuration.getEmailSetup().getFromAddress())); if (StringUtils.isBlank(overrideEmail)) { for (String recipient : toAddresses) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); } } else { StringTokenizer st = new StringTokenizer(overrideEmail, ","); while (st.hasMoreTokens()) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(st.nextToken())); } } StringBuilder subject = new StringBuilder(configuration.getEmailSetup().getSubject()); subject.append(" - Platform: "); if (testSuite != null) { subject.append(testSuite.getPlatformName()); subject.append(", TestSuite: "); subject.append(testSuite.getName()); } else { subject.append(testHeader.getPlatformName()); subject.append(", Test: "); subject.append(testHeader.getTestName()); } subject.append(" - [errors="); subject.append(errorCount); subject.append(", warnings="); subject.append(warningCount); subject.append(", successes="); subject.append(successCount); subject.append("]"); msg.setSubject(subject.toString()); StringBuilder msgtxt = new StringBuilder(256); msgtxt.append("Please see test output in the following attached files:\n"); for (File f : testResults) { msgtxt.append(f.getName()); msgtxt.append("\n"); } // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgtxt.toString()); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); for (File f : testResults) { if (f.exists() && f.isFile()) { // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message mbp2.setDataHandler(new DataHandler(new FileDataSource(f))); mbp2.setFileName(f.getName()); mp.addBodyPart(mbp2); } } // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); Transport.send(msg); } catch (MessagingException ex) { LOG.warn(ex.toString(), ex); } } } }
From source file:com.quinsoft.zeidon.zeidonoperations.ZDRVROPR.java
public int CreateSeeMessage(int lConnection, String szSMTPServer, String szUserEmailAddress, String szRecipientEmailAddress, String szCCAddress, String szBCCAddress, String szSubjectText, int MimeType, String szMessageBody, String string4, String string5, int attachmentFlag, String szAttachmentFileName, String szUserEmailName, String szUserEmailPassword) { InternetAddress fromAddress = null;// w w w .j a v a2 s .com String host = szSMTPServer; //enc-exhub.enc-ad.enc.edu String from = szUserEmailAddress; String to[] = szRecipientEmailAddress.split("[\\s,;]+"); InternetAddress[] toAddress = new InternetAddress[to.length]; String cc[] = szCCAddress.split("[\\s,;]+"); InternetAddress[] ccAddress = new InternetAddress[cc.length]; String bcc[] = szBCCAddress.split("[\\s,;]+"); InternetAddress[] bccAddress = new InternetAddress[bcc.length]; //String host = "enc-exhub.enc-ad.enc.edu"; //String from = "kellysautter@comcast.net"; //String to = "kellysautter@comcast.net"; // Set properties Properties props = new Properties(); //mail.smtp.sendpartial props.put("mail.smtp.host", host); props.put("mail.debug", "true"); // Get session // Going to use getDefaultInstance // If I get java.lang.SecurityException: Access to default session denied // then it said to go use .getInstance(props). Session session = Session.getDefaultInstance(props); try { // Instantiate a message Message msg = new MimeMessage(session); try { if (from != null && !from.isEmpty()) fromAddress = new InternetAddress(from); if (szRecipientEmailAddress != null && !szRecipientEmailAddress.isEmpty()) { for (int iCnt = 0; iCnt < to.length; iCnt++) { toAddress[iCnt] = new InternetAddress(to[iCnt]); } } if (szCCAddress != null && !szCCAddress.isEmpty()) { for (int iCnt = 0; iCnt < cc.length; iCnt++) { ccAddress[iCnt] = new InternetAddress(cc[iCnt]); } } if (szBCCAddress != null && !szBCCAddress.isEmpty()) { for (int iCnt = 0; iCnt < bcc.length; iCnt++) { bccAddress[iCnt] = new InternetAddress(bcc[iCnt]); } } } catch (AddressException e) { task.log().error("*** CreateSeeMessage: setting addresses **** "); task.log().error(e); return -1; } // Set the FROM message msg.setFrom(fromAddress); if (szRecipientEmailAddress != null && !szRecipientEmailAddress.isEmpty()) msg.setRecipients(Message.RecipientType.TO, toAddress); if (szCCAddress != null && !szCCAddress.isEmpty()) msg.setRecipients(Message.RecipientType.CC, ccAddress); if (szBCCAddress != null && !szBCCAddress.isEmpty()) msg.setRecipients(Message.RecipientType.BCC, bccAddress); // Set the message subject and date we sent it. msg.setSubject(szSubjectText); msg.setSentDate(new Date()); // Set message content msg.setText(szMessageBody); // Send the message Transport.send(msg); } catch (MessagingException mex) { task.log().error("*** CreateSeeMessage: Transport.send error **** "); task.log().error(mex); // Email was bad? if (mex instanceof SendFailedException) return -1; // SMTP connection bad? return -2; } return 0; }