当前位置: 首页>>代码示例>>Java>>正文


Java MimeMessageHelper.addAttachment方法代码示例

本文整理汇总了Java中org.springframework.mail.javamail.MimeMessageHelper.addAttachment方法的典型用法代码示例。如果您正苦于以下问题:Java MimeMessageHelper.addAttachment方法的具体用法?Java MimeMessageHelper.addAttachment怎么用?Java MimeMessageHelper.addAttachment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.mail.javamail.MimeMessageHelper的用法示例。


在下文中一共展示了MimeMessageHelper.addAttachment方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendWithAttachment

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@Override
public void sendWithAttachment(String to, String subject, String templateName, Map<String, String> data,
    InputStream attachment, String attachmentName) {

    LOG.debug("Sending email from {} to {} with subject {}", from, to, subject);

    MimeMessagePreparator messagePreparator = message -> {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(templateService.createTemplate(templateName, data));
        helper.addAttachment(attachmentName, new ByteArrayResource(IOUtils.toByteArray(attachment)));
    };
    mailSender.send(messagePreparator);

    IOUtils.closeQuietly(attachment);
}
 
开发者ID:Contargo,项目名称:iris,代码行数:19,代码来源:SpringEmailService.java

示例2: sendEmail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@Override
public void sendEmail(@NonNull final Mail _mail,
                      @NonNull final String _body,
                      @Nullable final String _attachmentFilename,
                      @Nullable final byte[] _pdfAttachment) {
    try {
        // Prepare message using a Spring helper
        final MimeMessage mimeMessage = mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, (_pdfAttachment != null), StandardCharsets.UTF_8.name());
        message.setSubject("Tifoon Scan Report");
        message.setFrom(_mail.getSender());
        message.setTo(_mail.getRecipient());

        message.setText(_body, true);

        if (_attachmentFilename != null && _pdfAttachment != null) {
            message.addAttachment(_attachmentFilename, new ByteArrayResource(_pdfAttachment));
        }

        // Send mail
        mailSender.send(mimeMessage);
    } catch (MessagingException _e) {
        log.error("Failed to send e-mail", _e);
    }
}
 
开发者ID:jonfryd,项目名称:tifoon,代码行数:26,代码来源:ReportEmailSenderServiceImpl.java

示例3: sendEmail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
/**
 * To send email.
 * 
 * @param mailParam its map containing mail headers details. i.e. "from"
 */  
public void sendEmail(EmailDTO emailDTO) {
	try { 
		if (Objects.nonNull(emailDTO)) {
			MimeMessage  mail = javaMailSenderImpl.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mail, true);
			helper.setSubject(emailDTO.getSubject());
			helper.setText(emailDTO.getMessage(), true);
			helper.setFrom(emailDTO.getFrom(), configurationService.getMailConfiguration().getFrom());
			helper.setTo(emailDTO.getTo());
			if (Objects.nonNull(emailDTO.getAttachment())) {
				helper.addAttachment(emailDTO.getAttachmentName(), emailDTO.getAttachment());
			}
			javaMailSenderImpl.send(mail);
		}
	} catch (MessagingException | MailException | UnsupportedEncodingException e) {
		logger.error("Exception while sending mail notification.", e);
	}
}
 
开发者ID:Zymr,项目名称:visitormanagement,代码行数:24,代码来源:EmailService.java

示例4: sendAttachmentsMail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
/**
 * 发送带附件的邮件
 */
@Async("mailAsync")
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
	MimeMessage message = mailSender.createMimeMessage();
	try {
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		helper.setSentDate(new Date());
		FileSystemResource file = new FileSystemResource(new File(filePath));
		String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
		helper.addAttachment(fileName, file);
		mailSender.send(message);
		logger.info("带附件的邮件已经发送。");
	} catch (MessagingException e) {
		logger.error("发送带附件的邮件时发生异常!", e);
	}
}
 
开发者ID:CharleyXu,项目名称:tulingchat,代码行数:23,代码来源:MailUtil.java

示例5: send

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@Override
public void send(NotificationType type, Recipient recipient, String attachment) throws MessagingException, IOException {

	final String subject = env.getProperty(type.getSubject());
	final String text = MessageFormat.format(env.getProperty(type.getText()), recipient.getAccountName());

	MimeMessage message = mailSender.createMimeMessage();

	MimeMessageHelper helper = new MimeMessageHelper(message, true);
	helper.setTo(recipient.getEmail());
	helper.setSubject(subject);
	helper.setText(text);

	if (StringUtils.hasLength(attachment)) {
		helper.addAttachment(env.getProperty(type.getAttachment()), new ByteArrayResource(attachment.getBytes()));
	}

	mailSender.send(message);

	log.info("{} email notification has been send to {}", type, recipient.getEmail());
}
 
