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

The following examples show how to use javax.mail.Message#setRecipient() . 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: JavaMail.java    From EasyML with Apache License 2.0 6 votes vote down vote up
public boolean sendMsg(String recipient, String subject, String content)
		throws MessagingException {
	// Create a mail object
	Session session = Session.getInstance(props, new Authenticator() {
		// Set the account information session,transport will send mail
		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(Constants.MAIL_USERNAME, Constants.MAIL_PASSWORD);
		}
	});
	session.setDebug(true);
	Message msg = new MimeMessage(session);
	try {
		msg.setSubject(subject);			//Set the mail subject
		msg.setContent(content,"text/html;charset=utf-8");
		msg.setFrom(new InternetAddress(Constants.MAIL_USERNAME));			//Set the sender
		msg.setRecipient(RecipientType.TO, new InternetAddress(recipient));	//Set the recipient
		Transport.send(msg);
		return true;
	} catch (Exception ex) {
		ex.printStackTrace();
		System.out.println(ex.getMessage());
		return false;
	}

}
 
Example 2
Source File: ComposeSimpleMessage.java    From java-course-ee with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        final Properties props = new Properties();
        props.load(ClassLoader.getSystemResourceAsStream("mail.properties"));

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(props.getProperty("smtp.username"), props.getProperty("smtp.pass"));
            }
        });

        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress(props.getProperty("address.sender")));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(props.getProperty("address.recipient")));
        message.setSubject("Test JavaMail");

        message.setText("Hello!\n\n\tThis is a test message from JavaMail.\n\nThank you.");

        Transport.send(message);

        log.info("Message was sent");
    }
 
Example 3
Source File: EmailUtils.java    From tools with MIT License 5 votes vote down vote up
/**
 * 设置收件人和抄送人
 *
 * @param tos    收件人
 * @param copyTo 抄送人
 * @param msg    消息
 * @throws MessagingException email异常
 */
private void setRecipient(String tos, String copyTo, Message msg) throws MessagingException {
    if (ParamUtils.split(tos, ",").length == 1) {
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(tos));
    } else {
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(tos));
    }
    if (ParamUtils.isNotEmpty(copyTo)) {
        InternetAddress[] copyToArr = InternetAddress.parse(copyTo);
        msg.setRecipients(Message.RecipientType.CC, copyToArr);
    }
}
 
Example 4
Source File: MailingService.java    From blog-tutorials with MIT License 5 votes vote down vote up
public void sendSimpleMail() {

    Message simpleMail = new MimeMessage(mailSession);

    try {
      simpleMail.setSubject("Hello World from Java EE!");
      simpleMail.setRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));

      MimeMultipart mailContent = new MimeMultipart();

      MimeBodyPart mailMessage = new MimeBodyPart();
      mailMessage.setContent("<p>Take a look at the <b>scecretMessage.txt</b> file</p>", "text/html; charset=utf-8");
      mailContent.addBodyPart(mailMessage);

      MimeBodyPart mailAttachment = new MimeBodyPart();
      DataSource source = new ByteArrayDataSource("This is a secret message".getBytes(), "text/plain");
      mailAttachment.setDataHandler(new DataHandler(source));
      mailAttachment.setFileName("secretMessage.txt");

      mailContent.addBodyPart(mailAttachment);
      simpleMail.setContent(mailContent);

      Transport.send(simpleMail);

      System.out.println("Message successfully send to: " + emailAddress);
    } catch (MessagingException e) {
      e.printStackTrace();
    }

  }
 
