当前位置: 首页>>代码示例>>Java>>正文


Java Message类代码示例

本文整理汇总了Java中javax.mail.Message的典型用法代码示例。如果您正苦于以下问题:Java Message类的具体用法?Java Message怎么用?Java Message使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Message类属于javax.mail包,在下文中一共展示了Message类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendMail

import javax.mail.Message; //导入依赖的package包/类
public static void sendMail(String host, int port, String username, String password, String recipients,
		String subject, String content, String from) throws AddressException, MessagingException {
	
	Properties props = new Properties();
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.starttls.enable", "true");
	props.put("mail.smtp.host", host);
	props.put("mail.smtp.port", port);

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

	Message message = new MimeMessage(session);
	message.setFrom(new InternetAddress(from));
	message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
	message.setSubject(subject);
	message.setText(content);

	Transport.send(message);
}
 
开发者ID:bndynet,项目名称:web-framework-for-java,代码行数:24,代码来源:MailHelper.java

示例2: getMessageContent

import javax.mail.Message; //导入依赖的package包/类
/**
 * Get the content of a mail message.
 * 
 * @param message
 *            the mail message
 * @return the content of the mail message
 */
private String getMessageContent(Message message) throws MessagingException {
    try {
        Object content = message.getContent();
        if (content instanceof Multipart) {
            StringBuffer messageContent = new StringBuffer();
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                Part part = multipart.getBodyPart(i);
                if (part.isMimeType("text/plain")) {
                    messageContent.append(part.getContent().toString());
                }
            }
            return messageContent.toString();
        }
        return content.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:29,代码来源:MailReader.java

示例3: sendTextMail

import javax.mail.Message; //导入依赖的package包/类
public boolean sendTextMail(MailSenderInfo mailInfo) {
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    try {
        Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
        mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());
        mailMessage.setText(mailInfo.getContent());
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
        return false;
    }
}
 
开发者ID:JamesLiAndroid,项目名称:AndroidKillerService,代码行数:21,代码来源:SimpleMailSender.java

示例4: sendEmail

import javax.mail.Message; //导入依赖的package包/类
@Override
public void sendEmail(String address, String subject, String content, Map<String, String> headers) throws Exception {
	Session session = MailUtilities.makeSession();
	if (session == null) {
		// LogService.getRoot().warning("Unable to create mail session. Not sending mail to "+address+".");
		LogService.getRoot().log(Level.WARNING, "com.rapidminer.tools.MailSenderSMTP.creating_mail_session_error",
				address);
	}
	MimeMessage msg = new MimeMessage(session);
	msg.setRecipients(Message.RecipientType.TO, address);
	msg.setFrom();
	msg.setSubject(subject, "UTF-8");
	msg.setSentDate(new Date());
	msg.setText(content, "UTF-8");

	if (headers != null) {
		for (Entry<String, String> header : headers.entrySet()) {
			msg.setHeader(header.getKey(), header.getValue());
		}
	}
	Transport.send(msg);
}
 
开发者ID:transwarpio,项目名称:rapidminer,代码行数:23,代码来源:MailSenderSMTP.java

示例5: send

import javax.mail.Message; //导入依赖的package包/类
public void send(String addresses, String topic, String textMessage) {

		try {
			session.getProperties().put("mail.smtp.port", 25000);
			Message message = new MimeMessage(session);
			message.setRecipients(TO, InternetAddress.parse(addresses));
			message.setSubject(topic);
			message.setText(textMessage);

			Transport.send(message);
			logger.info("sent mail with resource!");

		} catch (MessagingException e) {
			logger.log(WARNING, "Cannot send mail", e);
		}
	}
 
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:17,代码来源:SendMailWithResource.java

示例6: sendMail

import javax.mail.Message; //导入依赖的package包/类
/**
 * Sends an email.
 * 
 * @param from
 *            The Email-address from the sender of this mail.
 * @param to
 *            The Email-address of the receiver of this mail.
 * @param subject
 *            The subject of the mail.
 * @param body
 *            The content of the mail.
 * @throws MailSendingException
 *             When sending the mail failed.
 */
