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


Java MimeMessage类代码示例

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


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

示例1: sendMail

import javax.mail.internet.MimeMessage; //导入依赖的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: sendEmail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
@Override
public void sendEmail(final UserDTO user, String url) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(user.getEmail());
            message.setSubject(SUBJECT);
            message.setFrom(EMAIL_FROM); // could be parameterized...
            Map model = new HashMap();
            model.put("user", user);
            model.put("url", url);
            String text = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "org/enricogiurin/sushibar/registration-confirmation.vm", model);
            message.setText(text, true);
        }
    };
    this.emailSender.send(preparator);
}
 
开发者ID:egch,项目名称:sushi-bar-BE,代码行数:19,代码来源:EmailSenderImpl.java

示例3: sendCodeMail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
/**
 * 发送邮件,从公邮里发邮件给成员变量email。
 *
 * @throws Exception 可能会有异常抛出,建议打出Log。
 */
public void sendCodeMail() throws MessagingException, UnsupportedEncodingException {
    if (email == null) {
        Log.e("sendCodeMail","调用错误");
        return;
    }
    //创建一封邮件
    MimeMessage message = createCodeMessage(session, myEmailAccount, email);

    Transport transport = session.getTransport();

    //使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
    transport.connect(myEmailAccount, myEmailPassword);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
}
 
开发者ID:GrayXu,项目名称:HustEating,代码行数:21,代码来源:Mail.java

示例4: toJsonAsStringShouldReturnTextBodyWhenMultipartAndTextHtml

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
@Test
public void toJsonAsStringShouldReturnTextBodyWhenMultipartAndTextHtml() throws Exception {
    MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
        .setMultipartWithBodyParts(
            MimeMessageBuilder.bodyPartBuilder()
                .data("<p>this is my body</p>")
                .type("text/html")
                .build())
        .addHeader("Date", "Wed, 24 May 2017 06:23:11 -0700")
        .build();

    FakeMail mail = FakeMail.from(message);
    
    ClassificationRequestBodySerializer testee = new ClassificationRequestBodySerializer(mail, new FakeUUIDGenerator());
    String jsonAsString = testee.toJsonAsString();

    assertThatJson(jsonAsString)
        .isEqualTo("{\"messageId\":\"524e4f85-2d2f-4927-ab98-bd7a2f689773\"," +
            "\"from\":[]," +
            "\"recipients\":{\"to\":[],\"cc\":[],\"bcc\":[]}," +
            "\"subject\":[\"\"]," +
            "\"date\":\"2017-05-24T13:23:11Z\"," +
            "\"textBody\":\"this is my body\\n\\n\"}");
}
 
开发者ID:linagora,项目名称:openpaas-mailets,代码行数:25,代码来源:ClassificationRequestBodySerializerTest.java

示例5: testSendMultipartHtmlEmail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject","testContent", true, true);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart)((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
开发者ID:oktadeveloper,项目名称:jhipster-microservices-example,代码行数:17,代码来源:MailServiceIntTest.java

示例6: sendMailBatch

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
public void sendMailBatch(List<String> to, String title, String content) throws MessagingException {
    Session session = Session.getDefaultInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(fromAddress));
    to.forEach(address -> {
        try {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
        } catch (MessagingException ignored) {
        }
    });
    message.setSubject(title);
    message.setContent(content, "text/html;charset=UTF-8");
    Transport.send(message);
}
 
开发者ID:superkoh,项目名称:k-framework,代码行数:20,代码来源:JavaMailMessage.java

示例7: sendMailWithNewPassword

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
/**
 * {@inheritDoc}
 */
@Async
@Override
public void sendMailWithNewPassword(
        @NotBlank @Email final String email,
        @NotBlank final String newPassword
) {
    log.info("Called with e-mail {}, newPassword {}", email, newPassword);

    try {
        final JavaMailSenderImpl sender = new JavaMailSenderImpl();

        final MimeMessage message = sender.createMimeMessage();

        final MimeMessageHelper helper = new MimeMessageHelper(message);

        helper.setTo(email);
        helper.setSubject("Recover password");
        helper.setText("Your new password: " + "<b>" + newPassword + "</b>", true);

        sendMail(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}
 
开发者ID:JonkiPro,项目名称:REST-Web-Services,代码行数:28,代码来源:MailServiceImpl.java

示例8: shouldSendRemindEmail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
@Test
public void shouldSendRemindEmail() throws MessagingException, IOException {

	final String subject = "subject";
	final String text = "text";

	Recipient recipient = new Recipient();
	recipient.setAccountName("test");
	recipient.setEmail("[email protected]");

	when(env.getProperty(NotificationType.REMIND.getSubject())).thenReturn(subject);
	when(env.getProperty(NotificationType.REMIND.getText())).thenReturn(text);

	emailService.send(NotificationType.REMIND, recipient, null);

	verify(mailSender).send(captor.capture());

	MimeMessage message = captor.getValue();
	assertEquals(subject, message.getSubject());
	// TODO check other fields
}
 
开发者ID:sniperqpc,项目名称:Spring-cloud-gather,代码行数:22,代码来源:EmailServiceImplTest.java

示例9: testAbortPrivateExam

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
@Test
@RunAsStudent
public void testAbortPrivateExam() throws Exception {
    Exam studentExam = createPrivateStudentExam();
    Result result = request(Helpers.PUT, String.format("/app/student/exam/abort/%s", studentExam.getHash()), null);
    assertThat(result.status()).isEqualTo(200);

    // Check that correct mail was sent
    assertThat(greenMail.waitForIncomingEmail(MAIL_TIMEOUT, 1)).isTrue();
    MimeMessage[] mails = greenMail.getReceivedMessages();
    assertThat(mails).hasSize(1);
    assertThat(mails[0].getFrom()[0].toString()).contains(ConfigFactory.load().getString("sitnet.email.system.account"));
    assertThat(mails[0].getSubject()).isEqualTo("Personal exam has been abandoned");
    String body = GreenMailUtil.getBody(mails[0]);
    // Make sure there is no link to review
    assertThat(body).doesNotContain("<a href");
}
 
开发者ID:CSCfi,项目名称:exam,代码行数:18,代码来源:StudentExamControllerTest.java

示例10: createMimeMessage

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
/**
 * 创建一封只包含文本的简单邮件
 *
 * @param session
 *            和服务器交互的会话
 * @param sendMail
 *            发件人邮箱
 * @param receiveMail
 *            收件人邮箱
 * @return
 * @throws Exception
 */
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String username,String pwd)
		throws Exception {
	// 1. 创建一封邮件
	MimeMessage message = new MimeMessage(session);

	// 2. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)
	message.setFrom(new InternetAddress(sendMail, "Mircle", "UTF-8"));

	// 3. To: 收件人(可以增加多个收件人、抄送、密送)
	message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "", "UTF-8"));

	// 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
	message.setSubject(subject, "UTF-8");

	// 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)
	message.setContent(getContent(username,pwd), "text/html;charset=UTF-8");

	// 6. 设置发件时间
	message.setSentDate(new Date());

	// 7. 保存设置
	message.saveChanges();

	return message;
}
 
