use of javax.mail.Address in project translationstudio8 by heartsome.
the class MailSender method setFrom.
/**
* 设置发件人
* @param from
* 发件人邮箱地址
* @throws MessagingException
* @throws EmailException
*/
public void setFrom(String from) throws MessagingException, EmailException {
if (from == null) {
return;
}
Address address = new InternetAddress(from);
Map<String, String> map = getEmailInfo(address.toString());
email.setFrom(map.get("email"), map.get("name"));
}
use of javax.mail.Address in project translationstudio8 by heartsome.
the class MailUtils method removeDuplicate.
/**
* 过滤邮件中的 From 和 To,使邮件不允许发件人和收件人一样.
* @param from
* 发件人数组
* @param to
* 收件人数组
* @return Address[]
* 收件人数组中过滤掉重复的发件人信息后剩余的集合。如果 from 为 null,返回 to;如果 to 为 null,返回 null
*/
public static Address[] removeDuplicate(Address[] from, Address[] to) {
if (from == null) {
return to;
}
if (to == null) {
return null;
}
Set<Address> fromSet = new HashSet<Address>();
Set<Address> toSet = new HashSet<Address>();
for (Address address : from) {
fromSet.add(address);
}
for (Address address : to) {
toSet.add(address);
}
toSet.removeAll(fromSet);
InternetAddress[] address = new InternetAddress[toSet.size()];
toSet.toArray(address);
return address;
}
use of javax.mail.Address in project ats-framework by Axway.
the class MimePackage method getSenderAddress.
/**
* This method resturns only the email address portion of the sender
* contained in the first From header
*
* @return the sender address
* @throws PackageException
*/
@PublicAtsApi
public String getSenderAddress() throws PackageException {
try {
Address[] fromAddresses = message.getFrom();
if (fromAddresses == null || fromAddresses.length == 0) {
throw new PackageException("Sender not present");
}
InternetAddress fromAddress = (InternetAddress) fromAddresses[0];
return fromAddress.getAddress();
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.Address in project spring-framework by spring-projects.
the class JavaMailSenderTests method testJavaMailSenderWithSimpleMessages.
public void testJavaMailSenderWithSimpleMessages() throws MessagingException, IOException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setUsername("username");
sender.setPassword("password");
SimpleMailMessage simpleMessage1 = new SimpleMailMessage();
simpleMessage1.setTo("he@mail.org");
SimpleMailMessage simpleMessage2 = new SimpleMailMessage();
simpleMessage2.setTo("she@mail.org");
sender.send(simpleMessage1, simpleMessage2);
assertEquals("host", sender.transport.getConnectedHost());
assertEquals("username", sender.transport.getConnectedUsername());
assertEquals("password", sender.transport.getConnectedPassword());
assertTrue(sender.transport.isCloseCalled());
assertEquals(2, sender.transport.getSentMessages().size());
MimeMessage sentMessage1 = sender.transport.getSentMessage(0);
List<Address> tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO));
assertEquals(1, tos1.size());
assertEquals("he@mail.org", ((InternetAddress) tos1.get(0)).getAddress());
MimeMessage sentMessage2 = sender.transport.getSentMessage(1);
List<Address> tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO));
assertEquals(1, tos2.size());
assertEquals("she@mail.org", ((InternetAddress) tos2.get(0)).getAddress());
}
use of javax.mail.Address in project spring-framework by spring-projects.
the class JavaMailSenderTests method javaMailSenderWithSimpleMessage.
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
MockJavaMailSender sender = new MockJavaMailSender();
sender.setHost("host");
sender.setPort(30);
sender.setUsername("username");
sender.setPassword("password");
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setFrom("me@mail.org");
simpleMessage.setReplyTo("reply@mail.org");
simpleMessage.setTo("you@mail.org");
simpleMessage.setCc(new String[] { "he@mail.org", "she@mail.org" });
simpleMessage.setBcc(new String[] { "us@mail.org", "them@mail.org" });
Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
simpleMessage.setSentDate(sentDate);
simpleMessage.setSubject("my subject");
simpleMessage.setText("my text");
sender.send(simpleMessage);
assertEquals("host", sender.transport.getConnectedHost());
assertEquals(30, sender.transport.getConnectedPort());
assertEquals("username", sender.transport.getConnectedUsername());
assertEquals("password", sender.transport.getConnectedPassword());
assertTrue(sender.transport.isCloseCalled());
assertEquals(1, sender.transport.getSentMessages().size());
MimeMessage sentMessage = sender.transport.getSentMessage(0);
List<Address> froms = Arrays.asList(sentMessage.getFrom());
assertEquals(1, froms.size());
assertEquals("me@mail.org", ((InternetAddress) froms.get(0)).getAddress());
List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
assertEquals("reply@mail.org", ((InternetAddress) replyTos.get(0)).getAddress());
List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
assertEquals(1, tos.size());
assertEquals("you@mail.org", ((InternetAddress) tos.get(0)).getAddress());
List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
assertEquals(2, ccs.size());
assertEquals("he@mail.org", ((InternetAddress) ccs.get(0)).getAddress());
assertEquals("she@mail.org", ((InternetAddress) ccs.get(1)).getAddress());
List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
assertEquals(2, bccs.size());
assertEquals("us@mail.org", ((InternetAddress) bccs.get(0)).getAddress());
assertEquals("them@mail.org", ((InternetAddress) bccs.get(1)).getAddress());
assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
assertEquals("my subject", sentMessage.getSubject());
assertEquals("my text", sentMessage.getContent());
}
Aggregations