/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mittpaket; import java.io.IOException; import java.util.Date; import java.util.Properties; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; /** * * @author h07jowan * * http://www.tutorialspoint.com/javamail_api/javamail_api_send_email_with_attachment.htm * http://www.rgagnon.com/javadetails/java-0504.html * */ public class SendAttachment { public void SendAttachment(String dir) throws MessagingException { // sets SMTP server properties Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.user", "dalastudent"); properties.put("mail.password", "enterpriseJava2014"); String userName = "dalastudent"; String password = "enterpriseJava2014"; String mailTo = "your-friend-email"; String subject = "h07jowan html-attachmentgrej"; String message = "

HTML

hej

"; String toAddress = "dalastudent@gmail.com"; // attachments String[] attachFiles = new String[1]; if(dir.equalsIgnoreCase("1")) { attachFiles[0] = "H:/h07jowan/Windesk/javaee/Labb4/1509726_10152407630312275_2010133601_n.jpg"; } else { attachFiles[0] = "H:/h07jowan/Windesk/javaee/Labb4/original.jpg"; } // creates a new session with an authenticator Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }; Session session = Session.getInstance(properties, auth); // creates a new e-mail message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); msg.setSentDate(new Date()); // creates message part MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(message, "text/html"); // creates multi-part Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // adds attachments if (attachFiles != null && attachFiles.length > 0) { for (String filePath : attachFiles) { MimeBodyPart attachPart = new MimeBodyPart(); try { attachPart.attachFile(filePath); } catch (IOException ex) { ex.printStackTrace(); } // messageBodyPart = new MimeBodyPart(); // DataSource fds = new FileDataSource(attachPart); // // multipart.addBodyPart(attachPart); // messageBodyPart.setDataHandler(); // messageBodyPart.setHeader("Content-ID",""); messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource (filePath); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID",""); multipart.addBodyPart(messageBodyPart); } } // sets the multi-part as e-mail's content msg.setContent(multipart); // sends the e-mail Transport.send(msg); }}