Java Code Examples for javax.mail.Message#addRecipients()

The following examples show how to use javax.mail.Message#addRecipients() . 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: EMail.java    From Hue-Ctrip-DI with MIT License 9 votes vote down vote up
/**
 * send email
 * 
 * @throws MessagingException
 * @throws Exception
 */
public static int sendMail(String subject, String content, String mails,
		String cc) throws MessagingException {

	Properties props = new Properties();
	props.put("mail.smtp.host", HOST);
	props.put("mail.smtp.starttls.enable", "true");
	// props.put("mail.smtp.port", "25");
	props.put("mail.smtp.auth", "true");
	// props.put("mail.debug", "true");
	Session mailSession = Session.getInstance(props, new MyAuthenticator());

	Message message = new MimeMessage(mailSession);
	message.setFrom(new InternetAddress(FROM));
	message.addRecipients(Message.RecipientType.TO, getMailList(mails));
	message.addRecipients(Message.RecipientType.CC, getMailList(cc));

	message.setSubject(subject);
	message.setContent(content, "text/html;charset=utf-8");

	Transport transport = mailSession.getTransport("smtp");
	try {
		transport.connect(HOST, USER, PASSWORD);
		transport.sendMessage(message, message.getAllRecipients());
	} finally {
		if (transport != null)
			transport.close();
	}

	return 0;
}
 
Example 2
Source File: Pop3Util.java    From anyline with Apache License 2.0 7 votes vote down vote up
/** 
 *  
 * @param fr 发送人姓名 
 * @param to 收件人地址 
 * @param title 邮件主题 
 * @param content  邮件内容 
 * @return return
 */ 
public boolean send(String fr, String to, String title, String content) { 
	log.warn("[send email][fr:{}][to:{}][title:{}][centent:{}]", fr, to, title, content); 
	try { 
		Session mailSession = Session.getDefaultInstance(props); 
		Message msg = new MimeMessage(mailSession); 
		msg.setFrom(new InternetAddress(config.ACCOUNT, fr)); 
		msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
		msg.setSubject(title); 
		msg.setContent(content, "text/html;charset=UTF-8"); 
		msg.saveChanges(); 
		Transport transport = mailSession.getTransport("smtp"); 
		transport.connect(config.HOST, config.ACCOUNT, config.PASSWORD); 
		transport.sendMessage(msg, msg.getAllRecipients()); 
		transport.close(); 
	} catch (Exception e) { 
		e.printStackTrace(); 
		return false; 
	} 

	return true; 
}
 
Example 3
Source File: MailUtil.java    From anyline with Apache License 2.0 7 votes vote down vote up
/** 
 *  
 * @param fr		发送人姓名  fr		发送人姓名
 * @param to		收件人地址  to		收件人地址
 * @param title		邮件主题  title		邮件主题
 * @param content	邮件内容  content	邮件内容
 * @return return
 */ 
public boolean send(String fr, String to, String title, String content) { 
	log.warn("[send email][fr:{}][to:{}][title:{}][content:{}]", fr,to,title,content);
	try { 
		Session mailSession = Session.getDefaultInstance(props); 
		Message msg = new MimeMessage(mailSession); 
		msg.setFrom(new InternetAddress(config.ACCOUNT,fr)); 
		msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
		msg.setSubject(title + ""); 
		msg.setContent(content + "", "text/html;charset=UTF-8"); 
		msg.saveChanges(); 
		Transport transport = mailSession.getTransport("smtp"); 
		transport.connect(config.HOST,
				config.ACCOUNT, 
				config.PASSWORD);
		transport.sendMessage(msg, msg.getAllRecipients()); 
		transport.close(); 
	} catch (Exception e) { 
		e.printStackTrace(); 
		return false; 
	} 

	return true; 
}
 
Example 4
Source File: EmailNotifierEngine.java    From repairnator with MIT License 7 votes vote down vote up
public void notify(String subject, String message) {
    if (this.from != null && !this.to.isEmpty()) {
        Message msg = new MimeMessage(this.session);

        try {
            Address[] recipients = this.to.toArray(new Address[this.to.size()]);
            Transport transport = this.session.getTransport();
            
            msg.setFrom(this.from);
            msg.addRecipients(Message.RecipientType.TO, recipients);
            msg.setSubject(subject);
            msg.setText(message);
            
            transport.connect();
            transport.sendMessage(msg, recipients);
            transport.close();
        } catch (MessagingException e) {
            logger.error("Error while sending notification message '" + subject + "'", e);
        }
    } else {
        logger.warn("From is null or to is empty. Notification won't be send.");
    }

}
 
Example 5
Source File: EmailSender.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public void send(List<String> addresses, String subject, String body, List<Attachment> attachments) throws EmailException {
  try {
    session = (session == null) ? createSession() : session;
    Message message = new MimeMessage(session);
    InternetAddress fromAddr = toAddress(from);
    message.setFrom(fromAddr);
    List<InternetAddress> toAddrs = toAddress(addresses);
    message.addRecipients(Message.RecipientType.TO, toAddrs.toArray(new InternetAddress[toAddrs.size()]));
    message.setSubject(subject);

    if(attachments != null && !attachments.isEmpty()) {
      MimeMultipart multipart = new MimeMultipart();
      MimeBodyPart htmlBodyPart = new MimeBodyPart();
      htmlBodyPart.setContent(body, "text/html; charset=UTF-8");
      multipart.addBodyPart(htmlBodyPart);

      for(Attachment attachment: attachments) {
        MimeBodyPart attachmentBodyPart = new MimeBodyPart();
        ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment.getInputStream(), attachment.getContentType());
        attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
        attachmentBodyPart.setFileName(attachment.getFilename());
        multipart.addBodyPart(attachmentBodyPart);
      }

      message.setContent(multipart);
    } else {
      message.setContent(body, "text/html; charset=UTF-8");
    }

    Transport transport = session.getTransport(protocol);
    if(auth) {
      transport.connect(host, user, password);
    } else {
      transport.connect(host, null, null);
    }
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
  } catch (Exception ex) {
    session = null;
    throw new EmailException(ex);
  }
}