Search in sources :

Example 26 with AddressException

use of javax.mail.internet.AddressException in project openhab1-addons by openhab.

the class CiscoSpark method sparkPerson.

/**
     * Sends a Message to a Person via Cisco Spark
     *
     * @param msgTxt the Message to send
     * @param personEmail the email of the person to which to send
     *
     * @return <code>true</code>, if sending the message has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends a Message via Cisco Spark", returns = "<code>true</code>, if sending the tweet has been successful and <code>false</code> in all other cases.")
public static boolean sparkPerson(@ParamDoc(name = "msgTxt", text = "the Message to send") String msgTxt, @ParamDoc(name = "personEmail", text = "the email of the person to which to send") String personEmail) {
    if (!CiscoSparkActionService.isProperlyConfigured) {
        logger.warn("Cisco Spark is not yet configured > execution aborted!");
        return false;
    }
    // Validate message
    if (msgTxt == null || "".equals(msgTxt)) {
        logger.warn("Message can't be empty");
        return false;
    }
    // Validate email
    try {
        InternetAddress email = new InternetAddress(personEmail);
        email.validate();
    } catch (AddressException e) {
        logger.warn("Email address is not valid");
        return false;
    }
    try {
        logger.debug("Creating message");
        // Create the message
        Message msg = new Message();
        msg.setToPersonEmail(personEmail);
        msg.setMarkdown(msgTxt);
        logger.debug("About to send message");
        // send the Message
        spark.messages().post(msg);
        logger.debug("Successfully sent Message '{}'", msg.getMarkdown());
        return true;
    } catch (SparkException se) {
        logger.warn("Failed to send message.", se);
        return false;
    } catch (Exception e) {
        logger.warn("Failed to send message!", e);
        return false;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(com.ciscospark.Message) SparkException(com.ciscospark.SparkException) AddressException(javax.mail.internet.AddressException) AddressException(javax.mail.internet.AddressException) SparkException(com.ciscospark.SparkException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 27 with AddressException

use of javax.mail.internet.AddressException in project spring-framework by spring-projects.

the class JavaMailSenderTests method javaMailSenderWithParseExceptionOnMimeMessagePreparator.

@Test
public void javaMailSenderWithParseExceptionOnMimeMessagePreparator() {
    MockJavaMailSender sender = new MockJavaMailSender();
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        @Override
        public void prepare(MimeMessage mimeMessage) throws MessagingException {
            mimeMessage.setFrom(new InternetAddress(""));
        }
    };
    try {
        sender.send(preparator);
    } catch (MailParseException ex) {
        // expected
        assertTrue(ex.getCause() instanceof AddressException);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) MailParseException(org.springframework.mail.MailParseException) Test(org.junit.Test)

Example 28 with AddressException

use of javax.mail.internet.AddressException in project ats-framework by Axway.

the class TypeEmailAddress method validate.

/**
     * Performs a type-specific validation for the given
     * {@link ValidationType}
     */
