public static void send( String nomContact, Integer numFacture, Double montantCommande, Integer nbLots, String typeEnvoi, String adresseContact) throws Exception { Properties props = System.getProperties(); props.put("mail.smtps.host", "smtp.orange.fr"); props.put("mail.smtps.auth", "true"); Session session = Session.getInstance(props, null); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("*****@*****.**")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adresseContact, false)); String corpsMessage = "Bonjour " + nomContact + "\n\n" + "votre commande n°" + numFacture + " d'un montant de " + montantCommande + "€ TTC vient d'être traitée.\n\n" + typeEnvoi + " \n\n" + "L'équipe du ciné cap vert vous remercie de votre confiance.\n\n\n\n" + " P.S.: Retrouvez en pièce jointe le mode d'emploi des chèques cinéma sur notre interface de réservation en ligne"; msg.setSubject("Votre commande de " + nbLots + " lots de chèques cinéma vient d'être traitée."); msg.setHeader("X-Mailer", "Test En-tete"); msg.setSentDate(new Date()); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(corpsMessage); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message mbp2.attachFile("ccvad.png"); Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); t.connect("smtp.orange.fr", "*****@*****.**", "popcorn21800"); t.sendMessage(msg, msg.getAllRecipients()); System.out.println("Réponse: " + t.getLastServerResponse()); t.close(); }
private static void sendSMTPMessage( Session session, String mailHost, String user, String password, Message msg) throws MessagingException { SMTPTransport t = (SMTPTransport) session.getTransport("smtp"); t.connect(mailHost, 25, user, password); t.sendMessage(msg, msg.getAllRecipients()); t.close(); }
private void sendMailMessage( SVNLogEntry logEntry, RepositoryName repositoryName, RepositoryConfiguration configuration) { try { final Message msg = createMessage(logEntry, repositoryName, configuration.getMailTemplate()); final SMTPTransport transport = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp"); try { if (auth) { transport.connect(host, user, password); } else { transport.connect(); } transport.sendMessage(msg, msg.getAllRecipients()); LOGGER.debug("Notification mail was sent successfully"); } finally { transport.close(); } } catch (Exception e) { LOGGER.error("Unable to send notification mail", e); } }
public void send(EmailBuilder[] emails, SmtpConfig smtp) throws AddressException, MessagingException, IOException { Properties props = System.getProperties(); if (smtp.getHost() != null) { props.put("mail.smtp.host", smtp.getHost()); } if (smtp.isAuth()) { props.put("mail.smtp.auth", "true"); props.put("mail.smtps.auth", "true"); } if (smtp.isStarttls()) { props.put("mail.smtp.starttls.enable", "true"); } // Get a Session object Session session = Session.getInstance(props, null); if (debug) { session.setDebug(true); } SMTPTransport transport = null; try { for (EmailBuilder email : emails) { // construct the message Message msg = email.getMessage(email, session); msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date()); if (transport == null) { transport = (SMTPTransport) session.getTransport(smtp.isSsl() ? "smtps" : "smtp"); if (smtp.isAuth()) { transport.connect(smtp.getHost(), smtp.getUser(), smtp.getPassword()); } else { transport.connect(); } } try { transport.sendMessage(msg, msg.getAllRecipients()); } finally { if (verbose) { System.out.println("SmtpMailer: Response = " + transport.getLastServerResponse()); } } if (verbose) { System.out.println("SmtpMailer: E-Mail successfully sent."); } } } finally { if (transport != null) transport.close(); } }
public static void envioCorreoZimbra( String destinatarios, String asunto, String detalle, List<File> listAdjunto, String emailEmisor, String passEmail) throws Exception { Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", true); properties.put("mail.smtp.ssl.trust", "smtp.gmail.com"); properties.put("mail.smtp.starttls.enable", true); Session session = Session.getInstance(properties, null); Message mensaje = new MimeMessage(session); mensaje.setFrom(new InternetAddress(emailEmisor)); try { mensaje.addRecipient(Message.RecipientType.TO, new InternetAddress(destinatarios)); // Message.RecipientType.CC; con copia // Message.RecipientType.BCC; con copia oculta } catch (Exception e) { e.printStackTrace(); } mensaje.setSubject(asunto); mensaje.setSentDate(new Date()); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(detalle); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); if (listAdjunto != null && !listAdjunto.isEmpty()) { for (File adjunto : listAdjunto) { messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(adjunto))); messageBodyPart.setFileName(adjunto.getName()); multipart.addBodyPart(messageBodyPart); } } mensaje.setContent(multipart); System.clearProperty("javax.net.ssl.keyStore"); System.clearProperty("javax.net.ssl.keyStorePassword"); System.clearProperty("javax.net.ssl.trustStore"); System.clearProperty("javax.net.ssl.trustStorePassword"); SMTPTransport transport = (SMTPTransport) session.getTransport("smtp"); try { transport.connect(emailEmisor, passEmail); transport.sendMessage(mensaje, mensaje.getAllRecipients()); } finally { transport.close(); } }
public void sendEmail() { // Recipient's email ID needs to be mentioned. String to = "*****@*****.**"; // Sender's email ID needs to be mentioned String from = "*****@*****.**"; // Assuming you are sending email from localhost String host = "smtp.gmail.com"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); properties.setProperty("mail.smtp.socketFactory.fallback", "false"); properties.setProperty("mail.smtp.port", "465"); // properties.setProperty("mail.smtp.socketFactory.port", "465"); properties.setProperty("mail.smtps.auth", "true"); properties.setProperty("mail.user", "*****@*****.**"); properties.setProperty("mail.password", "WHyq1993"); // Get the default Session object. Session session = Session.getDefaultInstance(properties); // Session session = Session.getDefaultInstance(properties,new // javax.mail.Authenticator(){ // // protected PasswordAuthentication getPasswordAuthentication() { // return new PasswordAuthentication("*****@*****.**", "WHyq1993"); // } // }); try { // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject(this.getSubject() + ": From " + this.getEmail()); // Now set the actual message message.setText(this.getText()); // Send message SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); t.connect("smtp.gmail.com", "*****@*****.**", "WHyq1993"); t.sendMessage(message, message.getAllRecipients()); // Transport.send(message); System.out.println("Sent message successfully...."); t.close(); FacesContext.getCurrentInstance() .addMessage( null, new FacesMessage( FacesMessage.SEVERITY_INFO, "Info : " + "Emails has been sent successfully!", "")); } catch (MessagingException mex) { System.out.println("Error"); mex.printStackTrace(); } }
// TODO: make sure this handles SSL transport (useAuth is true) and regular public void sendAlert( short alertType, long dataCenterId, Long podId, Long clusterId, String subject, String content) throws MessagingException, UnsupportedEncodingException { AlertVO alert = null; if ((alertType != AlertManager.ALERT_TYPE_HOST) && (alertType != AlertManager.ALERT_TYPE_USERVM) && (alertType != AlertManager.ALERT_TYPE_DOMAIN_ROUTER) && (alertType != AlertManager.ALERT_TYPE_CONSOLE_PROXY) && (alertType != AlertManager.ALERT_TYPE_STORAGE_MISC) && (alertType != AlertManager.ALERT_TYPE_MANAGMENT_NODE)) { alert = _alertDao.getLastAlert(alertType, dataCenterId, podId, clusterId); } if (alert == null) { // set up a new alert AlertVO newAlert = new AlertVO(); newAlert.setType(alertType); newAlert.setSubject(subject); newAlert.setClusterId(clusterId); newAlert.setPodId(podId); newAlert.setDataCenterId(dataCenterId); newAlert.setSentCount(1); // initialize sent count to 1 since we are now sending an alert newAlert.setLastSent(new Date()); _alertDao.persist(newAlert); } else { if (s_logger.isDebugEnabled()) { s_logger.debug( "Have already sent: " + alert.getSentCount() + " emails for alert type '" + alertType + "' -- skipping send email"); } return; } if (_smtpSession != null) { SMTPMessage msg = new SMTPMessage(_smtpSession); msg.setSender(new InternetAddress(_emailSender, _emailSender)); msg.setFrom(new InternetAddress(_emailSender, _emailSender)); for (InternetAddress address : _recipientList) { msg.addRecipient(RecipientType.TO, address); } msg.setSubject(subject); msg.setSentDate(new Date()); msg.setContent(content, "text/plain"); msg.saveChanges(); SMTPTransport smtpTrans = null; if (_smtpUseAuth) { smtpTrans = new SMTPSSLTransport( _smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword)); } else { smtpTrans = new SMTPTransport( _smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword)); } smtpTrans.connect(); smtpTrans.sendMessage(msg, msg.getAllRecipients()); smtpTrans.close(); } }