Search in sources :

Example 51 with Address

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"));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress)

Example 52 with Address

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;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) HashSet(java.util.HashSet)

Example 53 with 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);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) NoSuchMimePackageException(com.axway.ats.action.objects.model.NoSuchMimePackageException) PackageException(com.axway.ats.action.objects.model.PackageException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 54 with Address

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());
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage)

Example 55 with Address

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());
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Address (javax.mail.Address)89 InternetAddress (javax.mail.internet.InternetAddress)69 MessagingException (javax.mail.MessagingException)35 MimeMessage (javax.mail.internet.MimeMessage)34 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)21 ArrayList (java.util.ArrayList)20 Date (java.util.Date)19 MimeBodyPart (javax.mail.internet.MimeBodyPart)13 Account (com.zimbra.cs.account.Account)12 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)10 IOException (java.io.IOException)9 Locale (java.util.Locale)9 Message (javax.mail.Message)9 ItemId (com.zimbra.cs.service.util.ItemId)8 Header (javax.mail.Header)8 NHINDAddress (org.nhindirect.stagent.NHINDAddress)8 AddressException (javax.mail.internet.AddressException)7 MimeMultipart (javax.mail.internet.MimeMultipart)7 ZVCalendar (com.zimbra.common.calendar.ZCalendar.ZVCalendar)6 Invite (com.zimbra.cs.mailbox.calendar.Invite)6