开发者ID:sniperqpc,项目名称:Spring-cloud-gather,代码行数:22,代码来源:EmailServiceImpl.java

示例6: sendAttachmentsMail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
/**
 * 发送带附件的邮件
 */
@Test
public void sendAttachmentsMail() {
    MimeMessage message = null;
    try {
        message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(Sender);
        helper.setTo(Sender);
        helper.setSubject("主题:带附件的邮件");
        helper.setText("带附件的邮件内容");
        //注意项目路径问题,自动补用项目路径
        FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
        //加入邮件
        helper.addAttachment("图片.jpg", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mailSender.send(message);
}
 
开发者ID:zhaoshuxue,项目名称:springBoot,代码行数:23,代码来源:UnitTest.java

示例7: sendMailWithAttachment

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
/**
 * 发送附件
 *
 * @param to
 * @param subject
 * @throws javax.mail.MessagingException
 */
public void sendMailWithAttachment(String from ,String[] to, String subject) throws MessagingException {

    MimeMessage message = mailSender.createMimeMessage();
    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.toString());
    helper.addAttachment("show_name.jpg", new FileSystemResource(new File("d:/email.jpg")));

    /**
     *邮件内容
     */
    helper.setFrom(from);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText("my text", true);


    mailSender.send(message);
}
 
开发者ID:h819,项目名称:spring-boot,代码行数:26,代码来源:SpringMailService.java

示例8: sendBugReport

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
public void sendBugReport(BugReport report) throws MailException, MessagingException {
    MimeMessage message = this.mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(bugReportEmail);
    helper.setFrom(from);
    helper.setSubject(report.getSubject());
    helper.setText(report.getDescription());
    if (report.getAttachments() != null) {
        for (BugReportAttachment attachment : report.getAttachments()) {
            // Decode base64 encoded data
            byte[] data =  Base64.getDecoder().decode(attachment.getData());
            ByteArrayDataSource dataSource = new ByteArrayDataSource(data, attachment.getMimetype());
            helper.addAttachment(attachment.getName(), dataSource);
        }
    }
    this.mailSender.send(message);
}
 
开发者ID:MaritimeConnectivityPlatform,项目名称:IdentityRegistry,代码行数:18,代码来源:EmailUtil.java

示例9: sendAttachmentsMail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@Test
public void sendAttachmentsMail() throws Exception {

    MimeMessage mimeMessage = mailSender.createMimeMessage();

    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setFrom("[email protected]");
    helper.setTo("[email protected]");
    helper.setSubject("主题:有附件");
    helper.setText("有附件的邮件");

    FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
    helper.addAttachment("附件-1.jpg", file);
    helper.addAttachment("附件-2.jpg", file);

    mailSender.send(mimeMessage);
}
 
开发者ID:xiaomoinfo,项目名称:SpringBootUnity,代码行数:18,代码来源:MailTests.java

示例10: attachments

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@RequestMapping(value = "attachments", method = RequestMethod.POST, produces = { "application/xml", "application/json" })
public ResponseEntity<Email> attachments(@RequestBody Email email) throws Exception {

	MimeMessage mimeMessage = javaMailSender.createMimeMessage();

	MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
	mimeMessageHelper.setFrom(email.getFrom());
	mimeMessageHelper.setTo(email.getTo());
	mimeMessageHelper.setSubject(email.getSubject());
	mimeMessageHelper.setText("<html><body><img src=\"cid:banner\" >" + email.getText() + "</body></html>", true);

	FileSystemResource file = new FileSystemResource(new File("banner.jpg"));
	mimeMessageHelper.addInline("banner", file);

	FileSystemResource fileSystemResource = new FileSystemResource(new File("Attachment.jpg"));
	mimeMessageHelper.addAttachment("Attachment.jpg", fileSystemResource);

	javaMailSender.send(mimeMessage);
	email.setStatus(true);

	return new ResponseEntity<Email>(email, HttpStatus.OK);
}
 
开发者ID:oscm,项目名称:web,代码行数:23,代码来源:EmailRestController.java

示例11: forwardLobbycloudEmail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@Async
public void forwardLobbycloudEmail(String to, String subject, ArrayList<EmailAttachment> attachments ) {

	MimeMessage mimeMessage = javaMailSender.createMimeMessage();
	try {
		MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, CharEncoding.UTF_8);
		message.setTo(to);
		message.setFrom(jHipsterProperties.getMail().getFrom());
		message.setSubject(subject);
		for (EmailAttachment emailAttachment : attachments) {
			message.addAttachment(emailAttachment.getEncodedName(), emailAttachment.getDataSource());
		}
		message.setText("see attachments", false);
		javaMailSender.send(mimeMessage);
		log.debug("Forwarded e-mail to '{}'", to);
	} catch (Exception e) {
		log.warn("E-mail could not be sent to user '{}', exception is: {}", to, e.getMessage());
		log.error(javaMailSender.getJavaMailProperties().toString());
	}
}
 
