本文整理匯總了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);
}
示例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);
}
示例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();
}
示例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\"}");
}
示例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");
}
示例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);
}
示例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();
}
}
示例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
}
示例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");
}
示例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;
}
示例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();
}
}
示例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();
}
示例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;
}
示例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
}
示例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);
}