Example 5
Source File: DocumentCRUD.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void sendMail(String emailAddress, String subject, String emailContent) throws Exception {

		SessionFacade facade = MailSessionBuilder.newInstance()
			.usingUserProfile()
			.withTimeout(5000)
			.withConnectionTimeout(5000)
			.build();

		// create a message
		Message msg = facade.createNewMimeMessage();

		InternetAddress addressTo = new InternetAddress(emailAddress);

		msg.setRecipient(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		// create and fill the first message part
		MimeBodyPart mbp1 = new MimeBodyPart();
		mbp1.setText(emailContent);
		// create the Multipart and add its parts to it
		Multipart mp = new MimeMultipart();
		mp.addBodyPart(mbp1);
		// add the Multipart to the message
		msg.setContent(mp);

		// send message
		facade.sendMessage(msg);

	}
 
Example 6
Source File: Signup.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void sendMail(String emailAddress, String subject, String emailContent) throws Exception {

		SessionFacade facade = MailSessionBuilder.newInstance()
			.usingUserProfile()
			.withTimeout(5000)
			.withConnectionTimeout(5000)
			.build();

		// create a message
		Message msg = facade.createNewMimeMessage();

		InternetAddress addressTo = new InternetAddress(emailAddress);

		msg.setRecipient(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		// create and fill the first message part
		// MimeBodyPart mbp1 = new MimeBodyPart();
		// mbp1.setText(emailContent);
		// // create the Multipart and add its parts to it
		// Multipart mp = new MimeMultipart();
		// mp.addBodyPart(mbp1);
		// // add the Multipart to the message
		// msg.setContent(mp);
		msg.setContent(emailContent, "text/html");

		// send message
		facade.sendMessage(msg);

	}
 
Example 7
Source File: MailMemoryLogger.java    From dkpro-jwpl with Apache License 2.0 5 votes vote down vote up
protected Message initMessage() throws MessagingException {
	Message msg = new MimeMessage(MAIL_SESSION);
	msg.setFrom(new InternetAddress(ADDRESS_FROM));
	msg.setRecipient(Message.RecipientType.TO, new InternetAddress(ADDRESS_TO));
	msg.setSubject(subject);
	return msg;
}
 
Example 8
Source File: SimpleMailSender.java    From KJFrameForAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 以文本格式发送邮件
 * 
 * @param mailInfo
 *            待发送的邮件的信息
 */
public boolean sendTextMail(MailSenderInfo mailInfo) {
    // 判断是否需要身份认证
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        // 如果需要身份认证,则创建一个密码验证器
        authenticator = new MyAuthenticator(mailInfo.getUserName(),
                mailInfo.getPassword());
    }
    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session
            .getDefaultInstance(pro, authenticator);
    try {
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        // 设置邮件消息的发送者
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address to = new InternetAddress(mailInfo.getToAddress());
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件消息的主题
        mailMessage.setSubject(mailInfo.getSubject());
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // 设置邮件消息的主要内容
        String mailContent = mailInfo.getContent();
        mailMessage.setText(mailContent);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
 
Example 9
Source File: SimpleMailSender.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * @param mailInfo
 *
 * @return
 */
private static Message setCommon(MailSenderInfo mailInfo) throws MessagingException {

    //
    // 判断是否需要身份认证
    //
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        // 如果需要身份认证,则创建一个密码验证器
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }

    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session.getDefaultInstance(pro, authenticator);

    // 根据session创建一个邮件消息
    Message mailMessage = new MimeMessage(sendMailSession);

    // 创建邮件发送者地址
    Address from = new InternetAddress(mailInfo.getFromAddress());

    // 设置邮件消息的发送者
    mailMessage.setFrom(from);

    // 创建邮件的接收者地址,并设置到邮件消息中
    Address to = new InternetAddress(mailInfo.getToAddress());
    mailMessage.setRecipient(Message.RecipientType.TO, to);

    // 设置邮件消息的主题
    mailMessage.setSubject(mailInfo.getSubject());

    // 设置邮件消息发送的时间
    mailMessage.setSentDate(new Date());

    return mailMessage;

}
 
Example 10
Source File: SimpleMailSender.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * @param mailInfo
 *
 * @return
 */
private static Message setCommon(MailSenderInfo mailInfo) throws MessagingException {

    //
    // 判断是否需要身份认证
    //
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        // 如果需要身份认证,则创建一个密码验证器
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }

    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session.getDefaultInstance(pro, authenticator);

    // 根据session创建一个邮件消息
    Message mailMessage = new MimeMessage(sendMailSession);

    // 创建邮件发送者地址
    Address from = new InternetAddress(mailInfo.getFromAddress());

    // 设置邮件消息的发送者
    mailMessage.setFrom(from);

    // 创建邮件的接收者地址,并设置到邮件消息中
    Address to = new InternetAddress(mailInfo.getToAddress());
    mailMessage.setRecipient(Message.RecipientType.TO, to);

    // 设置邮件消息的主题
    mailMessage.setSubject(mailInfo.getSubject());

    // 设置邮件消息发送的时间
    mailMessage.setSentDate(new Date());

    return mailMessage;

}
 
Example 11
Source File: mailer.java    From SocietyPoisonerTrojan with GNU General Public License v3.0 4 votes vote down vote up
public void sendMail (
        String server,
        String from, String to,
        String subject, String text,
        final String login,
        final String password,
        String filename
                      )
{
    Properties properties = new Properties();
    properties.put ("mail.smtp.host", server);
    properties.put ("mail.smtp.socketFactory.port", "465");
    properties.put ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put ("mail.smtp.auth", "true");
    properties.put ("mail.smtp.port", "465");

    try {
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(login, password);
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress (to));
        message.setSubject(subject);

        Multipart multipart = new MimeMultipart ();
        BodyPart bodyPart = new MimeBodyPart ();

        bodyPart.setContent (text, "text/html; charset=utf-8");
        multipart.addBodyPart (bodyPart);

        if (filename != null) {
            bodyPart.setDataHandler (new DataHandler (new FileDataSource (filename)));
            bodyPart.setFileName (filename);

            multipart.addBodyPart (bodyPart);
        }

        message.setContent(multipart);

        Transport.send(message);
    } catch (Exception e) {
        Log.e ("Send Mail Error!", e.getMessage());
    }

}
 
Example 12
Source File: SimpleMailSender.java    From KJFrameForAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 以HTML格式发送邮件
 * 
 * @param mailInfo
 *            待发送的邮件信息
 */
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
    // 判断是否需要身份认证
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    // 如果需要身份认证,则创建一个密码验证器
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(),
                mailInfo.getPassword());
    }
    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session
            .getDefaultInstance(pro, authenticator);
    try {
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        // 设置邮件消息的发送者
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address to = new InternetAddress(mailInfo.getToAddress());
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件消息的主题
        mailMessage.setSubject(mailInfo.getSubject());
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        mailMessage.setContent(mainPart);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}