public static void main(String[] args) { // Get the Properties and Create a default session Properties prop = System.getProperties(); prop.setProperty("mail.server.com", "127.0.0.1"); Session session = Session.getDefaultInstance(prop); try { // Set the mail headers MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("*****@*****.**")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("*****@*****.**")); msg.setSubject("First Mail"); // Create the mime body and attachments MimeBodyPart msgBody = new MimeBodyPart(); msgBody.setContent("Hello World", "text/html"); MimeBodyPart attFile = new MimeBodyPart(); attFile.attachFile("RecvMail.java"); Multipart partMsg = new MimeMultipart(); partMsg.addBodyPart(msgBody); partMsg.addBodyPart(attFile); msg.setContent(partMsg); Transport.send(msg); System.out.println("Message Successfully sent..."); } catch (Exception e) { e.printStackTrace(); } }
public void sendSimpleEmailAuthenticated( String SMTPHost, String userName, String password, String subject, String body, String toAddress, String fromAddress) throws MessagingException { SMTP_AUTH_USER = userName; SMTP_AUTH_PWD = password; Properties props = new Properties(); props.put("mail.smtp.host", SMTPHost); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session session = Session.getDefaultInstance(props, auth); MimeMessage message = new MimeMessage(session); message.setSubject(subject); message.setContent(body, "text/plain"); message.setFrom(new InternetAddress(fromAddress)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); Transport.send(message); }
public static void main(String[] args) { String to = "*****@*****.**"; // change accordingly String from = "*****@*****.**"; // change accordingly String host = "smtp.gmail.com"; // or IP address // Get the session object Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties); // compose the message try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Ping"); message.setText("Hello, this is example of sending email "); // Send message Transport.send(message); System.out.println("message sent successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } }
public static void sendMail(String mailMessage) { String to = "*****@*****.**"; String from = "FlotaWeb"; String host = "mail.arabesque.ro"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(properties); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Distributie"); message.setText(mailMessage); Transport.send(message); } catch (MessagingException e) { logger.error(Utils.getStackTrace(e)); } }
public static void sendMailBySea( String formAddress, String toAddress, String title, String content) throws Exception { Properties p = new Properties(); p.put("mail.smtp.host", "smtp.sina.com"); p.put("mail.smtp.port", "25"); p.put("mail.smtp.auth", "true"); Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("*****@*****.**", "password"); } }; Session sendMailSession = Session.getDefaultInstance(p, authenticator); Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(formAddress); mailMessage.setFrom(from); Address to = new InternetAddress(toAddress); // 设置接收人员 mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(title); // 设置邮件标题 mailMessage.setText(content); // 设置邮件内容 // 发送邮件 Transport.send(mailMessage); }
public void sendMailWithCalendar( String receivers, String subject, String body, String attachment) { log.info("Sending mail"); try { Authenticator authenticator = new Authenticator(serverDetails.userName, serverDetails.password); Session session = Session.getDefaultInstance(properties, authenticator); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(serverDetails.userName)); message.addRecipients(Message.RecipientType.TO, receivers); message.setSubject(subject); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(body); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage"); messageBodyPart.setHeader("Content-ID", "calendar_message"); messageBodyPart.setDataHandler( new DataHandler( new ByteArrayDataSource(Files.readAllBytes(Paths.get(attachment)), "text/calendar"))); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send(message); log.info("Mail sent"); } catch (Exception e) { log.severe("Could not send mail!" + e.getMessage()); } }
public static void sfSendEmail(String subject, String message) throws Exception { String SMTP_HOST_NAME = "smtp.gmail.com"; String SMTP_PORT = "465"; // message = "Test Email Notification From Monitor"; // subject = "Test Email Notification From Monitor"; String from = "*****@*****.**"; String[] recipients = { "*****@*****.**", "*****@*****.**", "*****@*****.**" }; String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // String[] recipients = { "*****@*****.**"}; // String[] recipients = { "*****@*****.**", "*****@*****.**", // "*****@*****.**", "*****@*****.**", "*****@*****.**", // "*****@*****.**", "*****@*****.**"}; // String[] recipients = {"*****@*****.**"}; Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); // props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance( props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "*****@*****.**", "els102sensorweb"); } }); // session.setDebug(debug); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/plain"); Transport.send(msg); System.out.println("Sucessfully Sent mail to All Users"); }
public static void sendMail(String recipient, String subject, String body) throws MessagingException { Session session = Session.getInstance( properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); message.setSubject(subject); message.setContent(body, "text/html; charset=utf-8"); logger.debug("send email to " + recipient); Transport.send(message); }
public static void email( CallingContext context, String templateName, Map<String, ? extends Object> templateValues) { final SMTPConfig config = getSMTPConfig(); Session session = getSession(config); EmailTemplate template = getEmailTemplate(context, templateName); try { // Instantiate a new MimeMessage and fill it with the required information. Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(config.getFrom())); String to = renderTemplate(template.getEmailTo(), templateValues); if (StringUtils.isBlank(to)) { throw RaptureExceptionFactory.create("No emailTo field"); } InternetAddress[] address = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(renderTemplate(template.getSubject(), templateValues)); msg.setSentDate(new Date()); msg.setContent( renderTemplate(template.getMsgBody(), templateValues), "text/html; charset=utf-8"); // Hand the message to the default transport service for delivery. Transport.send(msg); } catch (MessagingException e) { log.error("Failed to send email", e); } }
@Override public void sendAsHtml( String subject, String html, Collection<String> recipients, Map<String, Object> htmlParams) throws Exception { Address[] addresses = new Address[recipients.size()]; Iterator<String> iterator = recipients.iterator(); int i = 0; while (iterator.hasNext()) { addresses[i] = new InternetAddress(iterator.next()); i++; } Template template = configuration.getTemplate(html); Writer writer = new StringWriter(); template.process(htmlParams, writer); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(writer.toString(), "text/html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart); Message message = new MimeMessage(session); message.setFrom(); message.setRecipients(Message.RecipientType.TO, addresses); message.setSubject(subject); message.setContent(multipart, "text/html"); Transport.send(message); }
public void send() { // Your SMTP server address here. String smtpHost = "myserver.jeffcorp.com"; // The sender's email address String from = "*****@*****.**"; // The recepients email address String to = "*****@*****.**"; Properties props = new Properties(); // The protocol to use is SMTP props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null); try { InternetAddress[] address = {new InternetAddress(to)}; MimeMessage message; message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, to); message.setSubject("Hello from Jeff"); message.setSentDate(sendDate); message.setText("Hello Jeff, \nHow are things going?"); Transport.send(message); System.out.println("email has been sent."); } catch (Exception e) { System.out.println(e); } }
public void postMail(String recipients[], String subject, String message, String from) throws MessagingException { boolean debug = false; // Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.timeout", 60000); Authenticator auth = new SMTPAuthenticator(); Session session = Session.getDefaultInstance(props, auth); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(message, "text/html"); Transport.send(msg); }
public void SendEmail(String email, String contestToken) { String to = email; String from = "Tyumen_ACM_Society"; String host = "smtp.gmail.com"; String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtp.host", host); props.put("mail.stmp.user", "*****@*****.**"); props.put("mail.smtp.password", "475508th"); 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.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); Session session = Session.getDefaultInstance(props, new SmtpAuthenticator()); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Registration on acm.utmn.ru"); message.setText( "Hello! Thank you for registration on acm.utmn.ru! Use next token to submit tasks: " + contestToken); Transport t = session.getTransport("smtp"); t.send(message); System.out.println("Message was successfully sent."); } catch (MessagingException mex) { mex.printStackTrace(); } }
/** Sends Emails to Customers who have not submitted their Bears */ public static void BearEmailSendMessage(String msgsubject, String msgText, String msgTo) { try { BearFrom = props.getProperty("BEARFROM"); // To = props.getProperty("TO"); SMTPHost = props.getProperty("SMTPHOST"); Properties mailprops = new Properties(); mailprops.put("mail.smtp.host", SMTPHost); // create some properties and get the default Session Session session = Session.getDefaultInstance(mailprops, null); // create a message Message msg = new MimeMessage(session); // set the from InternetAddress from = new InternetAddress(BearFrom); msg.setFrom(from); InternetAddress[] address = InternetAddress.parse(msgTo); msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(msgsubject); msg.setContent(msgText, "text/plain"); Transport.send(msg); } // end try catch (MessagingException mex) { USFEnv.getLog().writeCrit("Message not sent", null, null); } catch (Exception ex) { USFEnv.getLog().writeCrit("Message not sent", null, null); } } // end BearEmailSendMessage
public void send(String subject, String text, String fromEmail, String toEmail) { Session session = Session.getDefaultInstance( props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); // от кого message.setFrom(new InternetAddress(username)); // кому message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); // тема сообщения message.setSubject(subject); // текст message.setText(text); // отправляем сообщение Transport.send(message); } catch (MessagingException e) { throw new RuntimeException(e); } }
public void mail(String toAddress, String subject, String body) { Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.ssl.enable", true); Authenticator authenticator = null; if (login) { props.put("mail.smtp.auth", true); authenticator = new Authenticator() { private PasswordAuthentication pa = new PasswordAuthentication(username, password); @Override public PasswordAuthentication getPasswordAuthentication() { return pa; } }; } Session s = Session.getInstance(props, authenticator); s.setDebug(debug); MimeMessage email = new MimeMessage(s); try { email.setFrom(new InternetAddress(from)); email.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); email.setSubject(subject); email.setSentDate(new Date()); email.setText(body); Transport.send(email); } catch (MessagingException ex) { ex.printStackTrace(); } }
public void sendMessage() throws FrameworkExecutionException { try { Transport.send(message); } catch (MessagingException me) { // do logging here throw new FrameworkExecutionException(me); } }
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 void sendEmail() throws MessagingException { if (msg.getFrom() == null) msg.setFrom(new InternetAddress("*****@*****.**")); msg.setHeader("X-mailer", "msgsend"); msg.setRecipients(Message.RecipientType.TO, (Address[]) toList.toArray(new Address[0])); msg.setRecipients(Message.RecipientType.CC, (Address[]) ccList.toArray(new Address[0])); msg.setRecipients(Message.RecipientType.BCC, (Address[]) bccList.toArray(new Address[0])); msg.setSentDate(new Date()); if (!toList.isEmpty()) Transport.send(msg); }
public void sendVerificationEmailLocally(String recipients, String verificationLink) { String body = String.format(bodyTemplate, verificationLink); log.debug("Sending email to recipients={}, subject={}, body={}", recipients, subject, body); // Gmail properties Properties smtpProperties = new Properties(); smtpProperties.put("mail.smtp.host", smtpHost); smtpProperties.put("mail.smtp.socketFactory.port", smtpPort); smtpProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); smtpProperties.put("mail.smtp.auth", "true"); smtpProperties.put("mail.smtp.port", "465"); Session session = Session.getInstance( smtpProperties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUsername, smtpPassword); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(fromAddress)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); message.setSubject(subject); message.setContent(body, "text/html; charset=utf-8"); Transport.send(message); log.info("Sent email to " + recipients); } catch (MessagingException e) { String smtpInfo = "Error sending email. SMTP_HOST=" + smtpHost + ", SMTP_PORT=" + smtpPort + ", smtpUsername="******", subject=" + subject; if (e.getCause() instanceof AuthenticationFailedException) { log.warn( "Failed to send mail due to missconfiguration? Reason {}", e.getCause().getMessage()); } throw new RuntimeException(smtpInfo, e); } }
public void sendEMailToUser(ICFSecuritySecUserObj toUser, String msgSubject, String msgBody) throws IOException, MessagingException, NamingException { final String S_ProcName = "sendEMailToUser"; Properties props = System.getProperties(); Context ctx = new InitialContext(); String smtpEmailFrom = (String) ctx.lookup("java:comp/env/CFInternet25SmtpEmailFrom"); if ((smtpEmailFrom == null) || (smtpEmailFrom.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFInternet25SmtpEmailFrom"); } smtpUsername = (String) ctx.lookup("java:comp/env/CFInternet25SmtpUsername"); if ((smtpUsername == null) || (smtpUsername.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFInternet25SmtpUsername"); } smtpPassword = (String) ctx.lookup("java:comp/env/CFInternet25SmtpPassword"); if ((smtpPassword == null) || (smtpPassword.length() <= 0)) { throw CFLib.getDefaultExceptionFactory() .newNullArgumentException( getClass(), S_ProcName, 0, "JNDI lookup for CFInternet25SmtpPassword"); } Session emailSess = Session.getInstance( props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUsername, smtpPassword); } }); MimeMessage msg = new MimeMessage(emailSess); msg.setFrom(new InternetAddress(smtpEmailFrom)); InternetAddress mailTo[] = InternetAddress.parse(toUser.getRequiredEMailAddress(), false); msg.setRecipient(Message.RecipientType.TO, mailTo[0]); msg.setSubject((msgSubject != null) ? msgSubject : "No subject"); msg.setSentDate(new Date()); msg.setContent(msgBody, "text/html"); msg.saveChanges(); Transport.send(msg); }
public void setInfected(boolean infected) { if (infected) { if (status == 0) { Linkable l = (Linkable) node.getProtocol(p.lid); Transport t = (Transport) node.getProtocol(p.tid); for (int i = 0; i < l.degree(); i++) { t.send(node, l.getNeighbor(i), INFECTION, p.pid); } status = 1; } } else { status = 0; } }
public void processEvent(Node node, int pid, Object event) { if (status == 2) return; Linkable l = (Linkable) node.getProtocol(p.lid); Transport t = (Transport) node.getProtocol(p.tid); if (event == TIMEOUT) { if (status == SUSCEPTIBLE) status = INFECTED; if (status == INFECTED) { EDSimulator.add(p.period, TIMEOUT, node, pid); int r = CommonState.r.nextInt(l.degree()); t.send(node, l.getNeighbor(r), INFECTION, pid); } } else if (event == INFECTION) { if (status == SUSCEPTIBLE) { status = INFECTED; EDSimulator.add(p.period, TIMEOUT, node, pid); int r = CommonState.r.nextInt(l.degree()); t.send(node, l.getNeighbor(r), INFECTION, pid); } else if (status == INFECTED) { float chance = CommonState.r.nextFloat(); if (chance < p.prob) status = REMOVED; } } }
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(); }
@Override public void notificate(FormContent formContent) { if (formContent == null) { throw new IllegalArgumentException("FormContent is null"); } String htmlContent = contentLoader.createHTMLEmailContent(formContent); try { Transport.send(createMessage(htmlContent)); } catch (MessagingException e) { LOG.error("Error occurred while sending message", e); } }
/* * The repsonding AL will be starting from zero state and thus would normally generate a * "permissive" LAST that permits the leader (also at zero state) to make a new proposal * immediately. We want the leader to have to clear out a previous proposal prior to that. So * we replace the "permissive" last, introducing a valid LAST that should cause the leader to * deliver up the included value and then re-propose(non-Javadoc) the original value above. */ public void send(Packet aPacket, InetSocketAddress anAddr) { if (_seenLast) _tp.send(aPacket, anAddr); else { if (aPacket.getMessage().getType() == PaxosMessage.Types.LAST) { ByteBuffer myBuffer = ByteBuffer.allocate(4); myBuffer.putInt(66); Last myOrig = (Last) aPacket.getMessage(); _seenLast = true; _tp.send( _tp.getPickler() .newPacket( new Last( myOrig.getSeqNum(), myOrig.getLowWatermark(), myOrig.getRndNumber() + 1, new Proposal("data", myBuffer.array()))), anAddr); } else { _tp.send(aPacket, anAddr); } } }
public void sendEmail(EmailBuilder builder) { // 收件人电子邮箱 String to = builder.getReceiver(); // 发件人电子邮箱 String from = builder.getSender(); // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", builder.getHost()); properties.put("mail.smtp.port", builder.getPort()); properties.put("mail.smtp.auth", builder.authNeeded()); // 获取默认session对象 Session session = Session.getDefaultInstance( properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, builder.getToken()); // 发件人邮件用户名、密码 } }); try { // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); message.addRecipients( Message.RecipientType.TO, new InternetAddress[] {new InternetAddress(to), new InternetAddress(to)}); // Set Subject: 头部头字段 message.setSubject(builder.getTitle()); // 设置消息体 message.setText(builder.getContent()); // 发送消息 Transport.send(message); System.out.println("Sent message successfully....from Java Mail"); } catch (MessagingException mex) { mex.printStackTrace(); } }
protected void send(Message message, long timestamp) { if (isDisabled()) { return; } try { transport.send(message.encoded(), timestamp); } catch (ConnectException e) { LOG.log(Level.SEVERE, e.getMessage(), e); // TODO Add backing off in case of errors } catch (FileNotFoundException e) { LOG.log(Level.SEVERE, e.getMessage(), e); // TODO Add backing off in case of errors } catch (IOException e) { LOG.log(Level.SEVERE, e.getMessage(), e); } }
@RequestMapping(method = RequestMethod.POST) public ResponseEntity<String> sendNewPassword(@RequestBody String data) throws UnsupportedEncodingException, JSONException { JSONObject json = new JSONObject(data); String email = (String) (json.get("email")); List<User> users = userRepository.findByEmail(email); if (users.size() > 0) { User user = users.get(0); String generatedString = UUID.randomUUID().toString().replaceAll("-", ""); user.setPassword(generatedString); userRepository.save(user); Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance( props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("perseuscontact", "PERSEUS1992"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("*****@*****.**", "Perseus App")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); message.setSubject("New password"); String text = "Hello" + user.getName() + "!.<\br> Your new password is :<b>" + generatedString + "</b><br/>You can change the passsword again."; message.setContent(text, "text/html; charset=utf-8"); Transport.send(message); System.out.println( "Message about new password sent to the user " + user.getEmail() + " - " + email); } catch (MessagingException e) { System.out.println("Error sending a 'user joined' message."); throw new RuntimeException(e); } } return new ResponseEntity<>(email, HttpStatus.CREATED); }