public void sendMail(final String from, final String to, final String subject, final String body)
        throws MailSendingException {
    final MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSentDate(new Date());
        message.setSubject(subject);
        message.setText(body);

        Transport.send(message);
    } catch (final MessagingException e) {
        throw new MailSendingException("Sending an email failed.", e);
    }

}
 
开发者ID:rbi,项目名称:trading4j,代码行数:31,代码来源:MailSender.java

示例7: sendAttachMail

import javax.mail.Message; //导入依赖的package包/类
public boolean sendAttachMail(MailSenderInfo mailInfo) {
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    try {
        Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
        mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());
        Multipart multi = new MimeMultipart();
        BodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        multi.addBodyPart(textBodyPart);
        for (String path : mailInfo.getAttachFileNames()) {
            DataSource fds = new FileDataSource(path);
            BodyPart fileBodyPart = new MimeBodyPart();
            fileBodyPart.setDataHandler(new DataHandler(fds));
            fileBodyPart.setFileName(path.substring(path.lastIndexOf("/") + 1));
            multi.addBodyPart(fileBodyPart);
        }
        mailMessage.setContent(multi);
        mailMessage.saveChanges();
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
        return false;
    }
}
 
开发者ID:JamesLiAndroid,项目名称:AndroidKillerService,代码行数:33,代码来源:SimpleMailSender.java

示例8: createFieldSearchTerm

import javax.mail.Message; //导入依赖的package包/类
private SearchTerm createFieldSearchTerm(String f) {
    switch (f.toLowerCase()) {
        case "from":
            return new FromStringTerm(matchingText);
        case "cc":
            return new RecipientStringTerm(Message.RecipientType.CC, matchingText);
        case "to":
            return new RecipientStringTerm(Message.RecipientType.TO, matchingText);
        case "body":
            return new BodyTerm(matchingText);
        case "subject":
            return new SubjectTerm(matchingText);
        default:
            return null;
    }
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Programming-Blueprints,代码行数:17,代码来源:Rule.java

示例9: moveMessage

import javax.mail.Message; //导入依赖的package包/类
private void moveMessage(Message toMove, Folder dest) {
    if (toMove != null) {
        try {
            final Folder source = toMove.getFolder();
            final Message[] messages = new Message[]{toMove};
            source.setFlags(messages, FLAGS_DELETED, true);
            source.copyMessages(messages, dest);
            moveCount++;
        } catch (MessagingException ex) {
            throw new RuntimeException(ex);
        }
    }
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Programming-Blueprints,代码行数:14,代码来源:AccountProcessor.java

示例10: prepareAndSendMail

import javax.mail.Message; //导入依赖的package包/类
private void prepareAndSendMail(final SimpleUserOrg user, final String password) {
	sendMail(mimeMessage -> {
		final String fullName = user.getFirstName() + " " + user.getLastName();
		final InternetAddress[] internetAddresses = getUserInternetAdresses(user, fullName);
		final String link = "<a href=\"" + configurationResource.get(URL_PUBLIC) + "\">"
				+ configurationResource.get(URL_PUBLIC) + "</a>";
		mimeMessage.setHeader("Content-Type", "text/plain; charset=UTF-8");
		mimeMessage.setFrom(new InternetAddress(configurationResource.get(MESSAGE_FROM),
				configurationResource.get(MESSAGE_FROM_TITLE), StandardCharsets.UTF_8.name()));
		mimeMessage.setSubject(String.format(configurationResource.get(MESSAGE_NEW_SUBJECT), fullName),
				StandardCharsets.UTF_8.name());
		mimeMessage.setRecipients(Message.RecipientType.TO, internetAddresses);
		mimeMessage.setContent(String.format(configurationResource.get(MESSAGE_NEW), fullName, user.getId(),
				password, link, fullName, user.getId(), password, link), "text/html; charset=UTF-8");
	});
}
 
开发者ID:ligoj,项目名称:plugin-password,代码行数:17,代码来源:PasswordResource.java

示例11: buildEmailMessage

import javax.mail.Message; //导入依赖的package包/类
private Message buildEmailMessage(EmailInfo emailInfo)
		throws AddressException, MessagingException, UnsupportedEncodingException {
	MimeMessage message = new MimeMessage(this.session);
	message.setFrom(new InternetAddress(emailInfo.getFrom(), "网页更新订阅系统", "UTF-8"));
	message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailInfo.getTo()));

	Multipart multipart = new MimeMultipart();
	BodyPart messageBodyPart = new MimeBodyPart();
	messageBodyPart.setContent(emailInfo.getContent(), "text/html;charset=UTF-8");
	multipart.addBodyPart(messageBodyPart);
	message.setContent(multipart);
	message.setSubject(emailInfo.getTitle());
	message.saveChanges();
	return message;
}
 
