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 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(); } }
private void SendEmail(String password, String emailTo, String staffNames) { // Recipient's email ID needs to be mentioned. String to = emailTo; // Sender's email ID needs to be mentioned String from = "*****@*****.**"; // Assuming you are sending email from localhost String host = "smtp.upcmail.ie"; // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.setProperty("mail.smtp.host", host); // Get the default Session object. Session session = Session.getDefaultInstance(properties); 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("Password Change From Help Manager"); // Now set the actual message message.setText( "Hello " + staffNames + ", \n\n Your new Password is: " + password + ".\n\n Regards \n Help Manager Team"); // Send message Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } }
public void sendEmails(Homework hw, ArrayList<Student> students) { System.out.println("se apeleza"); String from = "*****@*****.**"; String host = "localhost"; Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.port", "25"); Session session = Session.getDefaultInstance(properties); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); for (Student s : students) { System.out.println("baga mesaj catre" + s.getEmail()); message.addRecipient(Message.RecipientType.TO, new InternetAddress(s.getEmail())); } message.setSubject("Homework"); // Send the actual HTML message, as big as you like message.setContent( "<h1>This is a test mail.</h1>" + "<p> It's a program I am developing and I just try to play with the mail API.<p>" + "<p> If you're receiving this, please ignore it!</p>" + "<p> You're homework is: </p>" + "<p>" + hw.getDescription() + "</p>" + "<p> Due Date: " + hw.getDueDate() + "</p>", "text/html"); // Send message Transport.send(message); System.out.println(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } }