Java Code Examples for org.springframework.mail.javamail.JavaMailSenderImpl#send()

The following examples show how to use org.springframework.mail.javamail.JavaMailSenderImpl#send() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailUtil.java    From boubei-tss with Apache License 2.0 8 votes vote down vote up
public static void send(String subject, String text, String receiver[], String _ms) {
	if(receiver == null || receiver.length == 0) {
		log.error("收件人不能为空"); 
		return;
	}
	
	JavaMailSenderImpl sender = MailUtil.getMailSender(_ms);
	try {
		SimpleMailMessage mail = new SimpleMailMessage();
		mail.setFrom( getEmailFrom(_ms) ); // 发送者,这里还可以另起Email别名
		mail.setTo( preCheatEmails(receiver) );
		mail.setSubject(subject);    // 主题
		mail.setText(text);         // 邮件内容
		
		sender.send(mail);
	} 
	catch (Exception e) {
		log.error("发送文本邮件(" +subject+ ")出错了, " + receiver + ":" + e.getMessage());
	}
}
 
Example 2
Source File: UnifiedUserMngImpl.java    From Lottery with GNU General Public License v2.0 6 votes vote down vote up
private void senderEmail(final Integer uid, final String username,
		final String to, final String resetKey, final String resetPwd,
		final EmailSender email, final MessageTemplate tpl) {
	JavaMailSenderImpl sender = new JavaMailSenderImpl();
	sender.setHost(email.getHost());
	sender.setUsername(email.getUsername());
	sender.setPassword(email.getPassword());
	sender.send(new MimeMessagePreparator() {
		public void prepare(MimeMessage mimeMessage)
				throws MessagingException, UnsupportedEncodingException {
			MimeMessageHelper msg = new MimeMessageHelper(mimeMessage,
					false, email.getEncoding());
			msg.setSubject(tpl.getForgotPasswordSubject());
			msg.setTo(to);
			msg.setFrom(email.getUsername(), email.getPersonal());
			String text = tpl.getForgotPasswordText();
			text = StringUtils.replace(text, "${uid}", String.valueOf(uid));
			text = StringUtils.replace(text, "${username}", username);
			text = StringUtils.replace(text, "${resetKey}", resetKey);
			text = StringUtils.replace(text, "${resetPwd}", resetPwd);
			msg.setText(text);
		}
	});
}
 
Example 3
Source File: EmailSender.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * 发送邮件
 */
public void sendSimpleMail(JavaMailSenderImpl javaMailSender, EmailMessage emailMessage) throws Exception {
    MimeMessage message = null;
    message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(emailMessage.getTo());
    if (emailMessage.getCc() != null && emailMessage.getCc().length > 0) {
        helper.setCc(emailMessage.getCc());
    }
    helper.setFrom(javaMailSender.getUsername());
    helper.setSubject(emailMessage.getSubject());
    helper.setText(emailMessage.getContent(), true);
    this.addAttachment(helper, emailMessage);
    javaMailSender.send(message);
}
 
Example 4
Source File: MailSender.java    From cola-cloud with MIT License 5 votes vote down vote up
public static void main(String[] args) {

        JavaMailSenderImpl js=new JavaMailSenderImpl();
        js.setHost("smtp.qq.com");
        js.setUsername("[email protected]");
        js.setPassword("mygdzwlcjlxpbghc");
        Properties props = new Properties();
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.ssl.enable", true);
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.timeout", 25000);
        js.setJavaMailProperties(props);
        MimeMessage message = null;
        try {
            message = js.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
           // helper.setFrom(new InternetAddress(this.getFrom(), MimeUtility.encodeText(this.name,"UTF-8", "B")));
            helper.setFrom("[email protected]");
            helper.setTo("[email protected]");
            helper.setSubject("测试");
            helper.setText("测试内容");
            //addAttachmentStatic(helper,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        js.send(message);
    }
 
Example 5
Source File: EmailServiceImpl.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
@Override
public void sendHtmlMail(String to, String title, String content) {
    EmailSetting emailSetting = configService.selectConfigByConfigKey(ConfigKey.CONFIG_KEY_EMAIL_SETTING, EmailSetting.class);
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setUsername(emailSetting.getUser());
    mailSender.setHost(emailSetting.getHost());
    mailSender.setDefaultEncoding("utf-8");
    mailSender.setPassword(emailSetting.getPassword());
    mailSender.setPort(emailSetting.getPort());

    Properties properties = new Properties();
    properties.put("mail.smtp.host", emailSetting.getHost());
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.socketFactory.fallback", "false"); // 只处理SSL的连接,对于非SSL的连接不做处理
    properties.put("mail.smtp.port", emailSetting.getPort());
    properties.put("mail.smtp.socketFactory.port", emailSetting.getPort());
    properties.put("mail.smtp.ssl.enable", true);
    Session session = Session.getInstance(properties);

    MimeMessage mimeMessage = mailSender.createMimeMessage();
    try {
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(emailSetting.getFromEmail());
        mimeMessageHelper.setTo(to);
        if (StringUtils.isNotEmpty(emailSetting.getStationmasterEmail())) {
            mimeMessageHelper.setBcc(emailSetting.getStationmasterEmail());
        }
        mimeMessageHelper.setSubject(title);
        mimeMessageHelper.setText(content, true);
        mailSender.setSession(session);
        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
       log.error(e.getMessage(), e);
    }
}
 
Example 6
Source File: ReportJob.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
private void send(String _ms, String[] receiver, ReceiverReports rr) {
	String title = EasyUtils.list2Str(rr.reportTitles);
	
	JavaMailSenderImpl sender = MailUtil.getMailSender(_ms);
	MimeMessage mailMessage = sender.createMimeMessage();
	
	try {
		// 设置utf-8或GBK编码,否则邮件会有乱码
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
		messageHelper.setFrom( MailUtil.getEmailFrom(_ms) ); // 发送者
		messageHelper.setTo(receiver);                       // 接受者
		messageHelper.setSubject(EX.TIMER_REPORT + title);   // 主题
		
		// 邮件内容,注意加参数true
		StringBuffer html = new StringBuffer();
		html.append("<html>");
		html.append("<head>");
		html.append("<style type='text/css'> " );
		html.append("	table { border-collapse:collapse; border-spacing:0; }");
		html.append("	td { line-height: 1.42857143; vertical-align: top;  border: 1px solid black; text-align: left;}");
		html.append("	td { margin:0; padding:0; padding: 2px 2px 2px 2px; font-family: 微软雅黑; font-size: 15px;}");
		html.append("	thead td { background-color:#E4E6F5; font-weight: bold; }");
		html.append("</style>");
		html.append("</head>");
		html.append("<body>");
		
		for(int index = 0; index < rr.reportIds.size(); index++) {
			buildEmailContent(rr, index, messageHelper, html);
		}
		
		html.append("</body>");
		html.append("</html>");
		log.debug(html);
		messageHelper.setText(html.toString(), true);
		sender.send(mailMessage);
	} 
	catch (Exception e) {
		log.error(" error when send report email ", e);
	}
}
 
Example 7
Source File: MailUtil.java    From boubei-tss with Apache License 2.0 5 votes vote down vote up
public static void sendHTML(String subject, String htmlText, String receiver[], String _ms, File...attaches) {
	// 邮件内容,注意加参数true
	StringBuffer html = new StringBuffer();
	html.append("<html>");
	html.append("<head>");
	html.append("<style type='text/css'> " );
	html.append("	body { margin:0; padding:5px; font-family: 微软雅黑; font-size: 15px;}");
	html.append("</style>");
	html.append("</head>");
	html.append("<body>");
	html.append(htmlText);
	html.append("</body>");
	html.append("</html>");
	
	JavaMailSenderImpl sender = MailUtil.getMailSender(_ms);
	MimeMessage mailMsg = sender.createMimeMessage();
	try {
		// 设置utf-8或GBK编码,否则邮件会有乱码
		MimeMessageHelper mh = new MimeMessageHelper(mailMsg, true, "utf-8");
		mh.setFrom( getEmailFrom(_ms) );       // 发送者
		mh.setTo( preCheatEmails(receiver) );  // 接受者
		mh.setSubject(subject);                // 主题
		mh.setText( html.toString(), true );
		
		for(File attach : attaches) {
			mh.addAttachment(MimeUtility.encodeWord(attach.getName()), attach);
		}
		
		sender.send(mailMsg);
	} 
	catch (Exception e) {
		log.error("发送邮件【" +subject+ "】出错了, " + Arrays.asList(receiver) + ", " + e.getMessage());
	}
}
 
Example 8
Source File: EmailServiceImpl.java    From BlogSystem with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sendFeedback(int bloggerId, String subject, String content, String contact) {

    JavaMailSenderImpl mailSender = mailManager.getMailSender(
            websiteProperties.getMailSenderAddress(),
            websiteProperties.getMailSenderPassword());

    MimeMessage mailMessage = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "utf-8");

        // 设置发件人
        helper.setFrom(websiteProperties.getMailSenderAddress());

        // 设置收件人
        helper.setTo(websiteProperties.getManageEmailAddress());

        // 设置主题
        helper.setSubject(subject);

        // 邮件体
        BloggerAccount account = bloggerId > 0 ? accountDao.getAccountById(bloggerId) : null;
        helper.setText(account != null ?
                "from:\n\tusername: " + account.getUsername() + "\n\tid: " + account.getId() + "\n\n" + content + "\n\n\tcontact: " + contact :
                content + "\n\n\tcontact: " + contact);

        mailSender.send(mailMessage);// 发送邮件
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;

}
 
Example 9
Source File: DefaultMailService.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private void sendMail(JavaMailSenderImpl mailSender, String mailFrom, String email, String subject, String message)
    throws IoTPException {
  try {
    MimeMessage mimeMsg = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, "UTF-8");
    helper.setFrom(mailFrom);
    helper.setTo(email);
    helper.setSubject(subject);
    helper.setText(message, true);
    mailSender.send(helper.getMimeMessage());
  } catch (Exception e) {
    throw handleException(e);
  }
}
 
Example 10
Source File: SendEmailService.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void sendEmail(JavaMailSenderImpl javaMailSenderImpl, String from, String to, String subject, String body) {
	SimpleMailMessage mailMessage = new SimpleMailMessage();
	mailMessage.setFrom(from);
	mailMessage.setTo(to);
	mailMessage.setSubject(subject);
	mailMessage.setText(body);
	javaMailSenderImpl.send(mailMessage);
}
 
Example 11
Source File: SendEmailService.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void sendEmail(JavaMailSenderImpl javaMailSenderImpl, String from, String to, String subject, String body) {
	SimpleMailMessage mailMessage = new SimpleMailMessage();
	mailMessage.setFrom(from);
	mailMessage.setTo(to);
	mailMessage.setSubject(subject);
	mailMessage.setText(body);
	javaMailSenderImpl.send(mailMessage);
}
 
Example 12
Source File: NeteaseMailUtil.java    From charging_pile_cloud with MIT License 4 votes vote down vote up
/**
 * @param to    目的邮箱
 * @param title 标题
 * @param text  内容
 * @return
 */
private static boolean sendMailText(String to, String title, String text) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    Properties prop = new Properties();
    System.out.println("sendMail...util...");
    try {
        //设定mail server
        senderImpl.setHost(emailHost);
        //senderImpl.setPort(587);

        // 设置收件人,寄件人 用数组发送多个邮件
        // String[] array = new String[]    {"[email protected]","[email protected]"};
        // mailMessage.setTo(array);
        mailMessage.setTo(to);
        mailMessage.setFrom(sendEmail);
        mailMessage.setSubject(title);
        mailMessage.setText(text);

        senderImpl.setUsername(emailUsername);
        senderImpl.setPassword(authorizationPwd);

        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        prop.setProperty("mail.smtp.socketFactory.fallback", "false");

        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");

        // 设置传输协议
        prop.setProperty("mail.transport.protocol", "smtp");

        prop.put("mail.smtp.auth", isAuth);
        prop.put("mail.smtp.timeout", linkTimeout);
        senderImpl.setJavaMailProperties(prop);
        System.out.println("发送信息:" + text);

        MimeMessage msg = senderImpl.createMimeMessage();
        msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(sendEmail));
        //发送邮件
        senderImpl.send(mailMessage);

        System.out.println("发送邮件成功");

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("发送邮件失败");
        return false;
    }
}
 
Example 13
Source File: NeteaseMailUtil.java    From charging_pile_cloud with MIT License 4 votes vote down vote up
/**
 * 发送网页图片邮箱
 *
 * @param to
 * @param title
 * @param text
 * @return
 */
private static boolean sendMailHtml(String to, String title, String text) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    //SimpleMailMessage mailMessage = new SimpleMailMessage();

    //建立邮件消息,发送简单邮件和html邮件的区别
    MimeMessage mailMessage = senderImpl.createMimeMessage();

    Properties prop = new Properties();
    System.out.println("sendMail...util...");
    try {
        //注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
        //multipart模式
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        prop.setProperty("mail.smtp.socketFactory.fallback", "false");

        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");

        // 设置传输协议
        prop.setProperty("mail.transport.protocol", "smtp");

        prop.put("mail.smtp.auth", isAuth);
        prop.put("mail.smtp.timeout", linkTimeout);
        senderImpl.setJavaMailProperties(prop);
        senderImpl.setUsername(emailUsername);
        senderImpl.setPassword(authorizationPwd);
        //设定mail server
        senderImpl.setHost(emailHost);
        //senderImpl.setPort(587);

        // 设置收件人,寄件人 用数组发送多个邮件
        // String[] array = new String[]    {"[email protected]","[email protected]"};
        // mailMessage.setTo(array);
        messageHelper.setTo(to);
        messageHelper.setFrom(sendEmail);
        messageHelper.setSubject(title);
        messageHelper.setText(messageHtml(text), true);

        System.out.println("发送信息:" + text);

        MimeMessage msg = senderImpl.createMimeMessage();
        msg.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(sendEmail));
        //发送邮件
        senderImpl.send(mailMessage);

        System.out.println("发送邮件成功");

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("发送邮件失败");
        return false;
    }
}