Java Code Examples for javax.mail.BodyPart#setText()

The following examples show how to use javax.mail.BodyPart#setText() . 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: Mailer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private static Multipart getMessagePart() throws MessagingException, IOException {
    Multipart multipart = new MimeMultipart();
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(getVal("msg.Body"));
    multipart.addBodyPart(messageBodyPart);
    if (getBoolVal("attach.reports")) {
        LOG.info("Attaching Reports as zip");
        multipart.addBodyPart(getReportsBodyPart());
    } else {
        if (getBoolVal("attach.standaloneHtml")) {
            multipart.addBodyPart(getStandaloneHtmlBodyPart());
        }
        if (getBoolVal("attach.console")) {
            multipart.addBodyPart(getConsoleBodyPart());
        }
        if (getBoolVal("attach.screenshots")) {
            multipart.addBodyPart(getScreenShotsBodyPart());
        }
    }
    messageBodyPart.setContent(getVal("msg.Body")
            .concat("\n\n\n")
            .concat(MailComponent.getHTMLBody()), "text/html");
    return multipart;
}
 
Example 2
Source File: GMailSender.java    From vocefiscal-android with Apache License 2.0 5 votes vote down vote up
public boolean send() throws Exception 
{ 
  Properties props = setProperties(); 

  if(!user.equals("") && !pass.equals("") && to.length > 0 && !from.equals("") && !subject.equals("") && !body.equals("")) 
  { 
    Session session = Session.getInstance(props, this); 

    MimeMessage msg = new MimeMessage(session); 

    msg.setFrom(new InternetAddress(from)); 

    InternetAddress[] addressTo = new InternetAddress[to.length]; 
    for (int i = 0; i < to.length; i++) 
    { 
      addressTo[i] = new InternetAddress(to[i]); 
    } 
      msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 

    msg.setSubject(subject); 
    msg.setSentDate(new Date()); 

    // setup message body 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText(body); 
    multipart.addBodyPart(messageBodyPart); 

    // Put parts in message 
    msg.setContent(multipart); 

    // send email 
    Transport.send(msg); 

    return true; 
  } else 
  { 
    return false; 
  } 
}
 
Example 3
Source File: MultiPartEmail.java    From commons-email with Apache License 2.0 5 votes vote down vote up
/**
 * Set the message of the email.
 *
 * @param msg A String.
 * @return An Email.
 * @throws EmailException see javax.mail.internet.MimeBodyPart
 *  for definitions
 * @since 1.0
 */
@Override
public Email setMsg(final String msg) throws EmailException
{
    // throw exception on null message
    if (EmailUtils.isEmpty(msg))
    {
        throw new EmailException("Invalid message supplied");
    }
    try
    {
        final BodyPart primary = getPrimaryBodyPart();

        if (primary instanceof MimePart && EmailUtils.isNotEmpty(charset))
        {
            ((MimePart) primary).setText(msg, charset);
        }
        else
        {
            primary.setText(msg);
        }
    }
    catch (final MessagingException me)
    {
        throw new EmailException(me);
    }
    return this;
}
 
Example 4
Source File: Mail.java    From mobikul-standalone-pos with MIT License 4 votes vote down vote up
public boolean send() throws Exception {
        Properties props = _setProperties();

        if (!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") &&
                !_subject.equals("") && !_body.equals("")) {

            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(ApplicationConstants.USERNAME_FOR_SMTP, ApplicationConstants.PASSWORD_FOR_SMTP);
                }
            });
            SMTPAuthenticator authentication = new SMTPAuthenticator();
            javax.mail.Message msg = new MimeMessage(Session
                    .getDefaultInstance(props, authentication));
            msg.setFrom(new InternetAddress(_from));

            InternetAddress[] addressTo = new InternetAddress[_to.length];
            for (int i = 0; i < _to.length; i++) {
                addressTo[i] = new InternetAddress(_to[i]);
            }
            msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

            msg.setSubject(_subject);
            msg.setSentDate(new Date());

// setup message body
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(_body);
            _multipart.addBodyPart(messageBodyPart);

// Put parts in message
            msg.setContent(_multipart);

// send email
            String protocol = "smtp";
            props.put("mail." + protocol + ".auth", "true");
            Transport t = session.getTransport(protocol);
            try {
                t.connect(ApplicationConstants.HOST_FOR_MAIL, ApplicationConstants.USERNAME_FOR_SMTP, ApplicationConstants.PASSWORD_FOR_SMTP);
                t.sendMessage(msg, msg.getAllRecipients());
            } finally {
                t.close();
            }

            return true;
        } else {
            return false;
        }
    }
 
Example 5
Source File: TestMailClient.java    From holdmail with Apache License 2.0 4 votes vote down vote up
private BodyPart createTextBodyPart(String messageBody) throws MessagingException {
    BodyPart textPart = new MimeBodyPart();
    textPart.setText(messageBody);
    return textPart;
}
 
Example 6
Source File: Mail.java    From AccelerationAlert with Apache License 2.0 4 votes vote down vote up
public boolean send() throws Exception
{
	Properties props = _setProperties();

	if (!_user.equals("") && !_pass.equals("") && _to.length > 0
			&& !_from.equals("") && !_subject.equals("")
			&& !_body.equals(""))
	{
		Session session = Session.getInstance(props, this);

		MimeMessage msg = new MimeMessage(session);

		msg.setFrom(new InternetAddress(_from));

		InternetAddress[] addressTo = new InternetAddress[_to.length];
		for (int i = 0; i < _to.length; i++)
		{
			addressTo[i] = new InternetAddress(_to[i]);
		}
		msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

		msg.setSubject(_subject);
		msg.setSentDate(new Date());

		// setup message body
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText(_body);
		_multipart.addBodyPart(messageBodyPart);

		// Put parts in message
		msg.setContent(_multipart);

		// send email
		Transport.send(msg);

		return true;
	}
	else
	{
		return false;
	}
}