开发者ID:miracle857,项目名称:weibo,代码行数:38,代码来源:Mail.java

示例11: sendAccountActivateEmail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
public static void sendAccountActivateEmail(String email, String uuid, int userId) {
  Session session;
  try {
    session = getSession();
    MimeMessage message = new MimeMessage(session);
    message.setSubject("������֤��");
    message.setSentDate(new Date());
    message.setFrom(new InternetAddress(FROM));
    message.setRecipient(RecipientType.TO, new InternetAddress(email));
    message.setContent("�����û�"+userId+":������֤��Ϊ��"+uuid,
        "text/html;charset=utf-8");
    message.saveChanges();
    Transport.send(message);
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
开发者ID:Luodian,项目名称:Higher-Cloud-Computing-Project,代码行数:18,代码来源:EmailUtils.java

示例12: buildMimeMessage

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
public MimeMessage buildMimeMessage(PlainTextMail data) {
    requireNonNull(data, "data");
    
    MimeMailMessage builder = mimeBuilder();
    builder.setFrom(config.fromAddress().get());
    builder.setTo(data.getRecipient().get());
    builder.setSubject(data.getSubject());
    builder.setText(data.getContent());
    
    return builder.getMimeMessage();
}
 
开发者ID:openmicroscopy,项目名称:omero-ms-queue,代码行数:12,代码来源:MessageBuilder.java

示例13: createMimeMessage

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
/**
 *
 * @param session 和服务器交互的会话
 * @param mail 邮件内容
 * @return
 * @throws Exception
 */
private static MimeMessage createMimeMessage(Session session, String SendAccount, Mail mail) throws Exception {
    MimeMessage message = new MimeMessage(session);
    //From: 发件人
    message.setFrom(new InternetAddress(SendAccount, mail.getPersonal(), "UTF-8"));
    // To: 收件人
    message.setRecipients(MimeMessage.RecipientType.TO, mail.getAddresses());
    // Subject: 邮件主题
    message.setSubject(mail.getSubject(), "UTF-8");
    // Content: 邮件正文(可以使用html标签)
    message.setContent(mail.getContext(), "text/html;charset=UTF-8");
    // 设置发件时间
    message.setSentDate(new Date());
    message.saveChanges();
    return message;
}
 
开发者ID:BetaSummer,项目名称:sztw,代码行数:23,代码来源:MailCore.java

示例14: shouldSendBackupEmail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
@Test
public void shouldSendBackupEmail() throws MessagingException, IOException {

	final String subject = "subject";
	final String text = "text";
	final String attachment = "attachment.json";

	Recipient recipient = new Recipient();
	recipient.setAccountName("test");
	recipient.setEmail("[email protected]");

	when(env.getProperty(NotificationType.BACKUP.getSubject())).thenReturn(subject);
	when(env.getProperty(NotificationType.BACKUP.getText())).thenReturn(text);
	when(env.getProperty(NotificationType.BACKUP.getAttachment())).thenReturn(attachment);

	emailService.send(NotificationType.BACKUP, recipient, "{\"name\":\"test\"");

	verify(mailSender).send(captor.capture());

	MimeMessage message = captor.getValue();
	assertEquals(subject, message.getSubject());
	// TODO check other fields
}
 
开发者ID:sniperqpc,项目名称:Spring-cloud-gather,代码行数:24,代码来源:EmailServiceImplTest.java

示例15: sendMail

import javax.mail.internet.MimeMessage; //导入依赖的package包/类
private void sendMail(SendMailActionMsg msg) throws MessagingException {
  log.debug("Sending mail {}", msg);
  MimeMessage mailMsg = mailSender.createMimeMessage();
  MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8");
  helper.setFrom(msg.getFrom());
  helper.setTo(msg.getTo());
  if (!StringUtils.isEmpty(msg.getCc())) {
    helper.setCc(msg.getCc());
  }
  if (!StringUtils.isEmpty(msg.getBcc())) {
    helper.setBcc(msg.getBcc());
  }
  helper.setSubject(msg.getSubject());
  helper.setText(msg.getBody());
  mailSender.send(helper.getMimeMessage());
  log.debug("Mail sent {}", msg);
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:18,代码来源:MailPlugin.java


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