Java Code Examples for javax.mail.internet.InternetAddress#setAddress()

The following examples show how to use javax.mail.internet.InternetAddress#setAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MailService.java    From ml-blog with MIT License 6 votes vote down vote up
@Async
public void sendEmail(String toEmail,String subject, String content) throws Exception {
    Context con = new Context();
    con.setVariable("content", content);
    String emailtext = templateEngine.process("portal/mail.html", con);

    MimeMessage message = this.javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    InternetAddress from = new InternetAddress();
    from.setAddress(this.mailProperties.getUsername());
    from.setPersonal(commonMap.get("blogName").toString(), "UTF-8");
    helper.setFrom(from);
    helper.setTo(toEmail);
    helper.setSubject(subject);
    helper.setText(emailtext, true);
    this.javaMailSender.send(message);

    log.info("发送邮件内容:{}",content);
}
 
Example 2
Source File: EmailInfoServiceImpl.java    From roncoo-adminlte-springmvc with Apache License 2.0 6 votes vote down vote up
public void send(String fromUser, String to, String subject, String text) {
	try {
		MimeMessage message = javaMailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		InternetAddress from = new InternetAddress();
		from.setAddress(fromUser);
		from.setPersonal(NAME, "UTF-8");
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(text, true);
		javaMailSender.send(message);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 3
Source File: MailAddress.java    From greenmail with Apache License 2.0 6 votes vote down vote up
public MailAddress(String str)
        throws AddressException {

    // Decoding the mail address in
    // case it contains non us-ascii characters
    String decoded = decodeStr(str);

    InternetAddress address = new InternetAddress();
    address.setAddress(decoded);
    email = address.getAddress();
    name = address.getPersonal();

    String[] strs = email.split("@");
    user = strs[0];
    if (strs.length > 1) {
        host = strs[1];
    } else {
        host = "localhost";
    }
}
 
Example 4
Source File: SendReceiveWithInternationalAddressTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
private void setAddressHeader(String name, InternetAddress[] addresses)
        throws MessagingException {

    try {

        // Encoding the email addresses so they are correctly sent to the mail server.
        for (int i = 0; i < addresses.length; i++) {
            String addStr = MimeUtility.encodeText(addresses[i].getAddress());
            InternetAddress ia = new InternetAddress();
            ia.setAddress(addStr);
            addresses[i] = ia;
        }

        String s = InternetAddress.toString(addresses, name.length() + 2);
        setHeader(name, s);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: MessageHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private Address[] getAddressHeader(String name) throws MessagingException {
    ensureMessage(false);

    String header = imessage.getHeader(name, ",");
    if (header == null)
        return null;

    header = fixEncoding(name, header);
    header = header.replaceAll("\\?=[\\r\\n\\t ]+=\\?", "\\?==\\?");
    Address[] addresses = InternetAddress.parseHeader(header, false);

    for (Address address : addresses) {
        InternetAddress iaddress = (InternetAddress) address;
        String email = iaddress.getAddress();
        String personal = iaddress.getPersonal();

        email = decodeMime(email);
        if (!Helper.isSingleScript(email))
            email = punyCode(email);

        iaddress.setAddress(email);

        if (personal != null) {
            try {
                iaddress.setPersonal(decodeMime(personal));
            } catch (UnsupportedEncodingException ex) {
                Log.w(ex);
            }
        }
    }

    return addresses;
}
 
Example 6
Source File: MimePackage.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Set the sender (From header) for the package
 *
 * @param sender
 *            the sender email address
 * @throws PackageException
 */
@PublicAtsApi
public void setSender(
                       String sender ) throws PackageException {

    String newSenderAddress;
    String newSenderPersonal;
    try {
        InternetAddress address = new InternetAddress();

        sender = sender.replaceAll("[<>]", "").trim();

        boolean hasPersonal = sender.contains(" ");
        if (hasPersonal) {
            newSenderAddress = sender.substring(sender.lastIndexOf(' '));
            newSenderPersonal = sender.substring(0, sender.lastIndexOf(' '));
            address.setPersonal(newSenderPersonal.trim());
        } else {
            newSenderAddress = sender;
        }

        // set the sender address
        address.setAddress(newSenderAddress.trim());

        message.setFrom(address);

    } catch (ArrayIndexOutOfBoundsException aioobe) {
        throw new PackageException("Sender not present");
    } catch (MessagingException me) {
        throw new PackageException(me);
    } catch (UnsupportedEncodingException uee) {
        throw new PackageException("Error setting address personal", uee);
    }
}
 
Example 7
Source File: DefaultMailService.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private Message createMessage ( final String to, final String subject ) throws MessagingException, AddressException
{
    final MimeMessage message = new MimeMessage ( this.session );

    final String from = getString ( "from" );
    if ( from != null )
    {
        message.setFrom ( new InternetAddress ( from ) );
    }
    else
    {
        message.setFrom ();
    }

    // recipient

    final InternetAddress recipient = new InternetAddress ();
    recipient.setAddress ( to );
    message.setRecipient ( javax.mail.Message.RecipientType.TO, recipient );

    // mail

    final String prefix = getString ( "prefix" );
    if ( prefix != null )
    {
        message.setSubject ( prefix + " " + subject );
    }
    else
    {
        message.setSubject ( subject );
    }

    message.setHeader ( "Return-Path", "<>" );

    return message;
}
 
Example 8
Source File: SendReceiveWithInternationalAddressTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Override
public void setRecipients(Message.RecipientType type, String addresses) throws MessagingException {

    InternetAddress address = new InternetAddress();
    address.setAddress(addresses);
    setAddressHeader(getHeaderName(type), new InternetAddress[]{address});
}
 
Example 9
Source File: SendReceiveWithInternationalAddressTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Override
public void setFrom(String address) throws MessagingException {

    InternetAddress internetAddress = new InternetAddress();
    internetAddress.setAddress(address);
    setAddressHeader("From", new InternetAddress[]{internetAddress});
}