@Override
public void validate() throws TypeException {
    try {
        super.validate();
    } catch (TypeException e) {
        throw new TypeException(ERROR_MESSAGE_INVALID_EMAIL + e.getMessage(), this.parameterName, e);
    }
    try {
        String email = (String) this.value;
        // Note that the JavaMail's implementation of email address validation is
        // somewhat limited. The Javadoc says "The current implementation checks many,
        // but not all, syntax rules.". For example, the address a@ is correctly
        // flagged as invalid, but the address "a"@ is considered
        // valid by JavaMail, even though it is not valid according to RFC 822.
        new InternetAddress(email, true);
    } catch (AddressException ae) {
        throw new TypeException(ERROR_MESSAGE_INVALID_EMAIL, this.parameterName, ae);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) TypeException(com.axway.ats.core.validation.exceptions.TypeException)

Example 29 with AddressException

use of javax.mail.internet.AddressException in project ats-framework by Axway.

the class MailReportSender method send.

/**
     * Email the report
     */
public void send() {
    log.info("Sending log mail report");
    // get the info needed for sending a mail
    ReportConfigurator reportConfigurator = ReportConfigurator.getInstance();
    String smtpServerName = reportConfigurator.getSmtpServerName();
    String smtpServerPort = reportConfigurator.getSmtpServerPort();
    String[] addressesTo = reportConfigurator.getAddressesTo();
    String[] addressesCc = reportConfigurator.getAddressesCc();
    String[] addressesBcc = reportConfigurator.getAddressesBcc();
    String addressFrom = reportConfigurator.getAddressFrom();
    // Attaching to default Session
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.host", smtpServerName);
    mailProperties.put("mail.smtp.port", smtpServerPort);
    Session session = Session.getDefaultInstance(mailProperties);
    Message msg = new MimeMessage(session);
    String errMsg = "Error creating mail object";
    try {
        // mail addresses
        msg.setFrom(new InternetAddress(addressFrom));
        msg.setRecipients(Message.RecipientType.TO, transformAdresses(addressesTo));
        msg.setRecipients(Message.RecipientType.CC, transformAdresses(addressesCc));
        msg.setRecipients(Message.RecipientType.BCC, transformAdresses(addressesBcc));
        // mail subject
        msg.setSubject(subject);
        // mail content
        msg.setContent(body, "text/html");
        // other header information
        msg.setSentDate(new Date());
    } catch (AddressException e) {
        throw new MailReportSendException(errMsg, e);
    } catch (MessagingException e) {
        throw new MailReportSendException(errMsg, e);
    }
    // send the message
    errMsg = "Error sending mail";
    try {
        Transport.send(msg);
    } catch (MessagingException e) {
        throw new MailReportSendException(errMsg, e);
    }
    log.info("Log mail report sent ok");
}
Also used : ReportConfigurator(com.axway.ats.log.report.model.ReportConfigurator) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MailReportSendException(com.axway.ats.log.report.exceptions.MailReportSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Example 30 with AddressException

use of javax.mail.internet.AddressException in project LAMSADE-tools by LAntoine.

the class Util method sendEmail.

/**
	 * Sends an email to an address passed by parameter. If there is an error
	 * other than lack of an Internet connection, the program will exit with a
	 * stack trace.
	 *
	 * @param to_address
	 *            the address to send the email to
	 * @param filename
	 *            if it's not empty, it will send the file referenced to by the
	 *            filename as an attachment
	 * @throws IllegalStateException
	 * @returns 0 if it all went well, -1 if there was some error.
	 */
public static int sendEmail(String to_address, String filename) throws IllegalStateException {
    int smtp_port = 465;
    String smtp_host = "smtp.gmail.com";
    String smtp_username = "lamsade.tools@gmail.com";
    String smtp_password = "z}}VC_-7{3bk^?*q^qZ4Z>G<5cgN&un&@>wU`gyza]kR(2v/&j!*6*s?H[^w=52e";
    Properties props = System.getProperties();
    props.put("mail.smtp.host", smtp_host);
    props.put("mail.smtps.auth", true);
    props.put("mail.smtps.starttls.enable", true);
    props.put("mail.smtps.debug", true);
    Session session = Session.getInstance(props, null);
    Message message = new MimeMessage(session);
    logger.info("Trying to send an email to " + to_address);
    try {
        try {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to_address));
        } catch (AddressException e) {
            logger.error("There is a problem with the address");
            throw new IllegalStateException(e);
        }
        message.setFrom(new InternetAddress(smtp_username));
        if (filename == "") {
            message.setSubject("Email Test Subject");
            message.setText("Email Test Body");
        } else {
            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent("Une conference a été partagée avec vous", "text/html");
            MimeBodyPart attachPart = new MimeBodyPart();
            String attachFile = filename;
            attachPart.attachFile(attachFile);
            // adds parts to the multipart
            multipart.addBodyPart(messageBodyPart);
            multipart.addBodyPart(attachPart);
            // sets the multipart as message's content
            message.setContent(multipart);
        }
        Transport transport = session.getTransport("smtps");
        try {
            transport.connect(smtp_host, smtp_port, smtp_username, smtp_password);
        } catch (MessagingException e) {
            logger.debug("There seems to be a problem with the connection. Try again later");
            return -1;
        }
        try {
            transport.sendMessage(message, message.getAllRecipients());
        } catch (SendFailedException e) {
            logger.error("Something went wrong trying to send the message");
            throw new IllegalStateException(e);
        }
        transport.close();
    } catch (MessagingException e) {
        logger.error("Something went wrong with building the message");
        throw new IllegalStateException(e);
    } catch (IOException e) {
        logger.error("Error in trying to add the attachment");
        throw new IllegalStateException(e);
    }
    logger.info("Email sent successfully");
    return 0;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) SendFailedException(javax.mail.SendFailedException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) MimeBodyPart(javax.mail.internet.MimeBodyPart) Transport(javax.mail.Transport) Session(javax.mail.Session)

Aggregations

AddressException (javax.mail.internet.AddressException)46 InternetAddress (javax.mail.internet.InternetAddress)37 MimeMessage (javax.mail.internet.MimeMessage)18 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)17 MessagingException (javax.mail.MessagingException)15 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Date (java.util.Date)7 Address (javax.mail.Address)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Properties (java.util.Properties)4 Session (javax.mail.Session)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 ServiceException (com.zimbra.common.service.ServiceException)3 Account (com.zimbra.cs.account.Account)3 Invite (com.zimbra.cs.mailbox.calendar.Invite)3 ZAttendee (com.zimbra.cs.mailbox.calendar.ZAttendee)3 Locale (java.util.Locale)3