Pages

Java GMail Send Attachment

Properties props;
final String appname = "App Name";
    String email = "xxx@gmail.com";
    String password = "*#";


            try {

                props = new java.util.Properties();
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

                // Session session = Session.getDefaultInstance(props, null);
                Session session = Session.getInstance(props,
                        new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(email, password);
                    }
                });

                Message msg = new MimeMessage(session);

                msg.setFrom(new InternetAddress(email));
                msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
                msg.setSubject("Errors Log File - " + appname);

                MimeBodyPart textBodyPart = new MimeBodyPart();
                textBodyPart.setText("New errors log.");

                MimeBodyPart attachmentBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource(lblURI.getText()); // ex : "C:\\test.pdf"
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                attachmentBodyPart.setFileName(source.getName()); // ex : "test.pdf"

                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(textBodyPart);  // add the text part
                multipart.addBodyPart(attachmentBodyPart); // add the attachement part

                msg.setContent(multipart);

                Transport.send(msg);
                Util.getUtil().setCursorDefault(rootPane);
                JOptionPane.showMessageDialog(rootPane, "Successfully sent the error message report.", "Alert.", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
             
                /*
                https://hellokoding.com/sending-email-through-gmail-smtp-server-with-java-mail-api-and-oauth-2-authorization/
                */
            } catch (Exception e) {
                Util.getUtil().setCursorDefault(rootPane);
                e.printStackTrace();

                if (e.getMessage().toString().contains("Unknown SMTP host")) {
                    LogFile.log(getClass().getName(), "btnSendActionPerformed", "Connection issue.\nPlease check the internet connection.", rootPane);
                } else if (e.getMessage().toString().contains("Username")) {
                    LogFile.log(getClass().getName(), "btnSendActionPerformed", "Invalid username password combination.\nCheck again.", rootPane);
                } else {
                    LogFile.log(getClass().getName(), "btnSendActionPerformed", e.getMessage().toString(), rootPane);
                }
            }
        }

No comments:

Post a Comment