List of usage examples for javax.mail.internet AddressException printStackTrace
public void printStackTrace()
From source file:de.tu_dortmund.ub.api.paaa.model.Patron.java
public void setEmail(String email) { try {//from w w w .ja v a 2 s . c om this.email = new InternetAddress(email); } catch (AddressException e) { e.printStackTrace(); } }
From source file:org.fao.geonet.services.register.SelfRegister.java
/** * Send an email.//from ww w . j a va2s.c o m * * @param host * @param port * @param subject * @param from * @param to * @param content * @return */ boolean sendMail(String host, int port, String subject, String from, String to, String content) { boolean isSendout = false; Properties props = new Properties(); props.put("mail.transport.protocol", PROTOCOL); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.auth", "false"); Session mailSession = Session.getDefaultInstance(props); try { Message msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSentDate(new Date()); msg.setSubject(subject); // Add content message msg.setText(content); Transport.send(msg); isSendout = true; } catch (AddressException e) { isSendout = false; e.printStackTrace(); } catch (MessagingException e) { isSendout = false; e.printStackTrace(); } return isSendout; }
From source file:com.sunrun.crportal.util.CRPortalUtil.java
/** * sendEmail - Sends an email//from w w w .j a va 2s.c o m * @param msgSubject * @param msgBody * @param emailAddress * @throws PortletException */ public static void sendEmail(String msgSubject, String msgBody, String emailAddress) throws PortletException { //MDSW-2443 LOG.debug("Entering sendEmail()"); InternetAddress from; InternetAddress to; try { from = new InternetAddress("no-reply@sunrunhome.com"); //MDSW-2443 to = new InternetAddress(emailAddress); //MDSW-2443 MailMessage message = new MailMessage(from, to, msgSubject, msgBody, true); //MDSW-2443 MailServiceUtil.sendEmail(message); //MDSW-2443 } catch (AddressException e) { //MDSW-2443 LOG.error("Invalid email address"); e.printStackTrace(); } LOG.debug("Leaving sendEmail()"); }
From source file:fsi_admin.JSmtpConn.java
private boolean prepareMsg(String FROM, String TO, String SUBJECT, String MIMETYPE, String BODY, StringBuffer msj, Properties props, Session session, MimeMessage mmsg, BodyPart messagebodypart, MimeMultipart multipart) {//from w w w. j a v a 2 s .co m // Create a message with the specified information. try { mmsg.setFrom(new InternetAddress(FROM)); mmsg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); mmsg.setSubject(SUBJECT); messagebodypart.setContent(BODY, MIMETYPE); multipart.addBodyPart(messagebodypart); return true; } catch (AddressException e) { e.printStackTrace(); msj.append("Error de Direcciones al preparar SMTP: " + e.getMessage()); return false; } catch (MessagingException e) { e.printStackTrace(); msj.append("Error de Mensajeria al preparar SMTP: " + e.getMessage()); return false; } }
From source file:org.alfresco.repo.content.transform.EMLParser.java
/** * Extracts properties and text from an EML Document input stream. * * @param stream/*from w w w. j a v a2s .c o m*/ * the stream * @param handler * the handler * @param metadata * the metadata * @param context * the context * @throws IOException * Signals that an I/O exception has occurred. * @throws SAXException * the sAX exception * @throws TikaException * the tika exception */ @Override public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); Properties props = System.getProperties(); Session mailSession = Session.getDefaultInstance(props, null); try { MimeMessage message = new MimeMessage(mailSession, stream); String subject = message.getSubject(); String from = this.convertAddressesToString(message.getFrom()); // Recipients : String messageException = ""; String to = ""; String cc = ""; String bcc = ""; try { // QVIDMS-2004 Added because of bug in Mail Api to = this.convertAddressesToString(message.getRecipients(Message.RecipientType.TO)); cc = this.convertAddressesToString(message.getRecipients(Message.RecipientType.CC)); bcc = this.convertAddressesToString(message.getRecipients(Message.RecipientType.BCC)); } catch (AddressException e) { e.printStackTrace(); messageException = e.getRef(); if (messageException.indexOf("recipients:") != -1) { to = messageException.substring(0, messageException.indexOf(":")); } } metadata.set(Office.AUTHOR, from); metadata.set(DublinCore.TITLE, subject); metadata.set(DublinCore.SUBJECT, subject); xhtml.element("h1", subject); xhtml.startElement("dl"); header(xhtml, "From", MimeUtility.decodeText(from)); header(xhtml, "To", MimeUtility.decodeText(to.toString())); header(xhtml, "Cc", MimeUtility.decodeText(cc.toString())); header(xhtml, "Bcc", MimeUtility.decodeText(bcc.toString())); // // Parse message // if (message.getContent() instanceof MimeMultipart) { // // Multipart message, call matching method // MimeMultipart multipart = (MimeMultipart) message.getContent(); // this.extractMultipart(xhtml, multipart, context); List<String> attachmentList = new ArrayList<String>(); // prepare attachments prepareExtractMultipart(xhtml, message, null, context, attachmentList); if (attachmentList.size() > 0) { // TODO internationalization header(xhtml, "Attachments", attachmentList.toString()); } xhtml.endElement("dl"); // a supprimer si pb et a remplacer par ce qui est commenT adaptedExtractMultipart(xhtml, message, null, context); xhtml.endDocument(); } catch (Exception e) { throw new TikaException("Error while processing message", e); } }
From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ContactMailController.java
private ResponseValues processValidRequest(VitroRequest vreq, String webusername, String webuseremail, String[] recipients, String comments) throws Error { String statusMsg = null; // holds the error status ApplicationBean appBean = vreq.getAppBean(); String deliveryfrom = "Message from the " + appBean.getApplicationName() + " Contact Form"; String originalReferer = getOriginalRefererFromSession(vreq); String msgText = composeEmail(webusername, webuseremail, comments, deliveryfrom, originalReferer, vreq.getRemoteAddr(), vreq); try {/*from w ww .j a v a2 s. c om*/ // Write the message to the journal file FileWriter fw = new FileWriter(locateTheJournalFile(), true); PrintWriter outFile = new PrintWriter(fw); writeBackupCopy(outFile, msgText, vreq); try { // Send the message Session s = FreemarkerEmailFactory.getEmailSession(vreq); sendMessage(s, webuseremail, webusername, recipients, deliveryfrom, msgText); } catch (AddressException e) { statusMsg = "Please supply a valid email address."; outFile.println(statusMsg); outFile.println(e.getMessage()); } catch (SendFailedException e) { statusMsg = "The system was unable to deliver your mail. Please try again later. [SEND FAILED]"; outFile.println(statusMsg); outFile.println(e.getMessage()); } catch (MessagingException e) { statusMsg = "The system was unable to deliver your mail. Please try again later. [MESSAGING]"; outFile.println(statusMsg); outFile.println(e.getMessage()); e.printStackTrace(); } outFile.close(); } catch (IOException e) { log.error("Can't open file to write email backup"); } if (statusMsg == null) { // Message was sent successfully return new TemplateResponseValues(TEMPLATE_CONFIRMATION); } else { Map<String, Object> body = new HashMap<String, Object>(); body.put("errorMessage", statusMsg); return new TemplateResponseValues(TEMPLATE_ERROR, body); } }
From source file:org.protocoderrunner.apprunner.api.PNetwork.java
@ProtocoderScript @APIMethod(description = "Send an E-mail. It requires passing a EmailConf object", example = "") @APIParam(params = { "url", "function(data)" }) public void sendEmail(String from, String to, String subject, String text, final EmailConf emailSettings) throws AddressException, MessagingException { if (emailSettings == null) { return;//from w ww . j a v a 2s. c o m } // final String host = "smtp.gmail.com"; // final String address = "@gmail.com"; // final String pass = ""; Multipart multiPart; String finalString = ""; Properties props = System.getProperties(); props.put("mail.smtp.starttls.enable", emailSettings.ttl); props.put("mail.smtp.host", emailSettings.host); props.put("mail.smtp.user", emailSettings.user); props.put("mail.smtp.password", emailSettings.password); props.put("mail.smtp.port", emailSettings.port); props.put("mail.smtp.auth", emailSettings.auth); Log.i("Check", "done pops"); final Session session = Session.getDefaultInstance(props, null); DataHandler handler = new DataHandler(new ByteArrayDataSource(finalString.getBytes(), "text/plain")); final MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setDataHandler(handler); Log.i("Check", "done sessions"); multiPart = new MimeMultipart(); InternetAddress toAddress; toAddress = new InternetAddress(to); message.addRecipient(Message.RecipientType.TO, toAddress); Log.i("Check", "added recipient"); message.setSubject(subject); message.setContent(multiPart); message.setText(text); Thread t = new Thread(new Runnable() { @Override public void run() { try { MLog.i("check", "transport"); Transport transport = session.getTransport("smtp"); MLog.i("check", "connecting"); transport.connect(emailSettings.host, emailSettings.user, emailSettings.password); MLog.i("check", "wana send"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); MLog.i("check", "sent"); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } }); t.start(); }
From source file:edu.ku.brc.helpers.EMailHelper.java
/** * Send an email. Also sends it as a gmail if applicable, and does password checking. * @param host host of SMTP server// w ww . ja v a 2 s . co m * @param uName username of email account * @param pWord password of email account * @param fromEMailAddr the email address of who the email is coming from typically this is the same as the user's email * @param toEMailAddr the email addr of who this is going to * @param subject the Textual subject line of the email * @param bodyText the body text of the email (plain text???) * @param fileAttachment and optional file to be attached to the email * @return true if the msg was sent, false if not */ public static ErrorType sendMsg(final String host, final String uName, final String pWord, final String fromEMailAddr, final String toEMailAddr, final String subject, final String bodyText, final String mimeType, final String port, final String security, final File fileAttachment) { String userName = uName; String password = pWord; if (StringUtils.isEmpty(toEMailAddr)) { UIRegistry.showLocalizedError("EMailHelper.NO_TO_ERR"); return ErrorType.Error; } if (StringUtils.isEmpty(fromEMailAddr)) { UIRegistry.showLocalizedError("EMailHelper.NO_FROM_ERR"); return ErrorType.Error; } //if (isGmailEmail()) //{ // return sendMsgAsGMail(host, userName, password, fromEMailAddr, toEMailAddr, subject, bodyText, mimeType, port, security, fileAttachment); //} Boolean fail = false; ArrayList<String> userAndPass = new ArrayList<String>(); boolean isSSL = security.equals("SSL"); String[] keys = { "mail.smtp.host", "mail.smtp.port", "mail.smtp.auth", "mail.smtp.starttls.enable", "mail.smtp.socketFactory.port", "mail.smtp.socketFactory.class", "mail.smtp.socketFactory.fallback", "mail.imap.auth.plain.disable", }; Properties props = System.getProperties(); for (String key : keys) { props.remove(key); } props.put("mail.smtp.host", host); //$NON-NLS-1$ if (StringUtils.isNotEmpty(port) && StringUtils.isNumeric(port)) { props.put("mail.smtp.port", port); //$NON-NLS-1$ //$NON-NLS-2$ } else { props.remove("mail.smtp.port"); } if (StringUtils.isNotEmpty(security)) { if (security.equals("TLS")) { props.put("mail.smtp.auth", "true"); //$NON-NLS-1$ //$NON-NLS-2$ props.put("mail.smtp.starttls.enable", "true"); //$NON-NLS-1$ //$NON-NLS-2$ } else if (isSSL) { props.put("mail.smtp.auth", "true"); //$NON-NLS-1$ //$NON-NLS-2$ String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; props.put("mail.smtp.socketFactory.port", port); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.imap.auth.plain.disable", "true"); } } Session session = null; if (isSSL) { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(uName, pWord); } }); } else { session = Session.getInstance(props, null); } session.setDebug(instance.isDebugging); if (instance.isDebugging) { log.debug("Host: " + host); //$NON-NLS-1$ log.debug("UserName: " + userName); //$NON-NLS-1$ log.debug("Password: " + password); //$NON-NLS-1$ log.debug("From: " + fromEMailAddr); //$NON-NLS-1$ log.debug("To: " + toEMailAddr); //$NON-NLS-1$ log.debug("Subject: " + subject); //$NON-NLS-1$ log.debug("Port: " + port); //$NON-NLS-1$ log.debug("Security: " + security); //$NON-NLS-1$ } try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromEMailAddr)); if (toEMailAddr.indexOf(",") > -1) //$NON-NLS-1$ { StringTokenizer st = new StringTokenizer(toEMailAddr, ","); //$NON-NLS-1$ InternetAddress[] address = new InternetAddress[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { String toStr = st.nextToken().trim(); address[i++] = new InternetAddress(toStr); } msg.setRecipients(Message.RecipientType.TO, address); } else { try { InternetAddress[] address = { new InternetAddress(toEMailAddr) }; msg.setRecipients(Message.RecipientType.TO, address); } catch (javax.mail.internet.AddressException ex) { UIRegistry.showLocalizedError("EMailHelper.TO_ADDR_ERR", toEMailAddr); return ErrorType.Error; } } msg.setSubject(subject); //msg.setContent( aBodyText , "text/html;charset=\"iso-8859-1\""); // create the second message part if (fileAttachment != null) { // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setContent(bodyText, mimeType);//"text/html;charset=\"iso-8859-1\""); //mbp1.setContent(bodyText, "text/html;charset=\"iso-8859-1\""); MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(fileAttachment); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); } else { // add the Multipart to the message msg.setContent(bodyText, mimeType); } final int TRIES = 1; // set the Date: header msg.setSentDate(new Date()); Exception exception = null; // send the message int cnt = 0; do { cnt++; SMTPTransport t = isSSL ? null : (SMTPTransport) session.getTransport("smtp"); //$NON-NLS-1$ try { if (isSSL) { Transport.send(msg); } else { t.connect(host, userName, password); t.sendMessage(msg, msg.getAllRecipients()); } fail = false; } catch (SendFailedException mex) { mex.printStackTrace(); exception = mex; } catch (MessagingException mex) { if (mex.getCause() instanceof UnknownHostException) { instance.lastErrorMsg = null; fail = true; UIRegistry.showLocalizedError("EMailHelper.UNK_HOST", host); } else if (mex.getCause() instanceof ConnectException) { instance.lastErrorMsg = null; fail = true; UIRegistry.showLocalizedError( "EMailHelper." + (StringUtils.isEmpty(port) ? "CNCT_ERR1" : "CNCT_ERR2"), port); } else { mex.printStackTrace(); exception = mex; } } catch (Exception mex) { mex.printStackTrace(); exception = mex; } finally { if (t != null) { log.debug("Response: " + t.getLastServerResponse()); //$NON-NLS-1$ t.close(); } } if (exception != null) { fail = true; instance.lastErrorMsg = exception.toString(); //wrong username or password, get new one if (exception.toString().equals("javax.mail.AuthenticationFailedException")) //$NON-NLS-1$ { UIRegistry.showLocalizedError("EMailHelper.UP_ERROR", userName); userAndPass = askForUserAndPassword((Frame) UIRegistry.getTopWindow()); if (userAndPass == null) { //the user is done instance.lastErrorMsg = null; return ErrorType.Cancel; } userName = userAndPass.get(0); password = userAndPass.get(1); } } exception = null; } while (fail && cnt < TRIES); } catch (Exception mex) { //edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); //edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, mex); instance.lastErrorMsg = mex.toString(); mex.printStackTrace(); Exception ex = null; if (mex instanceof MessagingException && (ex = ((MessagingException) mex).getNextException()) != null) { ex.printStackTrace(); instance.lastErrorMsg = instance.lastErrorMsg + ", " + ex.toString(); //$NON-NLS-1$ } return ErrorType.Error; } if (fail) { return ErrorType.Error; } //else return ErrorType.OK; }
From source file:library.Form_Library.java
License:asdf
public void sendFromGMail(String from, String pass, String to, String subject, String body) { this.taBaoCao.append("Sent to " + to + " successfully.\n"); Properties props = System.getProperties(); String host = "smtp.gmail.com"; props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", pass); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); try {/*from w w w . j a va2 s. co m*/ message.setFrom(new InternetAddress(from)); InternetAddress toAddress = new InternetAddress(); // To get the array of addresses // for( int i = 0; i < to.length; i++ ) { toAddress = new InternetAddress(to); // } // for( int i = 0; i < toAddress.length; i++) { message.addRecipient(Message.RecipientType.TO, toAddress); // } message.setSubject(subject); message.setText(body); Transport transport = session.getTransport("smtp"); transport.connect(host, from, pass); transport.sendMessage(message, message.getAllRecipients()); System.out.print("Successfully Sent" + "\n"); transport.close(); } catch (AddressException ae) { ae.printStackTrace(); } catch (MessagingException me) { me.printStackTrace(); } }