开发者ID:wrayzheng,项目名称:webpage-update-subscribe,代码行数:16,代码来源:EmailServer.java

示例12: getStringInfo

import javax.mail.Message; //导入依赖的package包/类
/**
 * Extracts string data from {@link MimeMessage}.
 *
 * @param mimeMessage
 * @return
 */
public static String getStringInfo(MimeMessage mimeMessage) {
	StringBuilder sb = new StringBuilder("MimeMessage: ");
	try {
		sb.append("from=").append(StringUtils.arrayToCommaDelimitedString(mimeMessage.getFrom())).append("; ");
		sb.append("replyTo=").append(StringUtils.arrayToCommaDelimitedString(mimeMessage.getReplyTo())).append("; ");
		sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(mimeMessage.getRecipients(Message.RecipientType.TO))).append("; ");
		sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(mimeMessage.getRecipients(Message.RecipientType.CC))).append("; ");
		sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(mimeMessage.getRecipients(Message.RecipientType.BCC))).append("; ");
		sb.append("subject=").append(mimeMessage.getSubject());
	} catch (MessagingException e) {
		LOG.error("Error retrieving log message: {}", e.getMessage());
	}

	return sb.toString();
}
 
开发者ID:mychalvlcek,项目名称:spring-rest-skeleton,代码行数:22,代码来源:LoggableMailSender.java

示例13: sendReport

import javax.mail.Message; //导入依赖的package包/类
@Test
public void sendReport() throws MessagingException {

    mockStatic(Transport.class);

    expect(ReportConfigurator.getInstance()).andReturn(mockReportConfigurator);
    expect(mockReportConfigurator.getSmtpServerName()).andReturn("localhost");
    expect(mockReportConfigurator.getSmtpServerPort()).andReturn("25");
    expect(mockReportConfigurator.getAddressesTo()).andReturn(new String[]{ "userTo" });
    expect(mockReportConfigurator.getAddressesCc()).andReturn(new String[0]);
    expect(mockReportConfigurator.getAddressesBcc()).andReturn(new String[0]);
    expect(mockReportConfigurator.getAddressFrom()).andReturn("userFrom");

    Transport.send(isA(Message.class));

    replayAll();

    triggerRun();

    verifyAll();
}
 
开发者ID:Axway,项目名称:ats-framework,代码行数:22,代码来源:Test_MailReportSender.java

示例14: sendEmailWithOrder

import javax.mail.Message; //导入依赖的package包/类
public static void sendEmailWithOrder(String text, String eMail, HttpServletRequest request) {
    try {
        Session session = EmailActions.authorizeWebShopEmail();

        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(ApplicationProperties.SHOP_EMAIL));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(eMail, false));
        msg.setSubject("Shop order");
        msg.setText(text);
        msg.setSentDate(new Date());
        Transport.send(msg);
    } catch (MessagingException e) {
        System.out.println("Error : " + e);
    }
}
 
开发者ID:xSzymo,项目名称:Spring-web-shop-project,代码行数:16,代码来源:SendEmailUserAccount.java

示例15: sendMessage

import javax.mail.Message; //导入依赖的package包/类
@Override
public void sendMessage(Message msg, Address[] addresses)
        throws MessagingException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        msg.writeTo(out);
        lastMail = new String(out.toByteArray(), "UTF-8");
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:12,代码来源:Transport.java


注:本文中的javax.mail.Message类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。