Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
新建 RecipientsSetter fix #45
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Apr 25, 2019
1 parent 2a410e4 commit 519b8db
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.feilong.net.mail;

import static com.feilong.core.Validator.isNotNullOrEmpty;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -246,7 +245,7 @@ private static void setMessageAttribute(Message message,MailSenderConfig mailSen

// 设置邮件接受人群
// 支持 to cc bcc
setRecipients(message, mailSenderConfig);
RecipientsSetter.setRecipients(message, mailSenderConfig);

//---------------------------------------------------------------
// 设置邮件消息的主题
Expand All @@ -255,39 +254,4 @@ private static void setMessageAttribute(Message message,MailSenderConfig mailSen
//header信息
setHeaders(message, mailSenderConfig);
}

//---------------------------------------------------------------

/**
* 设置邮件接受人群.
*
* <p>
* 支持 to cc bcc.
* </p>
*
* @param message
* the message
* @param mailSenderConfig
* the new recipients
* @throws MessagingException
* the messaging exception
*/
static void setRecipients(Message message,MailSenderConfig mailSenderConfig) throws MessagingException{
// 创建邮件的接收者地址,并设置到邮件消息中
// Message.RecipientType.TO属性表示接收者的类型为TO
if (isNotNullOrEmpty(mailSenderConfig.getTos())){
message.setRecipients(Message.RecipientType.TO, InternetAddressUtil.toAddressArray(mailSenderConfig.getTos()));
}

//cc 抄送
if (isNotNullOrEmpty(mailSenderConfig.getCcs())){
message.setRecipients(Message.RecipientType.CC, InternetAddressUtil.toAddressArray(mailSenderConfig.getCcs()));
}

//bcc 密送
if (isNotNullOrEmpty(mailSenderConfig.getBccs())){
message.setRecipients(Message.RecipientType.BCC, InternetAddressUtil.toAddressArray(mailSenderConfig.getBccs()));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.net.mail;

import static com.feilong.core.Validator.isNotNullOrEmpty;

import javax.mail.Message;
import javax.mail.MessagingException;

import com.feilong.net.mail.entity.MailSenderConfig;
import com.feilong.net.mail.util.InternetAddressUtil;

/**
* The Class RecipientsSetter.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 1.13.2
*/
public final class RecipientsSetter{

/**
* 设置邮件接受人群.
*
* <p>
* 支持 to cc bcc.
* </p>
*
* @param message
* the message
* @param mailSenderConfig
* the new recipients
* @throws MessagingException
* the messaging exception
*/
public static void setRecipients(Message message,MailSenderConfig mailSenderConfig) throws MessagingException{
// 创建邮件的接收者地址,并设置到邮件消息中
// Message.RecipientType.TO属性表示接收者的类型为TO
String[] tos = mailSenderConfig.getTos();
if (isNotNullOrEmpty(tos)){
message.setRecipients(Message.RecipientType.TO, InternetAddressUtil.toAddressArray(tos));
}

//---------------------------------------------------------------

//cc 抄送
String[] ccs = mailSenderConfig.getCcs();
if (isNotNullOrEmpty(ccs)){
message.setRecipients(Message.RecipientType.CC, InternetAddressUtil.toAddressArray(ccs));
}

//---------------------------------------------------------------

//bcc 密送
String[] bccs = mailSenderConfig.getBccs();
if (isNotNullOrEmpty(bccs)){
message.setRecipients(Message.RecipientType.BCC, InternetAddressUtil.toAddressArray(bccs));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
import com.feilong.io.FileUtil;
import com.feilong.net.mail.entity.MailSenderConfig;

/**
* The Class MailSenderTest.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 1.0.9
*/
public abstract class AbstractMailSenderTest{

/** The Constant folder. */
private static final String folder = "/Users/feilong/Development/DataCommon/Files/Java/config/";

//---------------------------------------------------------------

/** The mail sender config. */
protected MailSenderConfig mailSenderConfig;
protected MailSenderConfig mailSenderConfig;

//---------------------------------------------------------------

Expand All @@ -57,14 +56,16 @@ public void before(){
* @since 1.10.0
*/
private void loadMailSenderConfig(){
mailSenderConfig = new MailSenderConfig();
ResourceBundle resourceBundle = ResourceBundleUtil.getResourceBundle(FileUtil.getFileInputStream(getConfigFile()));
ResourceBundle resourceBundle = ResourceBundleUtil.getResourceBundle(FileUtil.getFileInputStream(folder + getConfigFile()));

ArrayConverter arrayConverter = new ArrayConverter(String[].class, new StringConverter(), 2);
char[] allowedChars = { '@' };
arrayConverter.setAllowedChars(allowedChars);
ConvertUtils.register(arrayConverter, String[].class);

//---------------------------------------------------------------

mailSenderConfig = new MailSenderConfig();
mailSenderConfig = BeanUtil.populate(mailSenderConfig, ResourceBundleUtil.toMap(resourceBundle));

//LOGGER.debug(JsonUtil.format(mailSenderConfig));
Expand All @@ -78,7 +79,7 @@ private void loadMailSenderConfig(){
* @return the config file
*/
protected String getConfigFile(){
return "/Users/feilong/Development/DataCommon/Files/Java/config/mail-feilongtestemail.properties";
return "mail-feilongtestemail.properties";
}

//---------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ static BodyPart build(MailSenderConfig mailSenderConfig) throws MessagingExcepti
return messageBodyPart;
}

//---------------------------------------------------------------

@Override
protected String getConfigFile(){
return "/Users/feilong/Development/DataCommon/Files/Java/config/mail-baozun.properties";
return "mail-baozun.properties";
}

//---------------------------------------------------------------

@Override
@After
public void after(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

import org.junit.Test;

import com.feilong.net.mail.SessionFactory;

public class EmailTest extends AbstractMailSenderTest{
public class EmailCalendarTest extends AbstractMailSenderTest{

@Test
public void testEmailTest() throws Exception{
Expand All @@ -39,6 +37,8 @@ public void testEmailTest() throws Exception{
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Outlook Meeting Request Using JavaMail");

//---------------------------------------------------------------

StringBuffer sb = new StringBuffer();

StringBuffer buffer = sb.append("BEGIN:VCALENDAR\n" + //
Expand Down Expand Up @@ -75,6 +75,8 @@ public void testEmailTest() throws Exception{
"END:VEVENT\n" + //
"END:VCALENDAR");

//---------------------------------------------------------------

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

Expand All @@ -83,6 +85,8 @@ public void testEmailTest() throws Exception{
messageBodyPart.setHeader("Content-ID", "calendar_message");
messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(buffer.toString(), "text/calendar")));// very important

//---------------------------------------------------------------

// Create a Multipart
Multipart multipart = new MimeMultipart();

Expand All @@ -92,6 +96,8 @@ public void testEmailTest() throws Exception{
// Put parts in message
message.setContent(multipart);

//---------------------------------------------------------------

// send message
Transport.send(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import org.junit.After;
import org.junit.Test;

/**
* The Class EmailTest5.
*/
public class EmailTest5 extends AbstractMailSenderTest{
public class EmailCalendarTest2 extends AbstractMailSenderTest{

/**
* Send.
Expand All @@ -45,33 +42,25 @@ public class EmailTest5 extends AbstractMailSenderTest{
*/
@Test
public void send() throws Exception{

mailSenderConfig.setContent("hello hahaha");

//---------------------------------------------------------------

String fromEmail = "feilongtestemail@163.com";
String toEmail = "xin.jin@baozun.cn";
Session session = SessionFactory.createSession(mailSenderConfig);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setSubject("Outlook Meeting Request Using JavaMail");
StringBuffer buffer = new StringBuffer();
buffer.append(
"BEGIN:VCALENDAR\n" + "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" + "VERSION:2.0\n"
+ "METHOD:REQUEST\n" + "BEGIN:VEVENT\n" + "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"
+ toEmail + "\n" + "ORGANIZER:MAILTO:" + toEmail + "\n" + "DTSTART:20180302T060000Z\n"
+ "DTEND:20180302T070000Z\n" + "LOCATION:Conference room\n" + "UID:" + UUID.randomUUID().toString()
+ "\n"//如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。
+ "CATEGORIES:SuccessCentral Reminder\n"
+ "DESCRIPTION:This the description of the meeting.<br>asd;flkjasdpfi\n\n"
+ "SUMMARY:Test meeting request\n" + "PRIORITY:5\n" + "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
+ "TRIGGER:-PT15M\n" + "ACTION:DISPLAY\n" + "DESCRIPTION:Reminder\n" + "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR");

//---------------------------------------------------------------

BodyPart messageBodyPart = new MimeBodyPart();
// 测试下来如果不这么转换的话,会以纯文本的形式发送过去,
//如果没有method=REQUEST;charset=\"UTF-8\",outlook会议附件的形式存在,而不是直接打开就是一个会议请求
messageBodyPart.setDataHandler(
new DataHandler(new ByteArrayDataSource(buffer.toString(), "text/calendar;method=REQUEST;charset=\"UTF-8\"")));
new DataHandler(new ByteArrayDataSource(buildContent(toEmail), "text/calendar;method=REQUEST;charset=\"UTF-8\"")));

Multipart multipart = new MimeMultipart();

Expand All @@ -86,9 +75,36 @@ public void send() throws Exception{

message.setContent(multipart);

//---------------------------------------------------------------

Transport.send(message);
}

//---------------------------------------------------------------

/**
* @param toEmail
* @return
* @since 1.13.2
*/
private String buildContent(String toEmail){
StringBuffer buffer = new StringBuffer();
buffer.append(
"BEGIN:VCALENDAR\n" + "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" + "VERSION:2.0\n"
+ "METHOD:REQUEST\n" + "BEGIN:VEVENT\n" + "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"
+ toEmail + "\n" + "ORGANIZER:MAILTO:" + toEmail + "\n" + "DTSTART:20180302T060000Z\n"
+ "DTEND:20180302T070000Z\n" + "LOCATION:Conference room\n" + "UID:" + UUID.randomUUID().toString()
+ "\n"//如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。
+ "CATEGORIES:SuccessCentral Reminder\n"
+ "DESCRIPTION:This the description of the meeting.<br>asd;flkjasdpfi\n\n"
+ "SUMMARY:Test meeting request\n" + "PRIORITY:5\n" + "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
+ "TRIGGER:-PT15M\n" + "ACTION:DISPLAY\n" + "DESCRIPTION:Reminder\n" + "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR");
return buffer.toString();
}

//---------------------------------------------------------------

/*
* (non-Javadoc)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,24 @@

import com.feilong.velocity.VelocityUtil;

/**
* The Class MailSenderTest.
*
* @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
* @since 1.0.9
*/
public class HelloWorldMailSenderTest extends AbstractMailSenderTest{

@Test
public void helloWorld(){
Map<String, Object> map = newHashMap();
String parseTemplateWithClasspathResourceLoader = VelocityUtil.INSTANCE
.parseTemplateWithClasspathResourceLoader("hello world.vm", map);

String content = VelocityUtil.INSTANCE.parseTemplateWithClasspathResourceLoader("hello world.vm", map);

mailSenderConfig.setSubject("hello world");
mailSenderConfig.setContent(parseTemplateWithClasspathResourceLoader);
mailSenderConfig.setContent(content);
}

/*
* (non-Javadoc)
*
* @see com.feilong.tools.mail.BaseMailSenderTest#getConfigFile()
*/
//---------------------------------------------------------------

@Override
protected String getConfigFile(){
String folder = "/Users/feilong/Development/DataCommon/Files/Java/config";

String file = "";
// file ="/mail-adidas-test.properties";
//file = "/mail-bztest-test.properties";
file = "/mail-feilongtestemail.properties";

return folder + file;
//return "mail-adidas-test.properties";
//return "mail-bztest-test.properties";
return "mail-feilongtestemail.properties";
}
}
Loading

0 comments on commit 519b8db

Please sign in to comment.