Search in sources :

Example 1 with Transport

use of javax.mail.Transport in project remusic by aa112901.

the class CommonUtils method sendTextMail.

/**
     * 以文本格式发送邮件
     * @param title 待发送的邮件的信息
     */
public static boolean sendTextMail(String title, String content) {
    try {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, null);
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.163.com", 25, "remusic_log@163.com", "remusiclog1");
        Message mailMessage = new SMTPMessage(session);
        Address from = new InternetAddress("remusic_log@163.com");
        mailMessage.setFrom(from);
        Address to = new InternetAddress("remusic_log@163.com");
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        mailMessage.setSubject(title);
        mailMessage.setSentDate(new Date());
        mailMessage.setText(content);
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Transport(javax.mail.Transport) Date(java.util.Date) Session(javax.mail.Session)

Example 2 with Transport

use of javax.mail.Transport in project javaee7-samples by javaee-samples.

the class AnnotatedEmailServlet method processRequest.

/**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sending email using @MailSessionDefinition</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Sending email using @MailSessionDefinition</h1>");
        try {
            out.println("Sending message from \"" + creds.getFrom() + "\" to \"" + creds.getTo() + "\"...<br>");
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(creds.getFrom()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(creds.getTo()));
            message.setSubject("Sending message using Annotated JavaMail " + Calendar.getInstance().getTime());
            message.setText("Java EE 7 is cool!");
            Transport t = session.getTransport();
            t.connect(creds.getFrom(), creds.getPassword());
            t.sendMessage(message, message.getAllRecipients());
            out.println("message sent!");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        out.println("</body>");
        out.println("</html>");
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) PrintWriter(java.io.PrintWriter)

Example 3 with Transport

use of javax.mail.Transport in project jforum2 by rafaelsteil.

the class Spammer method dispatchMessages.

public boolean dispatchMessages() {
    try {
        int sendDelay = SystemGlobals.getIntValue(ConfigKeys.MAIL_SMTP_DELAY);
        if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_AUTH)) {
            if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
                boolean ssl = SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_SSL);
                Transport transport = this.session.getTransport(ssl ? "smtps" : "smtp");
                try {
                    String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
                    transport.connect(host, username, password);
                    if (transport.isConnected()) {
                        for (Iterator userIter = this.users.iterator(); userIter.hasNext(); ) {
                            User user = (User) userIter.next();
                            if (this.needCustomization) {
                                this.defineUserMessage(user);
                            }
                            Address address = new InternetAddress(user.getEmail());
                            logger.debug("Sending mail to: " + user.getEmail());
                            this.message.setRecipient(Message.RecipientType.TO, address);
                            transport.sendMessage(this.message, new Address[] { address });
                            if (sendDelay > 0) {
                                try {
                                    Thread.sleep(sendDelay);
                                } catch (InterruptedException ie) {
                                    logger.error("Error while Thread.sleep." + ie, ie);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new MailException(e);
                } finally {
                    try {
                        transport.close();
                    } catch (Exception e) {
                    }
                }
            }
        } else {
            for (Iterator iter = this.users.iterator(); iter.hasNext(); ) {
                User user = (User) iter.next();
                if (this.needCustomization) {
                    this.defineUserMessage(user);
                }
                Address address = new InternetAddress(user.getEmail());
                logger.debug("Sending mail to: " + user.getEmail());
                this.message.setRecipient(Message.RecipientType.TO, address);
                Transport.send(this.message, new Address[] { address });
                if (sendDelay > 0) {
                    try {
                        Thread.sleep(sendDelay);
                    } catch (InterruptedException ie) {
                        logger.error("Error while Thread.sleep." + ie, ie);
                    }
                }
            }
        }
    } catch (MessagingException e) {
        logger.error("Error while dispatching the message." + e, e);
    }
    return true;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) User(net.jforum.entities.User) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Iterator(java.util.Iterator) MailException(net.jforum.exceptions.MailException) Transport(javax.mail.Transport) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 4 with Transport

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

the class JavaMailSenderImpl method connectTransport.

/**
	 * Obtain and connect a Transport from the underlying JavaMail Session,
	 * passing in the specified host, port, username, and password.
	 * @return the connected Transport object
	 * @throws MessagingException if the connect attempt failed
	 * @since 4.1.2
	 * @see #getTransport
	 * @see #getHost()
	 * @see #getPort()
	 * @see #getUsername()
	 * @see #getPassword()
	 */
protected Transport connectTransport() throws MessagingException {
    String username = getUsername();
    String password = getPassword();
    if ("".equals(username)) {
        // probably from a placeholder
        username = null;
        if ("".equals(password)) {
            // in conjunction with "" username, this means no password to use
            password = null;
        }
    }
    Transport transport = getTransport(getSession());
    transport.connect(getHost(), getPort(), username, password);
    return transport;
}
Also used : Transport(javax.mail.Transport)

Example 5 with Transport

use of javax.mail.Transport in project gocd by gocd.

the class GoSmtpMailSender method send.

public ValidationBean send(String subject, String body, String to) {
    Transport transport = null;
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Sending email [%s] to [%s]", subject, to));
        }
        Properties props = mailProperties();
        MailSession session = MailSession.getInstance().createWith(props, username, password);
        transport = session.getTransport();
        transport.connect(host, port, nullIfEmpty(username), nullIfEmpty(password));
        MimeMessage msg = session.createMessage(from, to, subject, body);
        transport.sendMessage(msg, msg.getRecipients(TO));
        return ValidationBean.valid();
    } catch (Exception e) {
        LOGGER.error(String.format("Sending failed for email [%s] to [%s]", subject, to), e);
        return ValidationBean.notValid(ERROR_MESSAGE);
    } finally {
        if (transport != null) {
            try {
                transport.close();
            } catch (MessagingException e) {
                LOGGER.error("Failed to close transport", e);
            }
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException)

Aggregations

Transport (javax.mail.Transport)41 Session (javax.mail.Session)28 MimeMessage (javax.mail.internet.MimeMessage)24 JMSession (com.zimbra.cs.util.JMSession)18 MessagingException (javax.mail.MessagingException)18 Test (org.junit.Test)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)14 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)14 InternetAddress (javax.mail.internet.InternetAddress)11 Properties (java.util.Properties)9 Date (java.util.Date)7 Message (javax.mail.Message)6 NoSuchProviderException (javax.mail.NoSuchProviderException)6 SendFailedException (javax.mail.SendFailedException)6 IOException (java.io.IOException)5 SMTPMessage (com.sun.mail.smtp.SMTPMessage)2 SMTPTransport (com.sun.mail.smtp.SMTPTransport)2 Address (javax.mail.Address)2 Multipart (javax.mail.Multipart)2 AddressException (javax.mail.internet.AddressException)2