Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- private void sendEmail() {
- String title = "YOUR_TITLE_TEXT";
- String body = "YOUR_BODY_TEXT";
- Properties props = new Properties();
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "25");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.EnableSSL.enable", "true");
- props.put("mail.smtp.auth", "true");
- props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.setProperty("mail.smtp.socketFactory.fallback", "false");
- props.setProperty("mail.smtp.port", "465");
- props.setProperty("mail.smtp.socketFactory.port", "465");
- Authenticator authenticator = new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- }
- };
- Session session = Session.getDefaultInstance(props, authenticator);
- try {
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(sender));
- msg.addRecipient(Message.RecipientType.TO,
- new InternetAddress(receiver, receiver));
- msg.setSubject(title);
- msg.setText(body);
- Transport.send(msg);
- } catch (MessagingException e) {
- System.out.println("sendEmail (MessagingException) : " + e.getMessage());
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- System.out.println("sendEmail (UnsupportedEncodingException) : " + e.getMessage());
- e.printStackTrace();
- } catch (Exception e) {
- System.out.println("sendEmail (Exception) : " + e.getMessage());
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement