props.put("mail.smtp.from", "replyto@example.com"); Session session = Session.getDefaultInstance(props, null); MimeMessage m = new MimeMessage(session); m.addFrom(InternetAddress.parse("from@example.com"));
@Override public void sendMessage(String email, MessageType messageType, String subject, String htmlContent) { MimeMessage message = mailSender.createMimeMessage(); try { message.addFrom(getSenderAddresses()); message.addRecipients(Message.RecipientType.TO, email); message.setSubject(subject); message.setContent(htmlContent, "text/html"); } catch (MessagingException e) { logger.error("Exception raised while sending message to " + email, e); } catch (UnsupportedEncodingException e) { logger.error("Exception raised while sending message to " + email, e); } mailSender.send(message); } }
MimeMessage msg = mailSender.createMimeMessage(); msg.addRecipient(RecipientType.TO, new InternetAddress(adminEmail)); msg.addFrom(new InternetAddress[] { new InternetAddress(adminEmail) }); msg.setSubject(subject, "UTF-8"); msg.setText(message, "UTF-8"); mailSender.send(msg);
message.addFrom(getAddresses(FROM));
message.addFrom(getAddresses(FROM));
/** * @see javax.mail.Message#addFrom(javax.mail.Address[]) */ public void addFrom(Address[] addresses) throws MessagingException { getWrappedMessageForWriting().addFrom(addresses); }
/** * Adding one from-address to the EmailMessage. * * @param internetAddress * The InternetAddress-object. * @throws MessagingException * is thrown if the underlying implementation does not support modification of * existing values */ public void addFrom(final Address internetAddress) throws MessagingException { super.addFrom(EmailExtensions.getAddressArray(internetAddress)); }
Properties properties = new Properties(); props.put("mail.smtp.from", "abc@gmail.com"); Session session = Session.getDefaultInstance(props, null); MimeMessage m = new MimeMessage(session); m.addFrom(InternetAddress.parse("xyz@yahoo.com")); m.setReplyTo(InternetAddress.parse("xyz@yahoo.com"));
@Override public void addFrom(Address[] addresses) throws MessagingException { super.addFrom(encodeAddresses(addresses)); }
try { MimeMessage message = new MimeMessage(mMailSession); message.setSubject(pSubject); MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent(pContent, "text/html"); Multipart mp = new MimeMultipart(); mp.addBodyPart(textPart); message.setContent(mp); message.addFrom(new Address[] { new InternetAddress(pFrom) }); for (int i = 0; i < pTo.length; i++) { String tTo = pTo[i]; message.addRecipient(Message.RecipientType.TO, new InternetAddress(tTo)); } mTransport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); } catch (MessagingException me) { throw new MailException(me); }
MimeMessage msg = new MimeMessage(session); Address[] from = InternetAddress.parse("me@localhost"); Address[] to = InternetAddress.parse("me@localhost"); msg.addFrom(from); msg.setRecipients(Message.RecipientType.TO, to); ByteArrayOutputStream out = new ByteArrayOutputStream(384); msg.saveChanges(); try { msg.writeTo(out); throw new AssertionError(); } catch (MessagingException | IOException test) { try { Transport.send(msg); throw new AssertionError(); } catch (MessagingException | IOException expect) { if (!exceptionEqual(test, expect)) { //Notify the user.... } } }
public class DomainMatch { public static void main(String[] args) throws Exception { MimeMessage msg = new MimeMessage((Session) null); msg.addFrom(InternetAddress.parse("foo@bar.org")); msg.saveChanges(); System.out.println(new FromStringTerm("@bar.org").match(msg)); System.out.println(new FromStringTerm("@spam.org").match(msg)); } }
public static void main(String[] args) throws Exception { Properties props = new Properties(); props.put("mail.from", "------@bar.baz"); Session s = Session.getInstance(props); MimeMessage m = new MimeMessage(s); m.addFrom(InternetAddress.parse("foo@bar.baz")); m.setText(""); m.saveChanges(); m.writeTo(System.out); }
/** * Sends to a mailbox */ public void send(String subject, String body) { try { MimeMessage msg = new MimeMessage(getSession()); if(_from.length > 0) msg.addFrom(_from); msg.addRecipients(RecipientType.TO, _to); if(subject != null) msg.setSubject(subject); msg.setContent(body, "text/plain"); send(msg); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Sends to a mailbox */ public void send(String subject, String body) { try { MimeMessage msg = new MimeMessage(getSession()); if(_from.length > 0) msg.addFrom(_from); msg.addRecipients(RecipientType.TO, _to); if(subject != null) msg.setSubject(subject); msg.setContent(body, "text/plain"); send(msg); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
mimeMessage.addFrom(fromAddresses.toArray(new InternetAddress[fromAddresses.size()]));
protected void assignFromAddress(SendingMessage sendingMessage, MimeMessage msg) throws MessagingException { InternetAddress[] internetAddresses = InternetAddress.parse(sendingMessage.getFrom()); for (InternetAddress internetAddress : internetAddresses) { if (StringUtils.isNotEmpty(internetAddress.getPersonal())) { try { internetAddress.setPersonal(internetAddress.getPersonal(), StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new MessagingException("Unsupported encoding type", e); } } } if (internetAddresses.length == 1) { msg.setFrom(internetAddresses[0]); } else { msg.addFrom(internetAddresses); } }
msg.addFrom(_from); msg.addRecipients(RecipientType.TO, _to); if(subject != null)
message.addFrom(getAddresses(FROM));