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

The following examples show how to use javax.mail.Message#addRecipient() . 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: NotificationSender.java    From herd-mdl with Apache License 2.0 6 votes vote down vote up
public void sendEmail( String msgBody, String subject ) {
	Properties prop = new Properties( System.getProperties() );
	prop.put( "mail.smtp.host", emailHost );

	Session session = Session.getDefaultInstance( prop, null );

	try {

		Message msg = new MimeMessage( session );
		msg.setFrom( new InternetAddress( "[email protected]", ags ) );

		msg.addRecipient( Message.RecipientType.TO, new InternetAddress( mailingList ) );

		msg.setSubject( getUpdatedSubject( subject ) );

		msg.setDataHandler( new DataHandler( new ByteArrayDataSource( msgBody, "text/plain" ) ) );
		javax.mail.Transport.send( msg );
	} catch ( Exception ex ) {
		log.error( ex.getMessage() );
	}
}
 
Example 2
Source File: NotifierBean.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
private void sendEmail(String email, String name, String subject, String content) throws MessagingException {

        if (email == null || email.isEmpty()) {
            LOGGER.log(Level.WARNING, "Cannot send mail, email is empty");
            return;
        }

        try {
            InternetAddress emailAddress = new InternetAddress(email, name);
            Message message = new MimeMessage(mailSession);
            message.addRecipient(Message.RecipientType.TO, emailAddress);
            message.setSubject(subject);
            message.setSentDate(new Date());
            message.setContent(content, "text/html; charset=utf-8");
            message.setFrom();
            Transport.send(message);
        } catch (UnsupportedEncodingException e) {
            String logMessage = "Unsupported encoding: " + e.getMessage();
            LOGGER.log(Level.SEVERE, logMessage, e);
        }
    }
 
Example 3
Source File: SendMail.java    From chronos with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@CoverageIgnore
public static void doSend(String subject, String messageBody, MailInfo mailInfo, Session session) {
  String hostname = "";
  try {
    hostname = InetAddress.getLocalHost().getHostName();
  } catch (Exception ignore) {}
  
  try {
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(mailInfo.from, mailInfo.fromName + " " + hostname));
    for (String currTo : mailInfo.to.split(",")) {
      msg.addRecipient(Message.RecipientType.TO,
                       new InternetAddress(currTo, mailInfo.toName));
    }
    msg.setSubject(subject);
    msg.setContent(messageBody, "text/html");
    Transport.send(msg);
    
  } catch (UnsupportedEncodingException | MessagingException e) {
    LOG.error("SendMail error:", e);
  }
  LOG.info(String.format("Sent email from %s, to %s", mailInfo.from, mailInfo.to));
}
 
Example 4
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Sends an email.
 * @return true if no error occurred, false otherwise
 */
public static boolean sendEmail( final InternetAddress from, final InternetAddress to, final InternetAddress cc, final String subject, final String body ) {
	LOGGER.info( "Sending email to: " + to.toString() + ", subject: " + subject );
	final Session session = Session.getDefaultInstance( new Properties(), null );
	try {
		final Message message = new MimeMessage( session );
		message.setFrom( from );
		message.addRecipient( Message.RecipientType.TO, to );
		if ( cc != null )
			message.addRecipient( Message.RecipientType.CC, cc );
		message.addRecipient( Message.RecipientType.BCC, ADMIN_EMAIL );
		message.setSubject( "[Sc2gears Database] " + subject );
		message.setText( body );
		Transport.send( message );
		return true;
	} catch ( final Exception e ) {
		LOGGER.log( Level.SEVERE, "Failed to send email!", e );
		return false;
	}
}
 
Example 5
Source File: MailServerImpl.java    From webcurator with Apache License 2.0 6 votes vote down vote up
private void addRecipient(Message message, String emails, Message.RecipientType rType) 
throws MessagingException, AddressException
{
	if (emails != null && emails.length() > 0) {
		if (emails.contains(";"))
		{
	    	for (String email: emails.split(";"))
	    	{
	    		message.addRecipient(rType, new InternetAddress(email));
	    	}
		}
		else
		{
			message.addRecipient(rType, new InternetAddress(emails));
		}
	}
}
 
Example 6
Source File: MailServerImpl.java    From webcurator with Apache License 2.0 6 votes vote down vote up
public void send(Mailable email) throws MessagingException {
    Session mailSession = Session.getInstance(this.mailConfig, null);
    Message message = new MimeMessage(mailSession);

    message.setFrom(new InternetAddress(email.getSender()));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getRecipients()));
    setUpCCandBCC(email, message);

    if(email.getReplyTo()!=null && email.getReplyTo().trim().length()!=0) {
        message.setReplyTo(new Address[] {new InternetAddress(email.getReplyTo())});
    }

    message.setSubject(email.getSubject());
    message.setSentDate(new Date());
    message.setContent(email.getMessage(), "text/plain; charset=UTF-8");

    Transport.send(message); 
}
 
