diff --git a/feilong-net-mail/src/main/java/com/feilong/net/mail/DefaultMailSender.java b/feilong-net-mail/src/main/java/com/feilong/net/mail/DefaultMailSender.java index 8f52b17..1d81c46 100644 --- a/feilong-net-mail/src/main/java/com/feilong/net/mail/DefaultMailSender.java +++ b/feilong-net-mail/src/main/java/com/feilong/net/mail/DefaultMailSender.java @@ -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; @@ -246,7 +245,7 @@ private static void setMessageAttribute(Message message,MailSenderConfig mailSen // 设置邮件接受人群 // 支持 to cc bcc - setRecipients(message, mailSenderConfig); + RecipientsSetter.setRecipients(message, mailSenderConfig); //--------------------------------------------------------------- // 设置邮件消息的主题 @@ -255,39 +254,4 @@ private static void setMessageAttribute(Message message,MailSenderConfig mailSen //header信息 setHeaders(message, mailSenderConfig); } - - //--------------------------------------------------------------- - - /** - * 设置邮件接受人群. - * - *
- * 支持 to cc bcc. - *
- * - * @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())); - } - } - } \ No newline at end of file diff --git a/feilong-net-mail/src/main/java/com/feilong/net/mail/RecipientsSetter.java b/feilong-net-mail/src/main/java/com/feilong/net/mail/RecipientsSetter.java new file mode 100644 index 0000000..d9d9732 --- /dev/null +++ b/feilong-net-mail/src/main/java/com/feilong/net/mail/RecipientsSetter.java @@ -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 feilong + * @since 1.13.2 + */ +public final class RecipientsSetter{ + + /** + * 设置邮件接受人群. + * + *+ * 支持 to cc bcc. + *
+ * + * @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)); + } + } +} diff --git a/feilong-net-mail/src/test/java/com/feilong/net/mail/AbstractMailSenderTest.java b/feilong-net-mail/src/test/java/com/feilong/net/mail/AbstractMailSenderTest.java index 8a29a59..aa50377 100644 --- a/feilong-net-mail/src/test/java/com/feilong/net/mail/AbstractMailSenderTest.java +++ b/feilong-net-mail/src/test/java/com/feilong/net/mail/AbstractMailSenderTest.java @@ -28,16 +28,15 @@ import com.feilong.io.FileUtil; import com.feilong.net.mail.entity.MailSenderConfig; -/** - * The Class MailSenderTest. - * - * @author feilong - * @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; //--------------------------------------------------------------- @@ -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)); @@ -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"; } //--------------------------------------------------------------- diff --git a/feilong-net-mail/src/test/java/com/feilong/net/mail/Email1.java b/feilong-net-mail/src/test/java/com/feilong/net/mail/Email1.java index f965457..671bb86 100644 --- a/feilong-net-mail/src/test/java/com/feilong/net/mail/Email1.java +++ b/feilong-net-mail/src/test/java/com/feilong/net/mail/Email1.java @@ -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(){ diff --git a/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailTest.java b/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailCalendarTest.java similarity index 90% rename from feilong-net-mail/src/test/java/com/feilong/net/mail/EmailTest.java rename to feilong-net-mail/src/test/java/com/feilong/net/mail/EmailCalendarTest.java index 3ebeead..cc0491e 100644 --- a/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailTest.java +++ b/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailCalendarTest.java @@ -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{ @@ -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" + // @@ -75,6 +75,8 @@ public void testEmailTest() throws Exception{ "END:VEVENT\n" + // "END:VCALENDAR"); + //--------------------------------------------------------------- + // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); @@ -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(); @@ -92,6 +96,8 @@ public void testEmailTest() throws Exception{ // Put parts in message message.setContent(multipart); + //--------------------------------------------------------------- + // send message Transport.send(message); } diff --git a/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailTest5.java b/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailCalendarTest2.java similarity index 85% rename from feilong-net-mail/src/test/java/com/feilong/net/mail/EmailTest5.java rename to feilong-net-mail/src/test/java/com/feilong/net/mail/EmailCalendarTest2.java index 1e63bc7..3719cd0 100644 --- a/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailTest5.java +++ b/feilong-net-mail/src/test/java/com/feilong/net/mail/EmailCalendarTest2.java @@ -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. @@ -45,9 +42,10 @@ 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); @@ -55,23 +53,14 @@ public void send() throws Exception{ 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.