public static void sendEmail( InternetAddress from, InternetAddress to, String content, String requester) throws MessagingException, UnsupportedEncodingException { Properties props = new Properties(); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtps.host", SMTP_HOST_NAME); props.put("mail.smtps.auth", "true"); Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(false); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("MN Traffic Generation is used by " + requester); message.setContent(content, "text/plain"); message.addRecipient(Message.RecipientType.TO, to); InternetAddress[] replyto = new InternetAddress[1]; replyto[0] = from; message.setFrom(from); message.setReplyTo(replyto); message.setSender(from); transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); }
@Override @Before public void setUp() throws Exception { super.setUp(); src = createBareRepository(); dst = createBareRepository(); // Fill dst with a some common history. // TestRepository<Repository> d = new TestRepository<Repository>(dst); a = d.blob("a"); A = d.commit(d.tree(d.file("a", a))); B = d.commit().parent(A).create(); d.update(R_MASTER, B); // Clone from dst into src // Transport t = Transport.open(src, uriOf(dst)); try { t.fetch(PM, Collections.singleton(new RefSpec("+refs/*:refs/*"))); assertEquals(B, src.resolve(R_MASTER)); } finally { t.close(); } // Now put private stuff into dst. // b = d.blob("b"); P = d.commit(d.tree(d.file("b", b)), A); d.update(R_PRIVATE, P); }
public static void main(String[] args) throws MessagingException, IOException { Properties props = new Properties(); try (InputStream in = Files.newInputStream(Paths.get("mail", "mail.properties"))) { props.load(in); } List<String> lines = Files.readAllLines(Paths.get(args[0]), Charset.forName("UTF-8")); String from = lines.get(0); String to = lines.get(1); String subject = lines.get(2); StringBuilder builder = new StringBuilder(); for (int i = 3; i < lines.size(); i++) { builder.append(lines.get(i)); builder.append("\n"); } Console console = System.console(); String password = new String(console.readPassword("Password: ")); Session mailSession = Session.getDefaultInstance(props); // mailSession.setDebug(true); MimeMessage message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(from)); message.addRecipient(RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setText(builder.toString()); Transport tr = mailSession.getTransport(); try { tr.connect(null, password); tr.sendMessage(message, message.getAllRecipients()); } finally { tr.close(); } }
// Send the specified message. public void sendMessage(int type, Message message) throws Exception { // Display message dialog to get message values. MessageDialog dialog; dialog = new MessageDialog(this, null, type, message); if (!dialog.display()) { // Return if dialog was cancelled. return; } // Create a new message with values from dialog. Message newMessage = new MimeMessage(session); newMessage.setFrom(new InternetAddress(dialog.getFrom())); newMessage.setSubject(dialog.getSubject()); newMessage.setSentDate(new Date()); newMessage.setText(dialog.getContent()); final Address[] recipientAddresses = InternetAddress.parse(dialog.getTo()); newMessage.setRecipients(Message.RecipientType.TO, recipientAddresses); Transport transport = session.getTransport("smtps"); transport.connect(getSmtpServer(), GMAIL_SMTP_PORT, getUsername(), getPassword()); transport.sendMessage(newMessage, recipientAddresses); transport.close(); }
private void closeTransport() { if (currentTransport != null) { currentTransport.close(); stats.aggregate(currentTransport.stats()); currentTransport = null; } }
public void send() { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", SMTP_HOST_NAME); Session mailSession = Session.getDefaultInstance(props); // mailSession.setDebug(true); Transport transport; try { transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(userName)); message.setSubject(subject); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); if (!cc.equals("")) { message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc)); } if (!bcc.equals("")) { message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc)); } // create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); // fill message messageBodyPart.setText(content); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); if (fList != null) { Iterator<File> i = fList.iterator(); // part two is attachment while (i.hasNext()) { File file = (File) i.next(); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(file.getName()); multipart.addBodyPart(messageBodyPart); } } // Put parts in message message.setContent(multipart); Transport.send(message); transport.close(); } catch (Exception e) { e.printStackTrace(); } }
public synchronized void sendMail(Mail m) throws MessagingException { MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(m.getBody().getBytes(), "text/plain")); message.setSender(new InternetAddress(m.getSender())); message.setSubject(m.getSubject()); message.setDataHandler(handler); message.setRecipient(Message.RecipientType.TO, new InternetAddress(m.getRecipient())); Transport tr = session.getTransport("smtp"); tr.connect(user, password); tr.send(message); tr.close(); }
public static void sendMail(String to, String subject, String msg) throws MessagingException { final String from = "*****@*****.**"; final String password = "******"; // JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); // mailSender.setHost("smtp.gmail.com"); // mailSender.setPort(587); // mailSender.setUsername("farmayaa"); // mailSender.setPassword(password); // // SimpleMailMessage message = new SimpleMailMessage(); // // message.setFrom(from); // message.setTo(to); // message.setSubject(subject); // message.setText(msg); // mailSender.send(message); Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.debug", "true"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance( props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); // session.setDebug(true); Transport transport = session.getTransport(); InternetAddress addressFrom = new InternetAddress(from); MimeMessage message = new MimeMessage(session); message.setSender(addressFrom); message.setSubject(subject); message.setContent(msg, "text/plain"); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); transport.connect(); Transport.send(message); transport.close(); }
public void sendText(String subject, String textMessage, String... emails) { setProperties(); MimeMessage message = new MimeMessage(mailSession); try { for (String email : emails) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); } message.setSubject(subject); message.setText(textMessage); mailTransport.sendMessage(message, message.getAllRecipients()); mailTransport.close(); } catch (MessagingException e) { e.printStackTrace(); } }
public void sendMessage(NMessage m) throws Exception { ensureConnected(); // Create a new message with values from dialog. MimeMessage newMessage = new MimeMessage(session); newMessage.setFrom(new InternetAddress(from /* m.getFrom() */)); newMessage.setSubject(m.getName()); newMessage.setSentDate(m.getWhen()); newMessage.setContent(m.getContent(), "text/html"); final Address[] recipientAddresses = InternetAddress.parse(m.getTo()); newMessage.setRecipients(Message.RecipientType.TO, recipientAddresses); Transport transport = session.getTransport("smtps"); transport.connect(getSmtpServer(), GMAIL_SMTP_PORT, getUsername(), getPassword()); transport.sendMessage(newMessage, recipientAddresses); transport.close(); }
public static int sendMail(String toAddr, String ccAddr, String mailTitle, String mailConcept) { Session s = Session.getInstance(SendMail.props, null); s.setDebug(false); Message message = new MimeMessage(s); try { Address from = new InternetAddress(SendMail.SenderEmailAddr); message.setFrom(from); Address to = new InternetAddress(toAddr); message.setRecipient(Message.RecipientType.TO, to); if (ccAddr != null && ccAddr != "") { Address cc = new InternetAddress(ccAddr); message.setRecipient(Message.RecipientType.CC, cc); } message.setSubject(mailTitle); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailConcept, "text/html;charset=ISO-8859-1"); mainPart.addBodyPart(html); message.setContent(mainPart); message.setSentDate(new Date()); message.saveChanges(); Transport transport = s.getTransport(SendMail.TransprotType); transport.connect(SendMail.SMTPServerName, SendMail.SMTPUserName, SendMail.SMTPPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); // System.out.println("发送邮件,邮件地址:"+toAddr); // System.out.println("发送邮件,邮件地址:"+ccAddr); // System.out.println("标题:"+mailTitle); // System.out.println("内容:"+mailConcept); System.out.println("Email Send Success!"); return 1; } catch (Exception e) { System.out.println(e.getMessage()); return -1; } }
private static void sendFromGMail( String from, String pass, String to, String subject, String body) { 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 { message.setFrom(new InternetAddress(from)); // InternetAddress[] toAddress = new InternetAddress[to.length]; // To get the array of addresses /*for( int i = 0; i < to.length; i++ ) { toAddress[i] = new InternetAddress(to[i]); }*/ /*for( int i = 0; i < toAddress.length; i++) { message.addRecipient(Message.RecipientType.TO, toAddress[i]); }*/ message.setSubject(subject); message.setText(body); Transport transport = session.getTransport("smtp"); transport.connect(host, from, pass); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (AddressException ae) { ae.printStackTrace(); } catch (MessagingException me) { me.printStackTrace(); } }
public static void sendEmailByJava(String subject, String content, List<String> address) throws MailingException { Transport transport = null; try { Smtp smtp = new Smtp(); // Create the mail session Session mailSession; mailSession = Session.getInstance(smtp.toProperties()); // Create the connection transport = mailSession.getTransport(smtp.getKindConnection()); transport.connect(smtp.getHost(), smtp.getUser(), smtp.getPassword()); InternetAddress fromAddress = null; try { fromAddress = new InternetAddress( MiddlewareProperties.NOTIFICATIONS_EMAIL_ADDRESS, "WB-API Notifications"); } catch (UnsupportedEncodingException e) { } if (address != null && !address.isEmpty()) { MimeMessage msg = new MimeMessage(mailSession); msg.setSubject(subject, "utf-8"); InternetAddress[] addressTo = new InternetAddress[address.size()]; for (int i = 0; i < address.size(); i++) { addressTo[i] = new InternetAddress(address.get(i)); } // Set recipient mail if (fromAddress != null) { msg.setFrom(fromAddress); } msg.setRecipients(Message.RecipientType.BCC, addressTo); // Set body text MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(content, "text/html; charset=utf-8"); // Multipart message Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); // Send the message try { transport.sendMessage(msg, msg.getAllRecipients()); } catch (MessagingException e) { logger.error(e); throw new MailingException(e); } } else { logger.debug( "Trying to send a mail to a null or empty list of recipients. This is the content of the message:\n '" + content + "'"); } logger.debug("Email sent."); } catch (Exception e) { logger.error(e); throw new MailingException(e); } finally { try { if (transport != null && transport.isConnected()) { // Close the email connection transport.close(); } } catch (MessagingException e) { logger.error(e); } } }
protected void sendFinished() throws Exception { if (transport.isConnected()) { transport.close(); } }
public static void sendEmail(String email, String id) throws MessagingException, UnsupportedEncodingException { Properties props = new Properties(); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtps.host", SMTP_HOST_NAME); props.put("mail.smtps.auth", "true"); Session mailSession = Session.getDefaultInstance(props); mailSession.setDebug(false); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); message.setSubject("Traffic Generation Request #" + id + " has received"); message.setContent( "Hi Sir/Madam, \n\n Your traffic generation request #" + id + " has been received by DMLab@UMN. \n You will be notified via email " + email + " , when we finish our processing.\n\n Please be patient. If you have any inquries, please send email to [email protected]. \n\n Thanks, \n DMLab@UMN", "text/plain"); InternetAddress[] mntgAddress = new InternetAddress[1]; mntgAddress[0] = new InternetAddress( "*****@*****.**", "Minnesota Traffic Generator"); // here we set our email alias and the desired display // name InternetAddress customer_email = new InternetAddress(email); // customer email message.addRecipient(Message.RecipientType.TO, customer_email); message.addRecipients(Message.RecipientType.CC, mntgAddress); message.addRecipients(Message.RecipientType.BCC, mntgAddress); message.setFrom(mntgAddress[0]); message.setReplyTo(mntgAddress); message.setSender(mntgAddress[0]); transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD); Address[] recipientsTo = message.getRecipients(Message.RecipientType.TO); Address[] recipientsCC = message.getRecipients(Message.RecipientType.CC); Address[] recipientsBCC = message.getRecipients(Message.RecipientType.BCC); Address[] allRecipients = new Address[recipientsTo.length + recipientsCC.length + recipientsBCC.length]; int allIndex = 0; for (int i = 0; i < recipientsTo.length; ++i, ++allIndex) { allRecipients[allIndex] = recipientsTo[i]; } for (int i = 0; i < recipientsCC.length; ++i, ++allIndex) { allRecipients[allIndex] = recipientsCC[i]; } for (int i = 0; i < recipientsBCC.length; ++i, ++allIndex) { allRecipients[allIndex] = recipientsBCC[i]; } transport.sendMessage(message, allRecipients); transport.close(); // InternetAddress notifyAddress = new InternetAddress("*****@*****.**", "Admin MailList"); // //here we set our email alias and the desired display name // sendEmail(mntgAddress[0], notifyAddress, "Traffic request #" + id + " has just // been submitted by user " + email + ".", email); /* * InternetAddress notifyAddress3 = new * InternetAddress("*****@*****.**", "Mohamed Mokbel"); //here we set * our email alias and the desired display name * sendEmail(mntgAddress[0], notifyAddress3, "Traffic request #" + id + * " has just been submitted by user " + email + ".", email); * */ }
public static boolean sendMail( String userName, String passWord, String host, String port, String starttls, String auth, boolean debug, String socketFactoryClass, String fallback, String[] to, String[] cc, String[] bcc, String subject, String text, String attachmentPath, String attachmentName) { // Object Instantiation of a properties file. Properties props = new Properties(); props.put("mail.smtp.user", userName); props.put("mail.smtp.host", host); if (!"".equals(port)) { props.put("mail.smtp.port", port); } if (!"".equals(starttls)) { props.put("mail.smtp.starttls.enable", starttls); props.put("mail.smtp.auth", auth); } if (debug) { props.put("mail.smtp.debug", "true"); } else { props.put("mail.smtp.debug", "false"); } if (!"".equals(port)) { props.put("mail.smtp.socketFactory.port", port); } if (!"".equals(socketFactoryClass)) { props.put("mail.smtp.socketFactory.class", socketFactoryClass); } if (!"".equals(fallback)) { props.put("mail.smtp.socketFactory.fallback", fallback); } try { Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); MimeMessage msg = new MimeMessage(session); msg.setText(text); msg.setSubject(subject); Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachmentPath); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachmentName); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); msg.setFrom(new InternetAddress(userName)); for (int i = 0; i < to.length; i++) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } for (int i = 0; i < cc.length; i++) { msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i])); } for (int i = 0; i < bcc.length; i++) { msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i])); } msg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(host, userName, passWord); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); return true; } catch (Exception mex) { mex.printStackTrace(); return false; } }
@Override protected void doClose() throws ElasticsearchException { transport.close(); }
/** Method closes the transport layer */ private void close() { // close socket transport.close(); }
public static synchronized boolean sendMail( String userName, String passWord, String host, String port, String starttls, String auth, boolean debug, String socketFactoryClass, String fallback, String[] to, String[] cc, String[] bcc, String subject, String text) { Properties props = new Properties(); // Properties props=System.getProperties(); props.put("mail.smtp.user", userName); props.put("mail.smtp.host", host); if (!"".equals(port)) { props.put("mail.smtp.port", port); } if (!"".equals(starttls)) { props.put("mail.smtp.starttls.enable", starttls); } props.put("mail.smtp.auth", auth); if (debug) { props.put("mail.smtp.debug", "true"); } else { props.put("mail.smtp.debug", "false"); } if (!"".equals(port)) { props.put("mail.smtp.socketFactory.port", port); } if (!"".equals(socketFactoryClass)) { props.put("mail.smtp.socketFactory.class", socketFactoryClass); } if (!"".equals(fallback)) { props.put("mail.smtp.socketFactory.fallback", fallback); } try { Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); MimeMessage msg = new MimeMessage(session); msg.setText(text); msg.setSubject(subject); msg.setFrom(new InternetAddress("*****@*****.**")); for (int i = 0; i < to.length; i++) { msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } msg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(host, userName, passWord); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); return true; } catch (Exception mex) { mex.printStackTrace(); return false; } }