List of usage examples for org.springframework.core.io FileSystemResource getFilename
@Override
public String getFilename()
From source file:ca.airspeed.demo.testingemail.EmailService.java
public void sendWithAttachments(InternetAddress to, String subject, String textBody, List<FileSystemResource> fileList) throws MessagingException { notNull(mailSender, String.format("Check your configuration, I need an instance of %s.", JavaMailSender.class.getCanonicalName())); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to);/* w w w. j av a 2 s .c o m*/ helper.setFrom(senderAddress); helper.setSubject(subject); helper.setText(textBody); for (FileSystemResource resource : fileList) { helper.addAttachment(resource.getFilename(), resource.getFile()); } mailSender.send(message); }
From source file:org.broadleafcommerce.admin.util.controllers.FileUploadController.java
private void backupExistingFile(FileSystemResource fileResource, String basepath) { if (fileResource.exists()) { String originalFilename = fileResource.getFilename(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); int dotIndex = originalFilename.lastIndexOf("."); String extension = originalFilename.substring(dotIndex, originalFilename.length()); String filename = originalFilename.substring(0, dotIndex); String dateString = dateFormat.format(new Date()); String backupFilename = filename + dateString + extension; fileResource.getFile().renameTo(new File(basepath + File.separator + backupFilename)); }//from www. j ava2 s .c om }
From source file:gov.nih.nci.cabig.caaers.tools.mail.CaaersJavaMailSender.java
/** * This method is used to send an email/* w ww.j av a 2 s .c o m*/ */ public void sendMail(String[] to, String[] cc, String subject, String content, String[] attachmentFilePaths) { if (to == null || to.length == 0 || to[0] == null) { return; } try { MimeMessage message = createMimeMessage(); message.setSubject(subject); // use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); if (cc != null && cc.length > 0) { helper.setCc(cc); } helper.setText(content); for (String attachmentPath : attachmentFilePaths) { if (StringUtils.isNotEmpty(attachmentPath)) { File f = new File(attachmentPath); FileSystemResource file = new FileSystemResource(f); helper.addAttachment(file.getFilename(), file); } } send(message); } catch (Exception e) { if (SUPRESS_MAIL_SEND_EXCEPTION) return; //supress the excetion related to email sending throw new CaaersSystemException("Error while sending email to " + to[0], e); } }
From source file:cz.zcu.kiv.eeg.mobile.base.ui.scenario.ScenarioAddActivity.java
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { //obtaining file selected in FileChooserActivity case (Values.SELECT_FILE_FLAG): { if (resultCode == Activity.RESULT_OK) { selectedFile = data.getExtras().getString(Values.FILE_PATH); selectedFileLength = data.getExtras().getLong(Values.FILE_LENGTH); TextView selectedFileView = (TextView) findViewById(R.id.fchooserSelectedFile); EditText mimeView = (EditText) findViewById(R.id.scenario_mime_value); TextView fileSizeView = (TextView) findViewById(R.id.scenario_file_size_value); FileSystemResource file = new FileSystemResource(selectedFile); selectedFileView.setText(file.getFilename()); mimeView.setText(FileUtils.getMimeType(selectedFile)); String fileSize = FileUtils.getFileSize(file.getFile().length()); fileSizeView.setText(fileSize); Toast.makeText(this, selectedFile, Toast.LENGTH_SHORT).show(); }//www . j a v a 2 s . c o m break; } } }
From source file:gov.nih.nci.cabig.caaers.esb.client.MessageNotificationService.java
public void sendMail(String[] to, String subject, String content, String attachment) throws Exception { MimeMessage message = caaersJavaMailSender.createMimeMessage(); message.setSubject(subject);//from w w w .j a v a 2 s . co m message.setFrom(new InternetAddress(configuration.get(Configuration.SYSTEM_FROM_EMAIL))); // use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setText(content); if (attachment != null) { File f = new File(attachment); FileSystemResource file = new FileSystemResource(f); helper.addAttachment(file.getFilename(), file); } caaersJavaMailSender.send(message); }
From source file:uk.ac.gda.dls.client.feedback.FeedbackDialog.java
@Override protected void createButtonsForButtonBar(Composite parent) { GridData data = new GridData(SWT.FILL, SWT.FILL, false, true, 4, 1); GridLayout layout = new GridLayout(5, false); parent.setLayoutData(data);//from w ww. j a va 2s . c o m parent.setLayout(layout); Button attachButton = new Button(parent, SWT.TOGGLE); attachButton.setText("Attach File(s)"); attachButton.setToolTipText("Add files to feedback"); final Button screenGrabButton = new Button(parent, SWT.CHECK); screenGrabButton.setText("Include Screenshot"); screenGrabButton.setToolTipText("Send screenshot with feedback"); Label space = new Label(parent, SWT.NONE); space.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true)); Button sendButton = new Button(parent, SWT.PUSH); sendButton.setText("Send"); Button cancelButton = new Button(parent, SWT.PUSH); cancelButton.setText("Cancel"); sendButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { final String name = nameText.getText(); final String email = emailText.getText(); final String subject = subjectText.getText(); final String description = descriptionText.getText(); fileList = attachedFileList.getItems(); Job job = new Job("Send feedback email") { @Override protected IStatus run(IProgressMonitor monitor) { try { final String recipientsProperty = LocalProperties.get("gda.feedback.recipients", "dag-group@diamond.ac.uk"); final String[] recipients = recipientsProperty.split(" "); for (int i = 0; i < recipients.length; i++) { recipients[i] = recipients[i].trim(); } final String from = String.format("%s <%s>", name, email); final String beamlineName = LocalProperties.get("gda.beamline.name", "Beamline Unknown"); final String mailSubject = String.format("[GDA feedback - %s] %s", beamlineName.toUpperCase(), subject); final String smtpHost = LocalProperties.get("gda.feedback.smtp.host", "localhost"); JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost(smtpHost); MimeMessage message = mailSender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper(message, (FeedbackDialog.this.hasFiles && fileList.length > 0) || FeedbackDialog.this.screenshot); helper.setFrom(from); helper.setTo(recipients); helper.setSubject(mailSubject); helper.setText(description); if (FeedbackDialog.this.screenshot) { PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { @Override public void run() { String fileName = "/tmp/feedbackScreenshot.png"; try { captureScreen(fileName); FileSystemResource file = new FileSystemResource(new File(fileName)); helper.addAttachment(file.getFilename(), file); } catch (Exception e) { logger.error("Could not attach screenshot to feedback", e); } } }); } if (FeedbackDialog.this.hasFiles) { for (String fileName : fileList) { FileSystemResource file = new FileSystemResource(new File(fileName)); helper.addAttachment(file.getFilename(), file); } } {//required to workaround class loader issue with "no object DCH..." error MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); mc.addMailcap( "text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); mc.addMailcap( "multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); CommandMap.setDefaultCommandMap(mc); } mailSender.send(message); return Status.OK_STATUS; } catch (Exception ex) { logger.error("Could not send feedback", ex); return new Status(IStatus.ERROR, Activator.PLUGIN_ID, 1, "Error sending email", ex); } } }; job.schedule(); setReturnCode(OK); close(); } }); cancelButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { setReturnCode(CANCEL); close(); } }); attachButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { hasFiles = !hasFiles; GridData data = ((GridData) attachments.getLayoutData()); data.exclude = !hasFiles; attachments.setVisible(hasFiles); topParent.layout(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); screenGrabButton.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { screenshot = ((Button) e.widget).getSelection(); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); }