List of usage examples for javax.mail.internet MimeMessage setText
@Override public void setText(String text, String charset) throws MessagingException
From source file:com.aurel.track.util.emailHandling.MailBuilder.java
/** * Prepares a plain MimeMessage: the MimeMessage.RecipientType.TO is not yet set * @return/*ww w . j a v a2 s.c o m*/ * @throws Exception */ private MimeMessage preparePlainMimeMessage(InternetAddress internetAddressFrom, String subject, String plainBody, List<LabelValueBean> attachments) throws Exception { MimeMessage msg = new MimeMessage(session); msg.setFrom(internetAddressFrom); msg.setHeader(XMAILER, xmailer); msg.setSubject(subject.trim(), mailEncoding); msg.setSentDate(new Date()); if (attachments == null || attachments.isEmpty()) { msg.setText(plainBody, mailEncoding); } else { MimeMultipart mimeMultipart = new MimeMultipart(); MimeBodyPart textBodyPart = new MimeBodyPart(); textBodyPart.setText(plainBody, mailEncoding); mimeMultipart.addBodyPart(textBodyPart); if (attachments != null) { includeAttachments(mimeMultipart, attachments); } msg.setContent(mimeMultipart); } return msg; }
From source file:com.predic8.membrane.core.interceptor.authentication.session.EmailTokenProvider.java
private void sendEmail(String sender, String recipient, String subject, String text) { try {// ww w . j a v a2 s . co m Properties props = System.getProperties(); props.put("mail.smtp.port", "" + smtpPort); props.put("mail.smtp.socketFactory.port", "" + smtpPort); if (ssl) { props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); } if (smtpUser != null) { props.put("mail.smtp.auth", "true"); } Session session = Session.getInstance(props, null); final MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(sender)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); msg.setSubject(subject); msg.setText(text, Constants.UTF_8); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp"); t.connect(smtpHost, smtpUser, smtpPassword); t.sendMessage(msg, msg.getAllRecipients()); t.close(); } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:ru.org.linux.exception.ExceptionResolver.java
/** * ? E-mail ?.//from www .j a va2 s .c o m * * @param request ? web- * @param exception ? * @return , ? ??? ? ? */ private String sendEmailToAdmin(HttpServletRequest request, Exception exception) { InternetAddress mail; String adminEmailAddress = configuration.getAdminEmailAddress(); try { mail = new InternetAddress(adminEmailAddress, true); } catch (AddressException e) { return EMAIL_NOT_SENT + " ? e-mail ?: " + adminEmailAddress; } StringBuilder text = new StringBuilder(); if (exception.getMessage() == null) { text.append(exception.getClass().getName()); } else { text.append(exception.getMessage()); } text.append("\n\n"); Template tmpl = Template.getTemplate(request); // text.append("Main URL: ").append(tmpl.getMainUrl()).append(request.getAttribute("javax.servlet.error.request_uri")); String mainUrl = "<unknown>"; mainUrl = configuration.getMainUrl(); text.append("Main URL: ").append(mainUrl).append(request.getServletPath()); if (request.getQueryString() != null) { text.append('?').append(request.getQueryString()).append('\n'); } text.append('\n'); text.append("IP: " + request.getRemoteAddr() + '\n'); text.append(" Headers: "); Enumeration enu = request.getHeaderNames(); while (enu.hasMoreElements()) { String paramName = (String) enu.nextElement(); text.append("\n ").append(paramName).append(": ").append(request.getHeader(paramName)); } text.append("\n\n"); StringWriter exceptionStackTrace = new StringWriter(); exception.printStackTrace(new PrintWriter(exceptionStackTrace)); text.append(exceptionStackTrace.toString()); Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); Session mailSession = Session.getDefaultInstance(props, null); MimeMessage emailMessage = new MimeMessage(mailSession); try { emailMessage.setFrom(new InternetAddress("no-reply@linux.org.ru")); emailMessage.addRecipient(Message.RecipientType.TO, mail); emailMessage.setSubject("Linux.org.ru: " + exception.getClass()); emailMessage.setSentDate(new Date()); emailMessage.setText(text.toString(), "UTF-8"); } catch (Exception e) { logger.error("An error occured while creating e-mail!", e); return EMAIL_NOT_SENT; } try { Transport.send(emailMessage); return EMAIL_SENT; } catch (Exception e) { return EMAIL_NOT_SENT; } }
From source file:jease.cms.service.Mails.java
/** * Sends an email synchronously./*www. j a v a 2 s. c o m*/ */ public void send(String sender, String recipients, String subject, String text) throws MessagingException { if (properties != null) { Session session = Session.getInstance(properties.asProperties(), new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(properties.getUser(), properties.getPassword()); } }); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sender)); message.setReplyTo(new InternetAddress[] { new InternetAddress(sender) }); message.setRecipients(Message.RecipientType.TO, recipients); message.setSubject(subject, "utf-8"); message.setSentDate(new Date()); message.setHeader("Content-Type", "text/plain; charset=\"utf-8\""); message.setHeader("Content-Transfer-Encoding", "quoted-printable"); message.setText(text, "utf-8"); Transport.send(message); } }
From source file:eu.optimis.sm.gui.server.ServiceManagerWebServiceImpl.java
public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException { final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtps.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.setProperty("mail.smtps.auth", "true"); props.put("mail.smtps.quitwait", "false"); Session session = Session.getInstance(props, null); final MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(username + "@gmail.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false)); if (ccEmail.length() > 0) { msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false)); }//www .j av a 2 s . c o m msg.setSubject(title); msg.setText(message, "utf-8"); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); t.connect("smtp.gmail.com", username, password); t.sendMessage(msg, msg.getAllRecipients()); t.close(); }
From source file:de.betterform.connector.smtp.SMTPSubmissionHandler.java
private void send(String uri, byte[] data, String encoding, String mediatype) throws Exception { URL url = new URL(uri); String recipient = url.getPath(); String server = null;// ww w . j a v a 2s.co m String port = null; String sender = null; String subject = null; String username = null; String password = null; StringTokenizer headers = new StringTokenizer(url.getQuery(), "&"); while (headers.hasMoreTokens()) { String token = headers.nextToken(); if (token.startsWith("server=")) { server = URLDecoder.decode(token.substring("server=".length()), "UTF-8"); continue; } if (token.startsWith("port=")) { port = URLDecoder.decode(token.substring("port=".length()), "UTF-8"); continue; } if (token.startsWith("sender=")) { sender = URLDecoder.decode(token.substring("sender=".length()), "UTF-8"); continue; } if (token.startsWith("subject=")) { subject = URLDecoder.decode(token.substring("subject=".length()), "UTF-8"); continue; } if (token.startsWith("username=")) { username = URLDecoder.decode(token.substring("username=".length()), "UTF-8"); continue; } if (token.startsWith("password=")) { password = URLDecoder.decode(token.substring("password=".length()), "UTF-8"); continue; } } if (LOGGER.isDebugEnabled()) { LOGGER.debug("smtp server '" + server + "'"); if (username != null) { LOGGER.debug("smtp-auth username '" + username + "'"); } LOGGER.debug("mail sender '" + sender + "'"); LOGGER.debug("subject line '" + subject + "'"); } Properties properties = System.getProperties(); properties.put("mail.debug", String.valueOf(LOGGER.isDebugEnabled())); properties.put("mail.smtp.from", sender); properties.put("mail.smtp.host", server); if (port != null) { properties.put("mail.smtp.port", port); } if (username != null) { properties.put("mail.smtp.auth", String.valueOf(true)); properties.put("mail.smtp.user", username); } Session session = Session.getInstance(properties, new SMTPAuthenticator(username, password)); MimeMessage message = null; if (mediatype.startsWith("multipart/")) { message = new MimeMessage(session, new ByteArrayInputStream(data)); } else { message = new MimeMessage(session); if (mediatype.toLowerCase().indexOf("charset=") == -1) { mediatype += "; charset=\"" + encoding + "\""; } message.setText(new String(data, encoding), encoding); message.setHeader("Content-Type", mediatype); } message.setRecipient(RecipientType.TO, new InternetAddress(recipient)); message.setSubject(subject); message.setSentDate(new Date()); Transport.send(message); }
From source file:org.latticesoft.util.resource.MessageUtil.java
/** * Sends the email.//from w ww . j ava2 s . c o m * @param info the EmailInfo containing the message and other details * @param p the properties to set in the environment when instantiating the session * @param auth the authenticator */ public static void sendMail(EmailInfo info, Properties p, Authenticator auth) { try { if (p == null) { if (log.isErrorEnabled()) { log.error("Null properties!"); } return; } Session session = Session.getInstance(p, auth); session.setDebug(true); if (log.isInfoEnabled()) { log.info(p); log.info(session); } MimeMessage mimeMessage = new MimeMessage(session); if (log.isInfoEnabled()) { log.info(mimeMessage); log.info(info.getFromAddress()); } mimeMessage.setFrom(info.getFromAddress()); mimeMessage.setSentDate(new Date()); List l = info.getToList(); if (l != null) { for (int i = 0; i < l.size(); i++) { String addr = (String) l.get(i); if (log.isInfoEnabled()) { log.info(addr); } mimeMessage.addRecipients(Message.RecipientType.TO, addr); } } l = info.getCcList(); if (l != null) { for (int i = 0; i < l.size(); i++) { String addr = (String) l.get(i); mimeMessage.addRecipients(Message.RecipientType.CC, addr); } } l = info.getBccList(); if (l != null) { for (int i = 0; i < l.size(); i++) { String addr = (String) l.get(i); mimeMessage.addRecipients(Message.RecipientType.BCC, addr); } } if (info.getAttachment().size() == 0) { if (info.getCharSet() != null) { mimeMessage.setSubject(info.getSubject(), info.getCharSet()); mimeMessage.setText(info.getContent(), info.getCharSet()); } else { mimeMessage.setSubject(info.getSubject()); mimeMessage.setText(info.getContent()); } mimeMessage.setContent(info.getContent(), info.getContentType()); } else { if (info.getCharSet() != null) { mimeMessage.setSubject(info.getSubject(), info.getCharSet()); } else { mimeMessage.setSubject(info.getSubject()); } Multipart mp = new MimeMultipart(); MimeBodyPart body = new MimeBodyPart(); if (info.getCharSet() != null) { body.setText(info.getContent(), info.getCharSet()); body.setContent(info.getContent(), info.getContentType()); } else { body.setText(info.getContent()); body.setContent(info.getContent(), info.getContentType()); } mp.addBodyPart(body); for (int i = 0; i < info.getAttachment().size(); i++) { String filename = (String) info.getAttachment().get(i); MimeBodyPart attachment = new MimeBodyPart(); FileDataSource fds = new FileDataSource(filename); attachment.setDataHandler(new DataHandler(fds)); attachment.setFileName(MimeUtility.encodeWord(fds.getName())); mp.addBodyPart(attachment); } mimeMessage.setContent(mp); } Transport.send(mimeMessage); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error in sending email", e); } } }
From source file:hudson.tasks.MailSender.java
private MimeMessage createBackToNormalMail(AbstractBuild<?, ?> build, String subject, BuildListener listener) throws MessagingException, UnsupportedEncodingException { MimeMessage msg = createEmptyMail(build, listener); msg.setSubject(getSubject(build, Messages.MailSender_BackToNormalMail_Subject(subject)), charset); StringBuilder buf = new StringBuilder(); appendBuildUrl(build, buf);//from w w w . ja va2s.co m msg.setText(buf.toString(), charset); return msg; }
From source file:tsuboneSystem.original.manager.MailManager.java
/** * ?/* ww w. ja va 2s. c o m*/ * @return */ public boolean sendMail() { //???TRUE if (!check()) { return false; } Properties objPrp = new Properties(); objPrp.setProperty("mail.smtp.host", "smtp.gmail.com"); objPrp.setProperty("mail.smtp.port", "465"); objPrp.setProperty("mail.smtp.auth", "true"); // objPrp.setProperty("mail.smtp.connectiontimeout", "5000"); objPrp.setProperty("mail.smtp.timeout", "5000"); //???????????JavaMail?Message-ID????? objPrp.setProperty("mail.user", "kagucho.net@gmail.com"); objPrp.setProperty("mail.host", "smtp.gmail.com"); //???????? objPrp.setProperty("mail.debug", "true"); // SSL objPrp.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); objPrp.setProperty("mail.smtp.socketFactory.fallback", "false"); objPrp.setProperty("mail.smtp.socketFactory.port", "465"); String address = ConfigUtil.getConfig("mail.address"); String pw = ConfigUtil.getConfig("mail.pw"); // Session session = Session.getInstance(objPrp, new PlainAuthenticator(address, pw)); // ?? MimeMessage objMsg = new MimeMessage(session); try { // ? objMsg.setFrom(new InternetAddress(address, displayName)); // ?? objMsg.setSubject(title, encoding); // ?TO????CCBCC? objMsg.setRecipients(Message.RecipientType.BCC, getToAddress()); // objMsg.setText(getContent(), encoding); // ? SMTPTransport t = (SMTPTransport) session.getTransport("smtp"); try { t.connect("smtp.gmail.com", address, pw); t.sendMessage(objMsg, objMsg.getAllRecipients()); } finally { t.close(); } return false; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return true; } catch (MessagingException e) { e.printStackTrace(); return true; } }
From source file:org.chiba.connectors.smtp.SMTPSubmissionHandler.java
private void send(String uri, byte[] data, String encoding, String mediatype) throws Exception { URL url = new URL(uri); String recipient = url.getPath(); String server = null;//from w w w.jav a 2s . com String port = null; String sender = null; String subject = null; String username = null; String password = null; StringTokenizer headers = new StringTokenizer(url.getQuery(), "&"); while (headers.hasMoreTokens()) { String token = headers.nextToken(); if (token.startsWith("server=")) { server = URLDecoder.decode(token.substring("server=".length())); continue; } if (token.startsWith("port=")) { port = URLDecoder.decode(token.substring("port=".length())); continue; } if (token.startsWith("sender=")) { sender = URLDecoder.decode(token.substring("sender=".length())); continue; } if (token.startsWith("subject=")) { subject = URLDecoder.decode(token.substring("subject=".length())); continue; } if (token.startsWith("username=")) { username = URLDecoder.decode(token.substring("username=".length())); continue; } if (token.startsWith("password=")) { password = URLDecoder.decode(token.substring("password=".length())); continue; } } if (LOGGER.isDebugEnabled()) { LOGGER.debug("smtp server '" + server + "'"); if (username != null) { LOGGER.debug("smtp-auth username '" + username + "'"); } LOGGER.debug("mail sender '" + sender + "'"); LOGGER.debug("subject line '" + subject + "'"); } Properties properties = System.getProperties(); properties.put("mail.debug", String.valueOf(LOGGER.isDebugEnabled())); properties.put("mail.smtp.from", sender); properties.put("mail.smtp.host", server); if (port != null) { properties.put("mail.smtp.port", port); } if (username != null) { properties.put("mail.smtp.auth", String.valueOf(true)); properties.put("mail.smtp.user", username); } Session session = Session.getInstance(properties, new SMTPAuthenticator(username, password)); MimeMessage message = null; if (mediatype.startsWith("multipart/")) { message = new MimeMessage(session, new ByteArrayInputStream(data)); } else { message = new MimeMessage(session); if (mediatype.toLowerCase().indexOf("charset=") == -1) { mediatype += "; charset=\"" + encoding + "\""; } message.setText(new String(data, encoding), encoding); message.setHeader("Content-Type", mediatype); } message.setRecipient(RecipientType.TO, new InternetAddress(recipient)); message.setSubject(subject); message.setSentDate(new Date()); Transport.send(message); }