SHOW:
|
|
- or go back to the newest paste.
| 1 | package com.z0ltan.mail.exchange; | |
| 2 | ||
| 3 | import java.net.HttpURLConnection; | |
| 4 | import java.net.MalformedURLException; | |
| 5 | import java.net.URL; | |
| 6 | import java.util.Properties; | |
| 7 | ||
| 8 | import javax.activation.DataHandler; | |
| 9 | import javax.activation.DataSource; | |
| 10 | import javax.activation.URLDataSource; | |
| 11 | import javax.mail.Address; | |
| 12 | import javax.mail.BodyPart; | |
| 13 | import javax.mail.Message; | |
| 14 | import javax.mail.MessagingException; | |
| 15 | import javax.mail.Multipart; | |
| 16 | import javax.mail.Session; | |
| 17 | import javax.mail.Transport; | |
| 18 | import javax.mail.internet.AddressException; | |
| 19 | import javax.mail.internet.InternetAddress; | |
| 20 | import javax.mail.internet.MimeBodyPart; | |
| 21 | import javax.mail.internet.MimeMessage; | |
| 22 | import javax.mail.internet.MimeMultipart; | |
| 23 | ||
| 24 | public class SendEmail {
| |
| 25 | private static final int MAX_SMTP_SIZE = 5; // MB | |
| 26 | ||
| 27 | public static void main(String[] args) {
| |
| 28 | - | String fromAddress = "[email protected]; |
| 28 | + | String fromAddress = "[email protected]"; |
| 29 | String smtpAddress = "10.0.45.1"; | |
| 30 | int smtpPort = 25; | |
| 31 | ||
| 32 | String attachmentLink | |
| 33 | = "http://localhost:9999/examples/servlets/ultimate.pdf"; | |
| 34 | String[] toAddress = { "[email protected]", "[email protected]" };
| |
| 35 | String subject = "Report file and link"; | |
| 36 | ||
| 37 | // First create the Properties object and use it to obtain the Session | |
| 38 | // object | |
| 39 | Properties props = new Properties(); | |
| 40 | props.put("mail.smtp.host", smtpAddress);
| |
| 41 | props.put("mail.smtp.port", smtpPort);
| |
| 42 | // props.put("mail.smtp.auth", true);
| |
| 43 | ||
| 44 | Session session = Session.getInstance(props, null); | |
| 45 | session.setDebug(true); | |
| 46 | ||
| 47 | // Create the Message | |
| 48 | final MimeMessage reportMessage = new MimeMessage(session); | |
| 49 | if (subject != null) {
| |
| 50 | try {
| |
| 51 | reportMessage.setSubject(subject, "UTF-8"); | |
| 52 | } catch (MessagingException e) {
| |
| 53 | e.printStackTrace(); | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | // Create the addresses and add to message body | |
| 58 | try {
| |
| 59 | Address from = new InternetAddress(fromAddress); | |
| 60 | reportMessage.setFrom(from); | |
| 61 | ||
| 62 | Address[] to = new InternetAddress[toAddress.length]; | |
| 63 | ||
| 64 | for (int i = 0; i < toAddress.length; i++) {
| |
| 65 | to[i] = new InternetAddress(toAddress[i]); | |
| 66 | } | |
| 67 | ||
| 68 | reportMessage.addRecipients(Message.RecipientType.TO, to); | |
| 69 | ||
| 70 | // Add the attachment and the attachment link to the e-mail body | |
| 71 | BodyPart messageBodyPart = new MimeBodyPart(); | |
| 72 | BodyPart attachBodyPart = null; | |
| 73 | ||
| 74 | Multipart multipart = new MimeMultipart(); | |
| 75 | ||
| 76 | if (isAttachmentSmallerThanMaxSmtpSize(attachmentLink)) {
| |
| 77 | attachBodyPart = new MimeBodyPart(); | |
| 78 | DataSource source | |
| 79 | = new URLDataSource(new URL(attachmentLink)); | |
| 80 | attachBodyPart.setDataHandler(new DataHandler(source)); | |
| 81 | String file = attachmentLink.substring(attachmentLink | |
| 82 | .lastIndexOf('/'));
| |
| 83 | attachBodyPart.setFileName(file); | |
| 84 | multipart.addBodyPart(attachBodyPart); | |
| 85 | messageBodyPart.setContent(attachmentLink, | |
| 86 | "text/plain;charset=UTF-8"); | |
| 87 | } else {
| |
| 88 | String messageAndAttachment = "Note: The report file: " | |
| 89 | + attachmentLink.substring(attachmentLink | |
| 90 | .lastIndexOf('/') + 1)
| |
| 91 | + " was not attached since it exceeds the user " | |
| 92 | + "configured Maximum SMTP Attachment size." | |
| 93 | + "\n\n" + attachmentLink; | |
| 94 | messageBodyPart.setContent(messageAndAttachment, | |
| 95 | "text/plain;charset=UTF-8"); | |
| 96 | } | |
| 97 | ||
| 98 | multipart.addBodyPart(messageBodyPart); | |
| 99 | ||
| 100 | reportMessage.setContent(multipart); | |
| 101 | reportMessage.saveChanges(); | |
| 102 | ||
| 103 | // Send the e-mail | |
| 104 | (new Thread() {
| |
| 105 | public void run() {
| |
| 106 | try {
| |
| 107 | Transport.send(reportMessage); | |
| 108 | } catch (MessagingException e) {
| |
| 109 | e.printStackTrace(); | |
| 110 | } | |
| 111 | } | |
| 112 | }).start(); | |
| 113 | ||
| 114 | } catch (AddressException e) {
| |
| 115 | e.printStackTrace(); | |
| 116 | } catch (MessagingException e) {
| |
| 117 | e.printStackTrace(); | |
| 118 | } catch (MalformedURLException e) {
| |
| 119 | e.printStackTrace(); | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | private static boolean isAttachmentSmallerThanMaxSmtpSize( | |
| 124 | String attachmentLink) {
| |
| 125 | boolean status = false; | |
| 126 | ||
| 127 | try {
| |
| 128 | HttpURLConnection connection = (HttpURLConnection) new URL( | |
| 129 | attachmentLink).openConnection(); | |
| 130 | int sizeInMB = connection.getContentLength() / (1024 * 1024); | |
| 131 | ||
| 132 | if (sizeInMB > MAX_SMTP_SIZE) {
| |
| 133 | status = false; | |
| 134 | } else {
| |
| 135 | status = true; | |
| 136 | } | |
| 137 | ||
| 138 | } catch (Exception ex) {
| |
| 139 | status = false; | |
| 140 | } | |
| 141 | ||
| 142 | return status; | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | ||
| 147 |