protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException { if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) { return; } MultiPartEmail mpEmail = (MultiPartEmail) email; for (File file : files) { mpEmail.attach(file); } for (DataSource ds : dataSources) { if (ds != null) { mpEmail.attach(ds, ds.getName(), null); } } }
/** * @param subject 主题 * @param body 内容 * @param attachment 附件 * @param recipients 收件人 */ public static MultiPartEmail getMultiPartEmail(String subject, String body, EmailAttachment attachment, String... recipients) { try { MultiPartEmail multiPartEmail = new MultiPartEmail(); configEmail(subject, multiPartEmail, recipients); multiPartEmail.setMsg(body); // add the attachment if (attachment != null) multiPartEmail.attach(attachment); return multiPartEmail; } catch (EmailException e) { throw new MailException("Unabled to send email", e); } }
/** * Attach a file located by its URL. The disposition of the file * is set to mixed. * * @param url The URL of the file (may be any valid URL). * @param name The name field for the attachment. * @param description A description for the attachment. * @return A MultiPartEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for definitions * @since 1.0 */ public MultiPartEmail attach(final URL url, final String name, final String description) throws EmailException { return attach(url, name, description, EmailAttachment.ATTACHMENT); }
mm.attach(bds,file.getName(), "", EmailAttachment.ATTACHMENT); } catch (MessagingException e) { e.printStackTrace();
/** * Attach a file. * * @param file A file attachment * @return A MultiPartEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for definitions * @since 1.3 */ public MultiPartEmail attach(final File file) throws EmailException { final String fileName = file.getAbsolutePath(); try { if (!file.exists()) { throw new IOException("\"" + fileName + "\" does not exist"); } final FileDataSource fds = new FileDataSource(file); return attach(fds, file.getName(), null, EmailAttachment.ATTACHMENT); } catch (final IOException e) { throw new EmailException("Cannot attach file \"" + fileName + "\"", e); } }
/** * 发送多媒体邮件,可以携带附件信息 * @param urls 附件的URL,key为附件的名称 */ public void sendMultipartEmail(String targetAddress,String title,String content,Map<String,URL> urls) throws Exception { MultiPartEmail email = new MultiPartEmail(); for(Map.Entry<String,URL> entry : urls.entrySet()) { email.attach(entry.getValue(),entry.getKey(),EmailAttachment.ATTACHMENT); } email.setSubject(title); email.addTo(targetAddress); email.setMsg(content); sendEmail(email); }
/** * Attach a file located by its URL. The disposition of the file * is set to mixed. * * @param url The URL of the file (may be any valid URL). * @param name The name field for the attachment. * @param description A description for the attachment. * @return A MultiPartEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for definitions * @since 1.0 */ public MultiPartEmail attach(final URL url, final String name, final String description) throws EmailException { return attach(url, name, description, EmailAttachment.ATTACHMENT); }
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException { if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) { return; } MultiPartEmail mpEmail = (MultiPartEmail) email; for (File file : files) { mpEmail.attach(file); } for (DataSource ds : dataSources) { if (ds != null) { mpEmail.attach(ds, ds.getName(), null); } } }
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException { if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) { return; } MultiPartEmail mpEmail = (MultiPartEmail) email; for (File file : files) { mpEmail.attach(file); } for (DataSource ds : dataSources) { if (ds != null) { mpEmail.attach(ds, ds.getName(), null); } } }
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException { if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) { return; } MultiPartEmail mpEmail = (MultiPartEmail) email; for (File file : files) { mpEmail.attach(file); } for (DataSource ds : dataSources) { if (ds != null) { mpEmail.attach(ds, ds.getName(), null); } } }
/** * Attach a file. * * @param file A file attachment * @return A MultiPartEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for definitions * @since 1.3 */ public MultiPartEmail attach(final File file) throws EmailException { final String fileName = file.getAbsolutePath(); try { if (!file.exists()) { throw new IOException("\"" + fileName + "\" does not exist"); } final FileDataSource fds = new FileDataSource(file); return attach(fds, file.getName(), null, EmailAttachment.ATTACHMENT); } catch (final IOException e) { throw new EmailException("Cannot attach file \"" + fileName + "\"", e); } }
public void sendWebAttachEmail(String toEmail, String toName, String subject, String msg, List attachments) throws Exception { try { MultiPartEmail email = new MultiPartEmail(); this.setEmail(email, toEmail, toName, subject, msg, null, null); for (int i = 0; i < attachments.size(); i++) { String attachPath = (String) attachments.get(i); String attachName = attachPath.substring(attachPath .lastIndexOf("/"), attachPath.length()); EmailAttachment attachment = new EmailAttachment(); attachment.setURL(new URL(attachPath)); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(attachName); attachment.setName(attachName); email.attach(attachment); } email.send(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } }
public void sendAttachEmail(String toEmail, String toName, String subject, String msg, List attachments) throws Exception { try { MultiPartEmail email = new MultiPartEmail(); this.setEmail(email, toEmail, toName, subject, msg, null, null); for (int i = 0; i < attachments.size(); i++) { String attachPath = (String) attachments.get(i); String attachName = attachPath.substring(attachPath .lastIndexOf("/"), attachPath.length()); EmailAttachment attachment = new EmailAttachment(); attachment.setPath(attachPath); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(attachName); attachment.setName(attachName); email.attach(attachment); } email.send(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } }
public void sendWebAttachEmails(Map toEmails, String subject, String msg, List attachments) throws Exception { try { MultiPartEmail email = new MultiPartEmail(); this.setEmail(email, toEmails, subject, msg); for (int i = 0; i < attachments.size(); i++) { String attachPath = (String) attachments.get(i); String attachName = attachPath.substring(attachPath .lastIndexOf("/"), attachPath.length()); EmailAttachment attachment = new EmailAttachment(); attachment.setURL(new URL(attachPath)); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(attachName); attachment.setName(attachName); email.attach(attachment); } email.send(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } }
// Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath(zipFile); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Zip File"); attachment.setName("myfiles.zip"); MultiPartEmail email = new MultiPartEmail(); email.attach(attachment);
/** * A sanity check that a simple email also works in reality. * * @throws Exception the test failed */ @Test public void testMultiPartEmail() throws Exception { final MultiPartEmail email = (MultiPartEmail) create(MultiPartEmail.class); email.setSubject("TestMultiPartMail"); email.setMsg("This is a test mail ... :-)"); email.attach(new File("./src/test/resources/attachments/logo.pdf")); EmailUtils.writeMimeMessage( new File("./target/test-emails/multipart.eml"), send(email).getMimeMessage()); }
/** * This test checks the correct character encoding when sending * non-ASCII content using SimpleEmail. * * https://issues.apache.org/jira/browse/EMAIL-79 * * @throws Exception the test failed */ @Test public void testCorrectCharacterEncoding() throws Exception { // U+03B1 : GREEK SMALL LETTER ALPHA // U+03B2 : GREEK SMALL LETTER BETA // U+03B3 : GREEK SMALL LETTER GAMMA final String subject = "[email] 5.Test: Subject with three greek UTF-8 characters : \u03B1\u03B2\u03B3"; final String textMsg = "My test body with with three greek UTF-8 characters : \u03B1\u03B2\u03B3\n"; final String attachmentName = "\u03B1\u03B2\u03B3.txt"; // make sure to set the charset before adding the message content final MultiPartEmail email = (MultiPartEmail) create(MultiPartEmail.class); email.setSubject(subject); email.setMsg(textMsg); // create a proper UTF-8 sequence for the text attachment (matching our default charset) final DataSource attachment = new javax.mail.util.ByteArrayDataSource(textMsg.getBytes("utf-8"), "text/plain"); email.attach(attachment, attachmentName, "Attachment in Greek"); EmailUtils.writeMimeMessage( new File("./target/test-emails/correct-encoding.eml"), send(email).getMimeMessage()); }
public static void main(String[] args) throws EmailException { // 创建附件 EmailAttachment attachment = new EmailAttachment(); attachment.setPath("mypictures/john.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Picture of John"); attachment.setName("John"); // 创建 Email Message MultiPartEmail email = new MultiPartEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("The picture"); email.setMsg("Here is the picture you wanted"); // 追加附件 email.attach(attachment); // 发送 email.send(); } }