开发者ID:TransparencyInternationalEU,项目名称:lobbycal,代码行数:21,代码来源:MailService.java

示例12: sendMessage

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
/**
    * Convenience method for sending messages with attachments.
    * 
    * @param recipients
    *                array of e-mail addresses
    * @param sender
    *                e-mail address of sender
    * @param resource
    *                attachment from classpath
    * @param bodyText
    *                text in e-mail
    * @param subject
    *                subject of e-mail
    * @param attachmentName
    *                name for attachment
    * @throws MessagingException
    *                 thrown when can't communicate with SMTP server
    */
   public void sendMessage(String[] recipients, String sender,
    ClassPathResource resource, String bodyText, String subject,
    String attachmentName) throws MessagingException {
MimeMessage message = ((JavaMailSenderImpl) mailSender)
	.createMimeMessage();

// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setTo(recipients);
helper.setFrom(sender);
helper.setText(bodyText);
helper.setSubject(subject);

helper.addAttachment(attachmentName, resource);

((JavaMailSenderImpl) mailSender).send(message);
   }
 
开发者ID:gisgraphy,项目名称:gisgraphy,代码行数:37,代码来源:MailEngine.java

示例13: sendEmail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
@Async
 public void sendEmail(String to, String from, String subject, String content, boolean isMultipart, boolean isHtml, Map<String, File> files) throws Exception {
 	System.out.println("MailService.sendEmail()");
     MimeMessage mimeMessage = javaMailSender.createMimeMessage();
     try {
         MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
         message.setTo(to);
         message.setFrom(from);
         message.setSubject(subject);
         message.setText(content, isHtml);
         
         if (files != null && files.size() > 0) {
         	for (String key : files.keySet()) {
		FileSystemResource file = new FileSystemResource(files.get(key));
		message.addAttachment(key, file);
	}
}
         
         System.out.println("MailService.sendEmail()#send");
         javaMailSender.send(mimeMessage);
     } catch (Exception e) {
         e.printStackTrace();
     }
 }
 
开发者ID:gleb619,项目名称:hotel_shop,代码行数:25,代码来源:MailService.java

示例14: sendMessage

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
/**
 * Convenience method for sending messages with attachments.
 * 
 * @param recipients array of e-mail addresses
 * @param sender e-mail address of sender
 * @param resource attachment from classpath
 * @param bodyText text in e-mail
 * @param subject subject of e-mail
 * @param attachmentName name for attachment
 * @throws MessagingException thrown when can't communicate with SMTP server
 */
public void sendMessage(String[] recipients, String sender, 
                        ClassPathResource resource, String bodyText,
                        String subject, String attachmentName)
throws MessagingException {
    MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setTo(recipients);

    // use the default sending if no sender specified
    if (sender == null) {
        helper.setFrom(defaultFrom);
    } else {
       helper.setFrom(sender);
    }

    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

    ((JavaMailSenderImpl) mailSender).send(message);
}
 
开发者ID:SMVBE,项目名称:ldadmin,代码行数:37,代码来源:MailEngine.java

示例15: sendEmail

import org.springframework.mail.javamail.MimeMessageHelper; //导入方法依赖的package包/类
public void sendEmail() throws MessagingException {
    String email = backup.getEmail();
    JavaMailSenderImpl mailimpl = new JavaMailSenderImpl();
    mailimpl.setHost("smtp.163.com");
    MimeMessage mailMsg = mailimpl.createMimeMessage();
    MimeMessageHelper msgHelper = new MimeMessageHelper(mailMsg,true,"utf8");
    //set sender,receiver
    msgHelper.setTo(backup.getToemail());
    msgHelper.setFrom(email);
    msgHelper.setSubject("数据库备份");
    msgHelper.setText("<html><head></head><body><h1>数据库备份</h1></body></html>",true);

    msgHelper.addAttachment("study.sql",new File(backup.getSavePath() + File.separator + "study.sql"));
    msgHelper.addAttachment("blog.sql",new File(backup.getSavePath() + File.separator + "blog.sql"));

    mailimpl.setUsername(email);
    mailimpl.setPassword("liubo704");
    Properties prop = new Properties();
    prop.put("mail.smtp.auth",true);
    prop.put("mail.smtp.timeout","25000");

    mailimpl.setJavaMailProperties(prop);
    mailimpl.send(mailMsg);
}
 
开发者ID:ycsoft,项目名称:SweetHeart,代码行数:25,代码来源:BackupController.java


注:本文中的org.springframework.mail.javamail.MimeMessageHelper.addAttachment方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。