// 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(); }
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(); } }
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(); }
public XMLizable send( String soapAction, QName requestElement, XMLizable request, QName responseElement, Class responseType) throws ConnectionException { long startTime = System.currentTimeMillis(); try { boolean firstTime = true; while (true) { try { Transport transport = newTransport(config); OutputStream out = transport.connect(url, soapAction); sendRequest(out, request, requestElement); InputStream in = transport.getContent(); XMLizable result; result = receive(transport, responseElement, responseType, in); return result; } catch (SessionTimedOutException se) { if (config.getSessionRenewer() == null || !firstTime) { throw (ConnectionException) se.getCause(); } else { SessionRenewer.SessionRenewalHeader sessionHeader = config.getSessionRenewer().renewSession(config); if (sessionHeader != null) { addHeader(sessionHeader.name, sessionHeader.headerElement); } } } firstTime = false; } } catch (SocketTimeoutException e) { long timeTaken = System.currentTimeMillis() - startTime; throw new ConnectionException( "Request to " + url + " timed out. TimeTaken=" + timeTaken + " ConnectionTimeout=" + config.getConnectionTimeout() + " ReadTimeout=" + config.getReadTimeout(), e); } catch (IOException e) { throw new ConnectionException("Failed to send request to " + url, e); } }
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 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(); }
protected void sendInit() throws Exception { // Set smtp properties Properties s_props = new Properties(); s_props.put("mail.smtp.host", mailHost); s_props.put("mail.smtp.port", mailPort); s_props.put("mail.smtp.connectiontimeout", mailConnTimeout); s_props.put("mail.smtp.timeout", mailSmtpTimeout); // Creating messaging session Session session = Session.getInstance(s_props, null); session.setDebug(debug); // Creating transport object for smtp transport = session.getTransport("smtp"); transport.connect(mailHost, mailUser, mailPass); }
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(); }
private void setProperties() { Properties emailProperties = System.getProperties(); emailProperties.put("mail.smtp.port", "587"); emailProperties.put("mail.smtp.auth", "true"); emailProperties.put("mail.smtp.starttls.enable", "true"); mailSession = Session.getDefaultInstance(emailProperties, null); /** Sender's credentials */ String user = "******"; String password = "******"; String host = "smtp.gmail.com"; try { mailTransport = mailSession.getTransport("smtp"); mailTransport.connect(host, user, password); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } }
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 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 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); } } }
public boolean sendMessage() { Properties properties = new Properties(); properties.put("mail.smtp.host", mailHost); properties.put("mail.from", Source); Session session = Session.getInstance(properties, null); try { Message message = new MimeMessage(session); InternetAddress[] addressTO = null; if (this.Destination != null && this.Destination.length() != 0) { StringTokenizer TOList = getTokenizer(this.Destination); addressTO = new InternetAddress[TOList.countTokens()]; for (int i = 0; i < addressTO.length; i++) { addressTO[i] = new InternetAddress(TOList.nextToken()); message.addRecipient(Message.RecipientType.TO, addressTO[i]); } } if (this.MsgCC != null && this.MsgCC.length() != 0) { StringTokenizer CCList = getTokenizer(this.MsgCC); InternetAddress[] addressCC = new InternetAddress[CCList.countTokens()]; for (int i = 0; i < addressCC.length; i++) { addressCC[i] = new InternetAddress(CCList.nextToken()); message.addRecipient(Message.RecipientType.CC, addressCC[i]); } } message.setFrom(new InternetAddress(Source)); message.setSubject(Subject); Content = getHtmlHeader() + Content + getHtmlFooter(); Content = Content.replaceAll("\\{style\\}", MailStyle); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(Content); messageBodyPart.setContent(Content, "text/html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); Iterator it = this.BinaryAttachments.iterator(); while (it.hasNext()) { ByteArrayDataSource bads = (ByteArrayDataSource) it.next(); messageBodyPart = new MimeBodyPart(); // messageBodyPart.setDataHandler(new DataHandler(new FileDataSource("c:/test/tom.jpg"))); messageBodyPart.setDataHandler(new DataHandler(bads)); messageBodyPart.setFileName(bads.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); Transport transport = session.getTransport(addressTO[0]); transport.addConnectionListener(new ConnectionHandler()); transport.addTransportListener(new TransportHandler()); transport.connect(); transport.send(message); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
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; } }
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; } }