Search in sources :

Example 6 with MessagingException

use of javax.mail.MessagingException 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 7 with MessagingException

use of javax.mail.MessagingException 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 8 with MessagingException

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

the class ProgrammaticEmailServlet 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 JavaMail API</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Sending email using JavaMail API</h1>");
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.debug", "true");
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(creds.getFrom(), creds.getPassword());
            }
        });
        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 Programmatic JavaMail " + Calendar.getInstance().getTime());
            message.setText("Java EE 7 is cool!");
            //                Transport t = session.getTransport();
            //                t.connect(creds.getFrom(), creds.getTo());
            //                t.sendMessage(message, message.getAllRecipients());
            Transport.send(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) Properties(java.util.Properties) PrintWriter(java.io.PrintWriter) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 9 with MessagingException

use of javax.mail.MessagingException in project disconf by knightliao.

the class SimpleMailSender method sendTextMail.

/**
     * 以文本格式发送邮件
     *
     * @param mailInfo 待发送的邮件的信息
     */
public static boolean sendTextMail(MailSenderInfo mailInfo) {
    try {
        // 设置一些通用的数据
        Message mailMessage = setCommon(mailInfo);
        // 设置邮件消息的主要内容
        String mailContent = mailInfo.getContent();
        mailMessage.setText(mailContent);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException)

Example 10 with MessagingException

use of javax.mail.MessagingException in project KJFrameForAndroid by kymjs.

the class SimpleMailSender method sendHtmlMail.

/**
     * 以HTML格式发送邮件
     * 
     * @param mailInfo
     *            待发送的邮件信息
     */
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
    // 判断是否需要身份认证
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    // 如果需要身份认证,则创建一个密码验证器
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    // 根据邮件会话属性和密码验证器构造一个发送邮件的session
    Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
    try {
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 创建邮件发送者地址
        Address from = new InternetAddress(mailInfo.getFromAddress());
        // 设置邮件消息的发送者
        mailMessage.setFrom(from);
        // 创建邮件的接收者地址,并设置到邮件消息中
        Address to = new InternetAddress(mailInfo.getToAddress());
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        // 设置邮件消息的主题
        mailMessage.setSubject(mailInfo.getSubject());
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        mailMessage.setContent(mainPart);
        // 发送邮件
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Aggregations

MessagingException (javax.mail.MessagingException)324 MimeMessage (javax.mail.internet.MimeMessage)126 IOException (java.io.IOException)125 InternetAddress (javax.mail.internet.InternetAddress)64 MimeMultipart (javax.mail.internet.MimeMultipart)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)58 Message (javax.mail.Message)45 Session (javax.mail.Session)43 InputStream (java.io.InputStream)41 Properties (java.util.Properties)38 Date (java.util.Date)35 ArrayList (java.util.ArrayList)33 PackageException (com.axway.ats.action.objects.model.PackageException)32 Address (javax.mail.Address)32 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)31 PublicAtsApi (com.axway.ats.common.PublicAtsApi)31 ServiceException (com.zimbra.common.service.ServiceException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)25 DataHandler (javax.activation.DataHandler)23 BodyPart (javax.mail.BodyPart)23