├── .gitignore ├── README.md ├── commons-email-example ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── javamail │ │ ├── CommonsEmailService.java │ │ └── ExampleUI.java │ └── resources │ ├── file.pdf │ └── mail.properties ├── javamail-example ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── javamail │ │ ├── ExampleUI.java │ │ └── JavaMailService.java │ └── resources │ ├── file.pdf │ └── mail.properties └── spring-mail-example ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── example │ └── javamail │ ├── ExampleUI.java │ └── SpringEmailService.java ├── resources ├── file.pdf └── mail.properties └── webapp └── WEB-INF └── applicationContext.xml /.gitignore: -------------------------------------------------------------------------------- 1 | */.idea/* 2 | rebel.xml 3 | *.iml 4 | */src/main/webapp/VAADIN/themes/mytheme/styles.css 5 | */target 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java-email-tutorial 2 | 3 | This exmaple shows how to send emails from Java applications in three different ways: 4 | 5 | 1. [Commons Email](https://github.com/alejandro-du/java-email-tutorial/tree/master/commons-email-example) 6 | 2. [Spring Mail Support](https://github.com/alejandro-du/java-email-tutorial/tree/master/spring-mail-example) 7 | 3. [Plain JavaMail](https://github.com/alejandro-du/java-email-tutorial/tree/master/javamail-example) 8 | 9 | [Read the complete tutorial here](https://vaadin.com/wiki?p_p_id=36&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=row-1&p_p_col_pos=1&p_p_col_count=3&p_r_p_185834411_title=Sending+Email+from+Java+Applications&p_r_p_185834411_nodeName=vaadin.com+wiki&_36_struts_action=%2Fwiki%2Fview). 10 | -------------------------------------------------------------------------------- /commons-email-example/README.md: -------------------------------------------------------------------------------- 1 | # Sending email using Apache Commons Email 2 | 3 | 1. Checkout the project form Git, and build and run with Maven: 4 | 5 | ``` 6 | git clone https://github.com/alejandro-du/java-email-tutorial 7 | 8 | cd java-email-tutorial 9 | 10 | cd commons-email-example 11 | 12 | mvn clean install 13 | 14 | mvn jetty:run 15 | ``` 16 | 17 | 2. Download and run [FakeSMTP](https://nilhcem.github.io/FakeSMTP). 18 | 19 | 3. Configure FakeSMTP listening port to `9090` and start the server. 20 | 21 | 4. Open [http://localhost:8080](http://localhost:8080) in your browser. 22 | -------------------------------------------------------------------------------- /commons-email-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.test 7 | vaadin-app 8 | war 9 | 1.0-SNAPSHOT 10 | vaadin-app 11 | 12 | 13 | 3 14 | 15 | 16 | 17 | 8.0.5 18 | 8.0.5 19 | 9.3.9.v20160517 20 | UTF-8 21 | 1.8 22 | 1.8 23 | 24 | local 25 | 26 | 27 | 28 | 29 | vaadin-addons 30 | http://maven.vaadin.com/vaadin-addons 31 | 32 | 33 | 34 | 35 | 36 | 37 | com.vaadin 38 | vaadin-bom 39 | ${vaadin.version} 40 | pom 41 | import 42 | 43 | 44 | 45 | 46 | 47 | 48 | javax.servlet 49 | javax.servlet-api 50 | 3.0.1 51 | provided 52 | 53 | 54 | com.vaadin 55 | vaadin-server 56 | 57 | 58 | com.vaadin 59 | vaadin-push 60 | 61 | 62 | com.vaadin 63 | vaadin-client-compiled 64 | 65 | 66 | com.vaadin 67 | vaadin-themes 68 | 69 | 70 | 71 | 72 | org.apache.commons 73 | commons-email 74 | 1.4 75 | 76 | 77 | 78 | 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-war-plugin 83 | 3.0.0 84 | 85 | false 86 | 87 | WEB-INF/classes/VAADIN/widgetsets/WEB-INF/** 88 | 89 | 90 | 91 | com.vaadin 92 | vaadin-maven-plugin 93 | ${vaadin.plugin.version} 94 | 95 | 96 | 97 | update-theme 98 | update-widgetset 99 | compile 100 | 101 | compile-theme 102 | 103 | 104 | 105 | 106 | 107 | org.apache.maven.plugins 108 | maven-clean-plugin 109 | 3.0.0 110 | 111 | 112 | 113 | 114 | src/main/webapp/VAADIN/themes 115 | 116 | **/styles.css 117 | **/styles.scss.cache 118 | 119 | 120 | 121 | 122 | 123 | 124 | 126 | 127 | org.eclipse.jetty 128 | jetty-maven-plugin 129 | ${jetty.plugin.version} 130 | 131 | 2 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | vaadin-prerelease 141 | 142 | false 143 | 144 | 145 | 146 | 147 | vaadin-prereleases 148 | http://maven.vaadin.com/vaadin-prereleases 149 | 150 | 151 | vaadin-snapshots 152 | https://oss.sonatype.org/content/repositories/vaadin-snapshots/ 153 | 154 | false 155 | 156 | 157 | true 158 | 159 | 160 | 161 | 162 | 163 | vaadin-prereleases 164 | http://maven.vaadin.com/vaadin-prereleases 165 | 166 | 167 | vaadin-snapshots 168 | https://oss.sonatype.org/content/repositories/vaadin-snapshots/ 169 | 170 | false 171 | 172 | 173 | true 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /commons-email-example/src/main/java/com/example/javamail/CommonsEmailService.java: -------------------------------------------------------------------------------- 1 | package com.example.javamail; 2 | 3 | import org.apache.commons.mail.EmailException; 4 | import org.apache.commons.mail.HtmlEmail; 5 | 6 | import javax.activation.DataHandler; 7 | import javax.mail.MessagingException; 8 | import javax.mail.util.ByteArrayDataSource; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.util.*; 12 | 13 | /** 14 | * @author alejandro@vaadin.com 15 | **/ 16 | public class CommonsEmailService { 17 | 18 | /** 19 | * Sends an email message with no attachments. 20 | * 21 | * @param from email address from which the message will be sent. 22 | * @param recipients the recipients of the message. 23 | * @param subject subject header field. 24 | * @param text content of the message. 25 | * @throws MessagingException 26 | * @throws IOException 27 | */ 28 | public static void send(String from, Collection recipients, String subject, String text) 29 | throws IOException, EmailException { 30 | send(from, recipients, subject, text, null, null, null); 31 | } 32 | 33 | /** 34 | * Sends an email message to one recipient with one attachment. 35 | * 36 | * @param from email address from which the message will be sent. 37 | * @param recipient the recipients of the message. 38 | * @param subject subject header field. 39 | * @param text content of the message. 40 | * @param attachment attachment to be included with the message. 41 | * @param fileName file name of the attachment. 42 | * @param mimeType mime type of the attachment. 43 | * @throws MessagingException 44 | * @throws IOException 45 | */ 46 | public static void send(String from, String recipient, String subject, String text, InputStream attachment, 47 | String fileName, String mimeType) 48 | throws IOException, EmailException { 49 | send(from, Arrays.asList(recipient), subject, text, Arrays.asList(attachment), Arrays.asList(fileName), 50 | Arrays.asList(mimeType)); 51 | } 52 | 53 | /** 54 | * Sends an email message with attachments. 55 | * 56 | * @param from email address from which the message will be sent. 57 | * @param recipients array of strings containing the recipients of the message. 58 | * @param subject subject header field. 59 | * @param text content of the message. 60 | * @param attachments attachments to be included with the message. 61 | * @param fileNames file names for each attachment. 62 | * @param mimeTypes mime types for each attachment. 63 | * @throws MessagingException 64 | * @throws IOException 65 | */ 66 | public static void send(String from, Collection recipients, String subject, String text, 67 | List attachments, List fileNames, List mimeTypes) 68 | throws EmailException, IOException { 69 | 70 | // check for null references 71 | Objects.requireNonNull(from); 72 | Objects.requireNonNull(recipients); 73 | 74 | // load email configuration from properties file 75 | Properties properties = new Properties(); 76 | properties.load(CommonsEmailService.class.getResourceAsStream("/mail.properties")); 77 | String host = properties.getProperty("mail.smtp.host"); 78 | String port = properties.getProperty("mail.smtp.port"); 79 | String ssl = properties.getProperty("mail.smtp.ssl.enable"); 80 | String username = properties.getProperty("mail.smtp.username"); 81 | String password = properties.getProperty("mail.smtp.password"); 82 | 83 | // create an email message with html support 84 | HtmlEmail email = new HtmlEmail(); 85 | 86 | // configure SMTP connection 87 | email.setHostName(host); 88 | email.setSmtpPort(Integer.parseInt(port)); 89 | email.setAuthentication(username, password); 90 | email.setSSLOnConnect(Boolean.parseBoolean(ssl)); 91 | 92 | // set its properties accordingly 93 | email.setFrom(from); 94 | email.addTo(recipients.toArray(new String[]{})); 95 | email.setSubject(subject); 96 | email.setHtmlMsg(text); 97 | 98 | if (attachments != null) { 99 | for (int i = 0; i < attachments.size(); i++) { 100 | // create a data source to wrap the attachment and its mime type 101 | ByteArrayDataSource dataSource = new ByteArrayDataSource(attachments.get(i), mimeTypes.get(i)); 102 | 103 | // add the attachment 104 | email.attach(dataSource, fileNames.get(i), "attachment"); 105 | } 106 | } 107 | 108 | // send it! 109 | email.send(); 110 | } 111 | 112 | } -------------------------------------------------------------------------------- /commons-email-example/src/main/java/com/example/javamail/ExampleUI.java: -------------------------------------------------------------------------------- 1 | package com.example.javamail; 2 | 3 | import com.vaadin.annotations.VaadinServletConfiguration; 4 | import com.vaadin.server.VaadinRequest; 5 | import com.vaadin.server.VaadinServlet; 6 | import com.vaadin.ui.*; 7 | 8 | import javax.servlet.annotation.WebServlet; 9 | import java.io.InputStream; 10 | 11 | /** 12 | * @author alejandro@vaadin.com 13 | **/ 14 | public class ExampleUI extends UI { 15 | 16 | @Override 17 | protected void init(VaadinRequest vaadinRequest) { 18 | // the text field where users will specify their email address 19 | TextField textField = new TextField("Your email:"); 20 | 21 | // a button with a click listener that sends the email 22 | Button button = new Button("Send me the PDF", e -> sendEmail(textField.getValue())); 23 | 24 | // a layout containing the previous components 25 | VerticalLayout layout = new VerticalLayout(textField, button); 26 | setContent(layout); // sets the content for this UI 27 | } 28 | 29 | private void sendEmail(String to) { 30 | try { 31 | // all values as variables to clarify its usage 32 | InputStream inputStream = getClass().getResourceAsStream("/file.pdf"); 33 | String from = "sender@test.com"; 34 | String subject = "Your PDF"; 35 | String text = "Here there is your PDF file!"; 36 | String fileName = "file.pdf"; 37 | String mimeType = "application/pdf"; 38 | 39 | // call the email service to send the message 40 | CommonsEmailService.send(from, to, subject, text, inputStream, fileName, mimeType); 41 | 42 | inputStream.close(); 43 | 44 | Notification.show("Email sent"); 45 | 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | Notification.show("Error sending the email", Notification.Type.ERROR_MESSAGE); 49 | } 50 | } 51 | 52 | @WebServlet(urlPatterns = "/*", name = "ExampleUIServlet", asyncSupported = true) 53 | @VaadinServletConfiguration(ui = ExampleUI.class, productionMode = false) 54 | public static class ExampleUIServlet extends VaadinServlet { 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /commons-email-example/src/main/resources/file.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alejandro-du/java-email-tutorial/e71768868e232aaab29730449ed3456be7f81ef1/commons-email-example/src/main/resources/file.pdf -------------------------------------------------------------------------------- /commons-email-example/src/main/resources/mail.properties: -------------------------------------------------------------------------------- 1 | # Test SMTP server: 2 | mail.smtp.host=localhost 3 | mail.smtp.port=9090 4 | mail.smtp.username=sender@test.com 5 | mail.smtp.password=password 6 | mail.smtp.ssl.enable=false 7 | 8 | # Gmail's SMTP server: 9 | # mail.smtp.host=smtp.gmail.com 10 | # mail.smtp.port=465 11 | # mail.smtp.ssl.enable=true 12 | # mail.smtp.username=your-account@gmail.com 13 | # mail.smtp.password=your-password 14 | -------------------------------------------------------------------------------- /javamail-example/README.md: -------------------------------------------------------------------------------- 1 | # Sending email using JavaMail 2 | 3 | 1. Checkout the project form Git, and build and run with Maven: 4 | 5 | ``` 6 | git clone https://github.com/alejandro-du/java-email-tutorial 7 | 8 | cd java-email-tutorial 9 | 10 | cd javamail-example 11 | 12 | mvn clean install 13 | 14 | mvn jetty:run 15 | ``` 16 | 17 | 2. Download and run [FakeSMTP](https://nilhcem.github.io/FakeSMTP). 18 | 19 | 3. Configure FakeSMTP listening port to `9090` and start the server. 20 | 21 | 4. Open [http://localhost:8080](http://localhost:8080) in your browser. 22 | -------------------------------------------------------------------------------- /javamail-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.test 7 | vaadin-app 8 | war 9 | 1.0-SNAPSHOT 10 | vaadin-app 11 | 12 | 13 | 3 14 | 15 | 16 | 17 | 8.0.5 18 | 8.0.5 19 | 9.3.9.v20160517 20 | UTF-8 21 | 1.8 22 | 1.8 23 | 24 | local 25 | 26 | 27 | 28 | 29 | vaadin-addons 30 | http://maven.vaadin.com/vaadin-addons 31 | 32 | 33 | 34 | 35 | 36 | 37 | com.vaadin 38 | vaadin-bom 39 | ${vaadin.version} 40 | pom 41 | import 42 | 43 | 44 | 45 | 46 | 47 | 48 | javax.servlet 49 | javax.servlet-api 50 | 3.0.1 51 | provided 52 | 53 | 54 | com.vaadin 55 | vaadin-server 56 | 57 | 58 | com.vaadin 59 | vaadin-push 60 | 61 | 62 | com.vaadin 63 | vaadin-client-compiled 64 | 65 | 66 | com.vaadin 67 | vaadin-themes 68 | 69 | 70 | org.apache.commons 71 | commons-email 72 | 1.4 73 | 74 | 75 | 76 | 77 | 78 | 79 | org.apache.maven.plugins 80 | maven-war-plugin 81 | 3.0.0 82 | 83 | false 84 | 85 | WEB-INF/classes/VAADIN/widgetsets/WEB-INF/** 86 | 87 | 88 | 89 | com.vaadin 90 | vaadin-maven-plugin 91 | ${vaadin.plugin.version} 92 | 93 | 94 | 95 | update-theme 96 | update-widgetset 97 | compile 98 | 99 | compile-theme 100 | 101 | 102 | 103 | 104 | 105 | org.apache.maven.plugins 106 | maven-clean-plugin 107 | 3.0.0 108 | 109 | 110 | 111 | 112 | src/main/webapp/VAADIN/themes 113 | 114 | **/styles.css 115 | **/styles.scss.cache 116 | 117 | 118 | 119 | 120 | 121 | 122 | 124 | 125 | org.eclipse.jetty 126 | jetty-maven-plugin 127 | ${jetty.plugin.version} 128 | 129 | 2 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | vaadin-prerelease 139 | 140 | false 141 | 142 | 143 | 144 | 145 | vaadin-prereleases 146 | http://maven.vaadin.com/vaadin-prereleases 147 | 148 | 149 | vaadin-snapshots 150 | https://oss.sonatype.org/content/repositories/vaadin-snapshots/ 151 | 152 | false 153 | 154 | 155 | true 156 | 157 | 158 | 159 | 160 | 161 | vaadin-prereleases 162 | http://maven.vaadin.com/vaadin-prereleases 163 | 164 | 165 | vaadin-snapshots 166 | https://oss.sonatype.org/content/repositories/vaadin-snapshots/ 167 | 168 | false 169 | 170 | 171 | true 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | -------------------------------------------------------------------------------- /javamail-example/src/main/java/com/example/javamail/ExampleUI.java: -------------------------------------------------------------------------------- 1 | package com.example.javamail; 2 | 3 | import com.vaadin.annotations.VaadinServletConfiguration; 4 | import com.vaadin.server.VaadinRequest; 5 | import com.vaadin.server.VaadinServlet; 6 | import com.vaadin.ui.*; 7 | 8 | import javax.servlet.annotation.WebServlet; 9 | import java.io.InputStream; 10 | 11 | /** 12 | * @author alejandro@vaadin.com 13 | **/ 14 | public class ExampleUI extends UI { 15 | 16 | @Override 17 | protected void init(VaadinRequest vaadinRequest) { 18 | // the text field where users will specify their email address 19 | TextField textField = new TextField("Your email:"); 20 | 21 | // a button with a click listener that sends the email 22 | Button button = new Button("Send me the PDF", e -> sendEmail(textField.getValue())); 23 | 24 | // a layout containing the previous components 25 | VerticalLayout layout = new VerticalLayout(textField, button); 26 | setContent(layout); // sets the content for this UI 27 | } 28 | 29 | private void sendEmail(String to) { 30 | try { 31 | // all values as variables to clarify its usage 32 | InputStream inputStream = getClass().getResourceAsStream("/file.pdf"); 33 | String from = "sender@test.com"; 34 | String subject = "Your PDF"; 35 | String text = "Here there is your PDF file!"; 36 | String fileName = "file.pdf"; 37 | String mimeType = "application/pdf"; 38 | 39 | // call the mail service to send the message 40 | JavaMailService.send(from, to, subject, text, inputStream, fileName, mimeType); 41 | 42 | inputStream.close(); 43 | 44 | Notification.show("Email sent"); 45 | 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | Notification.show("Error sending the email", Notification.Type.ERROR_MESSAGE); 49 | } 50 | } 51 | 52 | @WebServlet(urlPatterns = "/*", name = "ExampleUIServlet", asyncSupported = true) 53 | @VaadinServletConfiguration(ui = ExampleUI.class, productionMode = false) 54 | public static class ExampleUIServlet extends VaadinServlet { 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /javamail-example/src/main/java/com/example/javamail/JavaMailService.java: -------------------------------------------------------------------------------- 1 | package com.example.javamail; 2 | 3 | import javax.activation.DataHandler; 4 | import javax.mail.Message; 5 | import javax.mail.MessagingException; 6 | import javax.mail.Session; 7 | import javax.mail.Transport; 8 | import javax.mail.internet.MimeBodyPart; 9 | import javax.mail.internet.MimeMessage; 10 | import javax.mail.internet.MimeMultipart; 11 | import javax.mail.util.ByteArrayDataSource; 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.util.*; 15 | 16 | /** 17 | * @author alejandro@vaadin.com 18 | **/ 19 | public class JavaMailService { 20 | 21 | /** 22 | * Sends an email message with no attachments. 23 | * 24 | * @param from email address from which the message will be sent. 25 | * @param recipients the recipients of the message. 26 | * @param subject subject header field. 27 | * @param text content of the message. 28 | * @throws MessagingException 29 | * @throws IOException 30 | */ 31 | public static void send(String from, Collection recipients, String subject, String text) 32 | throws MessagingException, IOException { 33 | send(from, recipients, subject, text, null, null, null); 34 | } 35 | 36 | /** 37 | * Sends an email message to one recipient with one attachment. 38 | * 39 | * @param from email address from which the message will be sent. 40 | * @param recipient the recipients of the message. 41 | * @param subject subject header field. 42 | * @param text content of the message. 43 | * @param attachment attachment to be included with the message. 44 | * @param fileName file name of the attachment. 45 | * @param mimeType mime type of the attachment. 46 | * @throws MessagingException 47 | * @throws IOException 48 | */ 49 | public static void send(String from, String recipient, String subject, String text, InputStream attachment, 50 | String fileName, String mimeType) 51 | throws MessagingException, IOException { 52 | send(from, Arrays.asList(recipient), subject, text, Arrays.asList(attachment), Arrays.asList(fileName), 53 | Arrays.asList(mimeType)); 54 | } 55 | 56 | /** 57 | * Sends an email message with attachments. 58 | * 59 | * @param from email address from which the message will be sent. 60 | * @param recipients array of strings containing the recipients of the message. 61 | * @param subject subject header field. 62 | * @param text content of the message. 63 | * @param attachments attachments to be included with the message. 64 | * @param fileNames file names for each attachment. 65 | * @param mimeTypes mime types for each attachment. 66 | * @throws MessagingException 67 | * @throws IOException 68 | */ 69 | public static void send(String from, Collection recipients, String subject, String text, 70 | List attachments, List fileNames, List mimeTypes) 71 | throws MessagingException, IOException { 72 | // check for null references 73 | Objects.requireNonNull(from); 74 | Objects.requireNonNull(recipients); 75 | 76 | // a message with attachments consists of several parts in a multipart 77 | MimeMultipart multipart = new MimeMultipart(); 78 | 79 | // create text part 80 | MimeBodyPart textPart = new MimeBodyPart(); 81 | textPart.setText(text, "utf-8", "html"); 82 | 83 | // add the text part to the multipart 84 | multipart.addBodyPart(textPart); 85 | 86 | // create attachment parts if required 87 | if (attachments != null) { 88 | // check that attachment and fileNames arrays sizes match 89 | if (attachments.size() != fileNames.size() || attachments.size() != mimeTypes.size()) { 90 | throw new IllegalArgumentException( 91 | "Attachments, file names, and mime types array sizes must match"); 92 | } 93 | 94 | // create parts and add them to the multipart 95 | for (int i = 0; i < attachments.size(); i++) { 96 | // create a data source to wrap the attachment and its mime type 97 | ByteArrayDataSource dataSource = new ByteArrayDataSource(attachments.get(i), mimeTypes.get(i)); 98 | 99 | // create a dataHandler wrapping the data source 100 | DataHandler dataHandler = new DataHandler(dataSource); 101 | 102 | // create a body part for the attachment and set its data handler and file name 103 | MimeBodyPart attachmentPart = new MimeBodyPart(); 104 | attachmentPart.setDataHandler(dataHandler); 105 | attachmentPart.setFileName(fileNames.get(i)); 106 | 107 | // add the body part to the multipart 108 | multipart.addBodyPart(attachmentPart); 109 | } 110 | } 111 | 112 | // load email configuration from properties file 113 | Properties properties = new Properties(); 114 | properties.load(JavaMailService.class.getResourceAsStream("/mail.properties")); 115 | 116 | // create a Session instance specifying the system properties 117 | Session session = Session.getInstance(properties); 118 | 119 | // create a message instance associated to the session 120 | MimeMessage message = new MimeMessage(session); 121 | 122 | // set the multipart as content for the message 123 | message.setContent(multipart); 124 | 125 | // configure from address, add recipients, and set the subject of the message 126 | message.setFrom(from); 127 | message.addRecipients(Message.RecipientType.TO, String.join(",", recipients)); 128 | message.setSubject(subject); 129 | 130 | // send the message 131 | String username = properties.getProperty("mail.smtp.username"); 132 | String password = properties.getProperty("mail.smtp.password"); 133 | Transport.send(message, username, password); 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /javamail-example/src/main/resources/file.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alejandro-du/java-email-tutorial/e71768868e232aaab29730449ed3456be7f81ef1/javamail-example/src/main/resources/file.pdf -------------------------------------------------------------------------------- /javamail-example/src/main/resources/mail.properties: -------------------------------------------------------------------------------- 1 | # Test SMTP server: 2 | mail.smtp.host=localhost 3 | mail.smtp.port=9090 4 | 5 | # Gmail's SMTP server: 6 | # mail.smtp.host=smtp.gmail.com 7 | # mail.smtp.port=465 8 | # mail.smtp.ssl.enable=true 9 | # mail.smtp.username=your-account@gmail.com 10 | # mail.smtp.password=your-password 11 | -------------------------------------------------------------------------------- /spring-mail-example/README.md: -------------------------------------------------------------------------------- 1 | # Sending email using JavaMail 2 | 3 | 1. Checkout the project form Git, and build and run with Maven: 4 | 5 | ``` 6 | git clone https://github.com/alejandro-du/java-email-tutorial 7 | 8 | cd java-email-tutorial 9 | 10 | cd spring-mail-example 11 | 12 | mvn clean install 13 | 14 | mvn jetty:run 15 | ``` 16 | 17 | 2. Download and run [FakeSMTP](https://nilhcem.github.io/FakeSMTP). 18 | 19 | 3. Configure FakeSMTP listening port to `9090` and start the server. 20 | 21 | 4. Open [http://localhost:8080](http://localhost:8080) in your browser. 22 | -------------------------------------------------------------------------------- /spring-mail-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.test 7 | vaadin-app 8 | war 9 | 1.0-SNAPSHOT 10 | vaadin-app 11 | 12 | 13 | 3 14 | 15 | 16 | 17 | 8.0.5 18 | 8.0.5 19 | 9.3.9.v20160517 20 | UTF-8 21 | 1.8 22 | 1.8 23 | 24 | local 25 | 26 | 27 | 28 | 29 | vaadin-addons 30 | http://maven.vaadin.com/vaadin-addons 31 | 32 | 33 | 34 | 35 | 36 | 37 | com.vaadin 38 | vaadin-bom 39 | ${vaadin.version} 40 | pom 41 | import 42 | 43 | 44 | 45 | 46 | 47 | 48 | javax.servlet 49 | javax.servlet-api 50 | 3.0.1 51 | provided 52 | 53 | 54 | com.vaadin 55 | vaadin-server 56 | 57 | 58 | com.vaadin 59 | vaadin-push 60 | 61 | 62 | com.vaadin 63 | vaadin-client-compiled 64 | 65 | 66 | com.vaadin 67 | vaadin-themes 68 | 69 | 70 | 71 | 72 | javax.mail 73 | mail 74 | 1.5.0-b01 75 | 76 | 77 | 78 | 79 | org.springframework 80 | spring-context-support 81 | 4.2.4.RELEASE 82 | 83 | 84 | 85 | 86 | com.vaadin 87 | vaadin-spring 88 | 2.0.1 89 | 90 | 91 | 92 | 93 | 94 | 95 | org.apache.maven.plugins 96 | maven-war-plugin 97 | 3.0.0 98 | 99 | false 100 | 101 | WEB-INF/classes/VAADIN/widgetsets/WEB-INF/** 102 | 103 | 104 | 105 | com.vaadin 106 | vaadin-maven-plugin 107 | ${vaadin.plugin.version} 108 | 109 | 110 | 111 | update-theme 112 | update-widgetset 113 | compile 114 | 115 | compile-theme 116 | 117 | 118 | 119 | 120 | 121 | org.apache.maven.plugins 122 | maven-clean-plugin 123 | 3.0.0 124 | 125 | 126 | 127 | 128 | src/main/webapp/VAADIN/themes 129 | 130 | **/styles.css 131 | **/styles.scss.cache 132 | 133 | 134 | 135 | 136 | 137 | 138 | 140 | 141 | org.eclipse.jetty 142 | jetty-maven-plugin 143 | ${jetty.plugin.version} 144 | 145 | 2 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | vaadin-prerelease 155 | 156 | false 157 | 158 | 159 | 160 | 161 | vaadin-prereleases 162 | http://maven.vaadin.com/vaadin-prereleases 163 | 164 | 165 | vaadin-snapshots 166 | https://oss.sonatype.org/content/repositories/vaadin-snapshots/ 167 | 168 | false 169 | 170 | 171 | true 172 | 173 | 174 | 175 | 176 | 177 | vaadin-prereleases 178 | http://maven.vaadin.com/vaadin-prereleases 179 | 180 | 181 | vaadin-snapshots 182 | https://oss.sonatype.org/content/repositories/vaadin-snapshots/ 183 | 184 | false 185 | 186 | 187 | true 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /spring-mail-example/src/main/java/com/example/javamail/ExampleUI.java: -------------------------------------------------------------------------------- 1 | package com.example.javamail; 2 | 3 | import com.vaadin.server.VaadinRequest; 4 | import com.vaadin.spring.annotation.EnableVaadin; 5 | import com.vaadin.spring.annotation.SpringUI; 6 | import com.vaadin.spring.server.SpringVaadinServlet; 7 | import com.vaadin.ui.*; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.web.context.ContextLoaderListener; 11 | 12 | import javax.servlet.annotation.WebListener; 13 | import javax.servlet.annotation.WebServlet; 14 | import java.io.InputStream; 15 | 16 | /** 17 | * @author alejandro@vaadin.com 18 | **/ 19 | @SpringUI 20 | public class ExampleUI extends UI { 21 | 22 | @Autowired 23 | private SpringEmailService springEmailService; 24 | 25 | @Override 26 | protected void init(VaadinRequest vaadinRequest) { 27 | // the text field where users will specify their email address 28 | TextField textField = new TextField("Your email:"); 29 | 30 | // a button with a click listener that sends the email 31 | Button button = new Button("Send me the PDF", e -> sendEmail(textField.getValue())); 32 | 33 | // a layout containing the previous components 34 | VerticalLayout layout = new VerticalLayout(textField, button); 35 | setContent(layout); // sets the content for this UI 36 | } 37 | 38 | private void sendEmail(String to) { 39 | try { 40 | // all values as variables to clarify its usage 41 | InputStream inputStream = getClass().getResourceAsStream("/file.pdf"); 42 | String from = "sender@test.com"; 43 | String subject = "Your PDF"; 44 | String text = "Here there is your PDF file!"; 45 | String fileName = "file.pdf"; 46 | String mimeType = "application/pdf"; 47 | 48 | springEmailService.send(from, to, subject, text, inputStream, fileName, mimeType); 49 | 50 | inputStream.close(); 51 | 52 | Notification.show("Email sent"); 53 | 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | Notification.show("Error sending the email", Notification.Type.ERROR_MESSAGE); 57 | } 58 | } 59 | 60 | @WebServlet(value = "/*", asyncSupported = true) 61 | public static class Servlet extends SpringVaadinServlet { 62 | } 63 | 64 | @WebListener 65 | public static class MyContextLoaderListener extends ContextLoaderListener { 66 | } 67 | 68 | @Configuration 69 | @EnableVaadin 70 | public static class MyConfiguration { 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /spring-mail-example/src/main/java/com/example/javamail/SpringEmailService.java: -------------------------------------------------------------------------------- 1 | package com.example.javamail; 2 | 3 | import org.springframework.mail.javamail.JavaMailSenderImpl; 4 | import org.springframework.mail.javamail.MimeMessageHelper; 5 | import org.springframework.stereotype.Service; 6 | 7 | import javax.mail.MessagingException; 8 | import javax.mail.internet.MimeMessage; 9 | import javax.mail.util.ByteArrayDataSource; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.*; 13 | 14 | /** 15 | * @author alejandro@vaadin.com 16 | **/ 17 | @Service 18 | public class SpringEmailService { 19 | 20 | /** 21 | * Sends an email message with no attachments. 22 | * 23 | * @param from email address from which the message will be sent. 24 | * @param recipients the recipients of the message. 25 | * @param subject subject header field. 26 | * @param text content of the message. 27 | * @throws MessagingException 28 | * @throws IOException 29 | */ 30 | public static void send(String from, Collection recipients, String subject, String text) 31 | throws MessagingException, IOException { 32 | send(from, recipients, subject, text, null, null, null); 33 | } 34 | 35 | /** 36 | * Sends an email message to one recipient with one attachment. 37 | * 38 | * @param from email address from which the message will be sent. 39 | * @param recipient the recipients of the message. 40 | * @param subject subject header field. 41 | * @param text content of the message. 42 | * @param attachment attachment to be included with the message. 43 | * @param fileName file name of the attachment. 44 | * @param mimeType mime type of the attachment. 45 | * @throws MessagingException 46 | * @throws IOException 47 | */ 48 | public static void send(String from, String recipient, String subject, String text, 49 | InputStream attachment, String fileName, String mimeType) 50 | throws MessagingException, IOException { 51 | send(from, Arrays.asList(recipient), subject, text, Arrays.asList(attachment), Arrays.asList(fileName), 52 | Arrays.asList(mimeType)); 53 | } 54 | 55 | /** 56 | * Sends an email message with attachments. 57 | * 58 | * @param from email address from which the message will be sent. 59 | * @param recipients array of strings containing the recipients of the message. 60 | * @param subject subject header field. 61 | * @param text content of the message. 62 | * @param attachments attachments to be included with the message. 63 | * @param fileNames file names for each attachment. 64 | * @param mimeTypes mime types for each attachment. 65 | * @throws MessagingException 66 | * @throws IOException 67 | */ 68 | public static void send(String from, Collection recipients, String subject, String text, 69 | List attachments, List fileNames, List mimeTypes) 70 | throws MessagingException, IOException { 71 | 72 | // check for null references 73 | Objects.requireNonNull(from); 74 | Objects.requireNonNull(recipients); 75 | 76 | // load email configuration from properties file 77 | Properties properties = new Properties(); 78 | properties.load(SpringEmailService.class.getResourceAsStream("/mail.properties")); 79 | String username = properties.getProperty("mail.smtp.username"); 80 | String password = properties.getProperty("mail.smtp.password"); 81 | 82 | // configure the connection to the SMTP server 83 | JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); 84 | mailSender.setJavaMailProperties(properties); 85 | mailSender.setUsername(username); 86 | mailSender.setPassword(password); 87 | 88 | MimeMessage message = mailSender.createMimeMessage(); 89 | MimeMessageHelper helper = new MimeMessageHelper(message, true); 90 | helper.setFrom(from); 91 | helper.setSubject(subject); 92 | helper.setText(text, true); 93 | 94 | for (String recipient : recipients) { 95 | helper.addTo(recipient); 96 | } 97 | 98 | if (attachments != null) { 99 | for (int i = 0; i < attachments.size(); i++) { 100 | // create a data source to wrap the attachment and its mime type 101 | ByteArrayDataSource dataSource = new ByteArrayDataSource(attachments.get(i), mimeTypes.get(i)); 102 | 103 | // add the attachment 104 | helper.addAttachment(fileNames.get(i), dataSource); 105 | } 106 | } 107 | 108 | mailSender.send(message); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /spring-mail-example/src/main/resources/file.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alejandro-du/java-email-tutorial/e71768868e232aaab29730449ed3456be7f81ef1/spring-mail-example/src/main/resources/file.pdf -------------------------------------------------------------------------------- /spring-mail-example/src/main/resources/mail.properties: -------------------------------------------------------------------------------- 1 | # Test SMTP server: 2 | mail.smtp.host=localhost 3 | mail.smtp.port=9090 4 | 5 | # Gmail's SMTP server: 6 | # mail.smtp.host=smtp.gmail.com 7 | # mail.smtp.port=465 8 | # mail.smtp.ssl.enable=true 9 | # mail.smtp.username=your-account@gmail.com 10 | # mail.smtp.password=your-password 11 | -------------------------------------------------------------------------------- /spring-mail-example/src/main/webapp/WEB-INF/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | --------------------------------------------------------------------------------