Example 7
Source File: SendConfirmationEmailServlet.java    From ud859 with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String email = request.getParameter("email");
    String conferenceInfo = request.getParameter("conferenceInfo");
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    String body = "Hi, you have created a following conference.\n" + conferenceInfo;
    try {
        Message message = new MimeMessage(session);
        InternetAddress from = new InternetAddress(
                String.format("noreply@%s.appspotmail.com",
                        SystemProperty.applicationId.get()), "Conference Central");
        message.setFrom(from);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, ""));
        message.setSubject("You created a new Conference!");
        message.setText(body);
        Transport.send(message);
    } catch (MessagingException e) {
        LOG.log(Level.WARNING, String.format("Failed to send an mail to %s", email), e);
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: SendConfirmationEmailServlet.java    From ud859 with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String email = request.getParameter("email");
    String conferenceInfo = request.getParameter("conferenceInfo");
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    String body = "Hi, you have created a following conference.\n" + conferenceInfo;
    try {
        Message message = new MimeMessage(session);
        InternetAddress from = new InternetAddress(
                String.format("noreply@%s.appspotmail.com",
                        SystemProperty.applicationId.get()), "Conference Central");
        message.setFrom(from);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, ""));
        message.setSubject("You created a new Conference!");
        message.setText(body);
        Transport.send(message);
    } catch (MessagingException e) {
        LOG.log(Level.WARNING, String.format("Failed to send an mail to %s", email), e);
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: SendConfirmationEmailServlet.java    From ud859 with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String email = request.getParameter("email");
    String conferenceInfo = request.getParameter("conferenceInfo");
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    String body = "Hi, you have created a following conference.\n" + conferenceInfo;
    try {
        Message message = new MimeMessage(session);
        InternetAddress from = new InternetAddress(
                String.format("noreply@%s.appspotmail.com",
                        SystemProperty.applicationId.get()), "Conference Central");
        message.setFrom(from);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, ""));
        message.setSubject("You created a new Conference!");
        message.setText(body);
        Transport.send(message);
    } catch (MessagingException e) {
        LOG.log(Level.WARNING, String.format("Failed to send an mail to %s", email), e);
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: Email.java    From smslib-v3 with Apache License 2.0 6 votes vote down vote up
@Override
public void MessagesReceived(Collection<InboundMessage> msgList) throws Exception
{
	for (InboundMessage im : msgList)
	{
		Message msg = new MimeMessage(this.mailSession);
		msg.setFrom();
		msg.addRecipient(RecipientType.TO, new InternetAddress(getProperty("to")));
		msg.setSubject(updateTemplateString(this.messageSubject, im));
		if (this.messageBody != null)
		{
			msg.setText(updateTemplateString(this.messageBody, im));
		}
		else
		{
			msg.setText(im.toString());
		}
		msg.setSentDate(im.getDate());
		Transport.send(msg);
	}
}
 
Example 11
Source File: EmailNotification.java    From oodt with Apache License 2.0 5 votes vote down vote up
@Override
public boolean performAction(File product, Metadata metadata)
      throws CrawlerActionException {
   try {
      Properties props = new Properties();
      props.put("mail.host", this.mailHost);
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.from", this.sender);

      Session session = Session.getDefaultInstance(props);
      Message msg = new MimeMessage(session);
      msg.setSubject(PathUtils.doDynamicReplacement(subject, metadata));
      msg.setText(new String(PathUtils.doDynamicReplacement(message,
            metadata).getBytes()));
      for (String recipient : recipients) {
         try {
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                  PathUtils.replaceEnvVariables(recipient.trim(), metadata),
                  ignoreInvalidAddresses));
            LOG.fine("Recipient: "
                  + PathUtils.replaceEnvVariables(recipient.trim(), metadata));
         } catch (AddressException ae) {
            LOG.fine("Recipient: "
                  + PathUtils.replaceEnvVariables(recipient.trim(), metadata));
            LOG.warning(ae.getMessage());
         }
      }
      LOG.fine("Subject: " + msg.getSubject());
      LOG.fine("Message: "
            + new String(PathUtils.doDynamicReplacement(message, metadata)
                  .getBytes()));
      Transport.send(msg);
      return true;
   } catch (Exception e) {
      LOG.severe(e.getMessage());
      return false;
   }
}