List of usage examples for javax.mail Message setFrom
public abstract void setFrom(Address address) throws MessagingException;
From source file:com.appeligo.search.messenger.Messenger.java
/** * /* w w w . j a v a 2s . co m*/ * @param messages */ public int send(com.appeligo.search.entity.Message... messages) { int sent = 0; if (messages == null) { return 0; } for (com.appeligo.search.entity.Message message : messages) { User user = message.getUser(); if (user != null) { boolean changed = false; boolean abort = false; if ((!user.isEnabled()) || (!user.isRegistrationComplete()) || (message.isSms() && (!user.isSmsValid()))) { abort = true; if (message.getExpires() == null) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, 24); message.setExpires(new Timestamp(cal.getTimeInMillis())); changed = true; } } if (message.isSms() && user.isSmsValid() && (!user.isSmsOKNow())) { Calendar now = Calendar.getInstance(user.getTimeZone()); now.set(Calendar.MILLISECOND, 0); now.set(Calendar.SECOND, 0); Calendar nextWindow = Calendar.getInstance(user.getTimeZone()); nextWindow.setTime(user.getEarliestSmsTime()); nextWindow.set(Calendar.MILLISECOND, 0); nextWindow.set(Calendar.SECOND, 0); nextWindow.add(Calendar.MINUTE, 1); // compensate for zeroing out millis, seconds nextWindow.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE)); int nowMinutes = (now.get(Calendar.HOUR) * 60) + now.get(Calendar.MINUTE); int nextMinutes = (nextWindow.get(Calendar.HOUR) * 60) + nextWindow.get(Calendar.MINUTE); if (nowMinutes > nextMinutes) { nextWindow.add(Calendar.HOUR, 24); } message.setDeferUntil(new Timestamp(nextWindow.getTimeInMillis())); changed = true; abort = true; } if (changed) { message.save(); } if (abort) { continue; } } String to = message.getTo(); String from = message.getFrom(); String subject = message.getSubject(); String body = message.getBody(); String contentType = message.getMimeType(); try { Properties props = new Properties(); //Specify the desired SMTP server props.put("mail.smtp.host", mailHost); props.put("mail.smtp.port", Integer.toString(port)); // create a new Session object Session session = null; if (password != null) { props.put("mail.smtp.auth", "true"); session = Session.getInstance(props, new SMTPAuthenticator(smtpUser, password)); } else { session = Session.getInstance(props, null); } session.setDebug(debug); // create a new MimeMessage object (using the Session created above) Message mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) }); mimeMessage.setSubject(subject); mimeMessage.setContent(body.toString(), contentType); if (mailHost.trim().equals("")) { log.info("No Mail Host. Would have sent:"); log.info("From: " + from); log.info("To: " + to); log.info("Subject: " + subject); log.info(mimeMessage.getContent()); } else { Transport.send(mimeMessage); sent++; } message.setSent(new Date()); } catch (Throwable t) { message.failedAttempt(); if (message.getAttempts() >= maxAttempts) { message.setExpires(new Timestamp(System.currentTimeMillis())); } log.error(t.getMessage(), t); } } return sent; }
From source file:com.bia.gmailjava.EmailService.java
/** * * @param addressTo/*from ww w. ja v a 2 s. c o m*/ * @param subject * @param message * @throws AddressException * @throws MessagingException */ private void send(InternetAddress[] addressTo, String subject, String message) throws AddressException, MessagingException { Message msg = new MimeMessage(createSession()); InternetAddress addressFrom = new InternetAddress(USERNAME); msg.setFrom(addressFrom); msg.setRecipients(Message.RecipientType.TO, addressTo); // set bcc //InternetAddress[] bcc1 = getBCC(); //msg.setRecipients(Message.RecipientType.BCC, bcc1); // Setting the Subject and Content Type msg.setSubject(subject); //String message = comment; msg.setContent(message, EMAIL_CONTENT_TYPE); Transport.send(msg); }
From source file:com.esri.gpt.framework.mail.MailRequest.java
/** * Sends the E-Mail message./*w w w . j a va 2 s .c o m*/ * @throws AddressException if an E-Mail address is invalid * @throws MessagingException if an exception occurs */ public void send() throws AddressException, MessagingException { // setup the mail server properties Properties props = new Properties(); props.put("mail.smtp.host", getHost()); if (getPort() > 0) { props.put("mail.smtp.port", "" + getPort()); } // set up the message Session session = Session.getDefaultInstance(props, _authenticator); Message message = new MimeMessage(session); message.setSubject(getSubject()); message.setContent(getBody(), getMimeType()); message.setFrom(makeAddress(escapeHtml4(stripControls(getFromAddress())))); for (String sTo : getRecipients()) { message.addRecipient(Message.RecipientType.TO, makeAddress(sTo)); } // send the message Transport.send(message); }
From source file:br.com.cgcop.administrativo.modelo.ContaEmail.java
public void enviarEmailHtml(List<String> destinos, String msg, String titulo) { // Recipient's email ID needs to be mentioned. String to = ""; String rodape = "<br/><br/><br/><br/> <div style=\"border-top: 1px dashed #c8cdbe;border-top: 1px dashed #c8cdbe \">" + "Esta mensagem uma notificao enviada automaticamente por tanto no deve ser respondida. <br/> " + "<span style=\"font-style: italic; font-family: Narrow; font-size: large; color: rgb(0, 153, 0);\">Sistema de Gesto de Projetos e Obras - SGPO</span><br/>" + "<span style=\"font-size: large; font-style: italic; color: rgb(0, 153, 0); font-family: Narrow;\">Oiti Engenharia e Gesto Ambiental</span>" + "</div>"; for (String d : destinos) { if (d.equals(destinos.get(destinos.size() - 1))) { to = to.concat(d);// w ww . ja v a 2s . c om } else { to = to.concat(d.concat(",")); } } // Sender's email ID needs to be mentioned String from = this.email; final String username = this.email;//change accordingly final String password = this.senha;//change accordingly Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", this.host); props.put("mail.smtp.port", "25"); // Get the Session object. Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject(titulo); // Send the actual HTML message, as big as you like message.setContent(msg + rodape, "text/html"); // Send message Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); throw new RuntimeException(e); } }
From source file:org.infoglue.common.util.mail.MailService.java
/** * @param attachments /*w ww .j a va 2 s .com*/ * */ private Message createMessage(String from, String to, String bcc, String subject, String content, String contentType, String encoding, List attachments) throws SystemException { try { final Message message = new MimeMessage(this.session); String contentTypeWithEncoding = contentType + ";charset=" + encoding; // message.setContent(content, contentType); message.setFrom(createInternetAddress(from)); //message.setRecipient(Message.RecipientType.TO, // createInternetAddress(to)); message.setRecipients(Message.RecipientType.TO, createInternetAddresses(to)); if (bcc != null) message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc)); // message.setSubject(subject); ((MimeMessage) message).setSubject(subject, encoding); MimeMultipart mp = new MimeMultipart(); MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setDataHandler(new DataHandler(new StringDataSource(content, contentTypeWithEncoding, encoding))); mp.addBodyPart(mbp1); if (attachments != null) { for (Iterator it = attachments.iterator(); it.hasNext();) { File attachmentFile = (File) it.next(); if (attachmentFile.exists()) { MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(attachmentFile.getName()); attachment.setDataHandler(new DataHandler(new FileDataSource(attachmentFile))); mp.addBodyPart(attachment); } } } message.setContent(mp); // message.setText(content); // message.setDataHandler(new DataHandler(new // StringDataSource(content, contentTypeWithEncoding, encoding))); // message.setText(content); // message.setDataHandler(new DataHandler(new // StringDataSource(content, "text/html"))); return message; } catch (MessagingException e) { throw new SystemException("Unable to create the message.", e); } }
From source file:com.waveerp.sendMail.java
public void sendMsgSSL(String strSource, String strSourceDesc, String strSubject, String strMsg, String strDestination, String strDestDesc) throws Exception { // Call the registry management system registrySystem rs = new registrySystem(); // Call the encryption management system desEncryption de = new desEncryption(); de.Encrypter("", ""); String strHost = rs.readRegistry("NA", "NA", "NA", "EMAILHOST"); String strPort = rs.readRegistry("NA", "NA", "NA", "EMAILPORT"); final String strUser = rs.readRegistry("NA", "NA", "NA", "EMAILUSER"); String strPass = rs.readRegistry("NA", "NA", "NA", "EMAILPASSWORD"); //Decrypt the encrypted password. final String strPass01 = de.decrypt(strPass); Properties props = new Properties(); props.put("mail.smtp.host", strHost); props.put("mail.smtp.socketFactory.port", strPort); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", strPort); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPass01); }/*from w w w . j a v a 2 s. c o m*/ }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(strSource)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strDestination)); message.setSubject(strSubject); message.setText(strMsg); Transport.send(message); //System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } //log(DEBUG, strHost + "|" + strPort + "|" + strUser + "|" + strPass ); }
From source file:com.intuit.tank.mail.TankMailer.java
/** * @{inheritDoc/*from w ww. j a v a 2s .c om*/ */ @Override public void sendMail(MailMessage message, String... emailAddresses) { MailConfig mailConfig = new TankConfig().getMailConfig(); Properties props = new Properties(); props.put("mail.smtp.host", mailConfig.getSmtpHost()); props.put("mail.smtp.port", mailConfig.getSmtpPort()); Session mailSession = Session.getDefaultInstance(props); Message simpleMessage = new MimeMessage(mailSession); InternetAddress fromAddress = null; InternetAddress toAddress = null; try { fromAddress = new InternetAddress(mailConfig.getMailFrom()); simpleMessage.setFrom(fromAddress); for (String email : emailAddresses) { try { toAddress = new InternetAddress(email); simpleMessage.addRecipient(RecipientType.TO, toAddress); } catch (AddressException e) { LOG.warn("Error with recipient " + email + ": " + e.toString()); } } simpleMessage.setSubject(message.getSubject()); final MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent(message.getPlainTextBody(), "text/plain"); textPart.setHeader("MIME-Version", "1.0"); textPart.setHeader("Content-Type", textPart.getContentType()); // HTML version final MimeBodyPart htmlPart = new MimeBodyPart(); // htmlPart.setContent(message.getHtmlBody(), "text/html"); htmlPart.setDataHandler(new DataHandler(new HTMLDataSource(message.getHtmlBody()))); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html"); // Create the Multipart. Add BodyParts to it. final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); // Set Multipart as the message's content simpleMessage.setContent(mp); simpleMessage.setHeader("MIME-Version", "1.0"); simpleMessage.setHeader("Content-Type", mp.getContentType()); logMsg(mailConfig.getSmtpHost(), simpleMessage); if (simpleMessage.getRecipients(RecipientType.TO) != null && simpleMessage.getRecipients(RecipientType.TO).length > 0) { Transport.send(simpleMessage); } } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:org.fao.geonet.services.register.SelfRegister.java
/** * Send an email.//from w w w . j a va2 s . c om * * @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:gov.nih.nci.cacisweb.util.CaCISUtil.java
/** * Sends email by setting some of the email properties that are common to Secure Email and XDS/NAV * /* w w w . j av a2s .co m*/ * @param host * @param port * @param protocol * @param senderEmail * @param senderUser * @param senderPassword * @param subject * @param message * @param recepientEmail * @throws EmailException */ public void sendEmail(String host, String port, String senderEmail, String senderUser, String senderPassword, String subject, String body, String recepientEmail) throws MessagingException { log.debug("sendEmail(String host, String port, String protocol, String senderEmail, String senderUser," + "String senderPassword, String subject, String message, String recepientEmail) - start"); final String username = senderUser; final String password = senderPassword; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(senderEmail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recepientEmail)); message.setSubject(subject); message.setText(body); Transport.send(message); System.out.println("Done"); // Email email = new SimpleEmail(); // email.setHostName(host); // email.setSmtpPort(port); // // email.setAuthentication(senderUser, senderPassword); // email.setAuthenticator(new DefaultAuthenticator(senderUser, senderPassword)); // email.addTo(recepientEmail); // email.setFrom(senderEmail); // email.setSubject(subject); // email.setMsg(message); // //email.setSSL(true); // log.info(String.format("Sending Email to %s, to report successful setup.", recepientEmail)); // email.send(); log.debug("sendEmail(String host, String port, String protocol, String senderEmail, String senderUser," + "String senderPassword, String subject, String message, String recepientEmail) - end"); }
From source file:com.waveerp.sendMail.java
public void sendMsgTLS(String strSource, String strSourceDesc, String strSubject, String strMsg, String strDestination, String strDestDesc) throws Exception { // Call the registry management system registrySystem rs = new registrySystem(); // Call the encryption management system desEncryption de = new desEncryption(); de.Encrypter("", ""); String strHost = rs.readRegistry("NA", "NA", "NA", "EMAILHOST"); String strPort = rs.readRegistry("NA", "NA", "NA", "EMAILPORT"); final String strUser = rs.readRegistry("NA", "NA", "NA", "EMAILUSER"); String strPass = rs.readRegistry("NA", "NA", "NA", "EMAILPASSWORD"); //Decrypt the encrypted password. final String strPass01 = de.decrypt(strPass); Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", strHost); props.put("mail.smtp.port", strPort); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPass01); }// w ww. jav a 2s . c o m }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(strSource)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strDestination)); message.setSubject(strSubject); message.setText(strMsg); Transport.send(message); //System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } log(DEBUG, strHost + "|" + strPort + "|" + strUser + "|" + strPass); //Email email = new SimpleEmail(); //email.setHostName(strHost); //email.setSmtpPort( Integer.parseInt(strPort) ); //email.setAuthenticator(new DefaultAuthenticator(strUser, strPass01)); //email.setTLS(true); //email.setFrom(strSource, strSourceDesc); //email.setSubject(strSubject); //email.setMsg(strMsg); //email.addTo(strDestination, strDestDesc); //email.send(); }