Properties props = new Properties(); props.put("mail.smtp.host", smtp); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(address,passwd); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(address)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); message.setSubject(subject); // message.setContent(content, "text/html;charset=utf-8"); // Create Mimemultipart to add content (can contain multiple attachments) MimeMultipart multipart = new MimeMultipart(); // MimeBodyPart (for letter content/attachment) BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(content.toString(), "text/html;charset=utf-8" ); // Add to MimeMultipart object multipart.addBodyPart(bodyPart); FileDataSource fds = new FileDataSource(new File(tTempPath)); BodyPart fileBodyPart = new MimeBodyPart(); // Load file in character stream fileBodyPart.setDataHandler( new DataHandler(fds)); // Set attachment file name String fname = new File(tTempPath).getName(); fname = new String(fname.getBytes("UTF-8"), "ISO-8859-1"); fileBodyPart.setFileName(fname); multipart.addBodyPart(fileBodyPart); message.setContent(multipart); Transport.send(message); return true; } catch (MessagingException e) { System.out.println(e.getMessage()); return false; }
Properties props = new Properties(); props.put("mail.smtp.host", smtp); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(address,passwd); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(address)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); message.setSubject(subject);// message.setContent(content, "text/html;charset=utf-8");
//Create Mimemultipart to add content (can contain multiple attachments) MimeMultipart multipart = new MimeMultipart(); //MimeBodyPart (for letter content/attachment) BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(content.toString(), "text/html;charset=utf-8"); //Add to MimeMultipart object multipart.addBodyPart(bodyPart); FileDataSource fds = new FileDataSource(new File(tTempPath)); BodyPart fileBodyPart = new MimeBodyPart(); // Load the file in the form of a character stream fileBodyPart.setDataHandler(new DataHandler(fds)); // Set the attachment file name String fname = new File(tTempPath).getName(); fname = new String(fname.getBytes("UTF-8" ), "ISO-8859-1"); fileBodyPart.setFileName(fname); multipart.addBodyPart(fileBodyPart); message.setContent(multipart); Transport.send(message); return true;
} catch (MessagingException e) { System.out.println(e.getMessage()); return false; }