☰
-
Php 95
-
Java 94
-
Javascript 37
-
Regex 18
-
Git 17
-
Security 16
-
Docker 07
-
Python 58
-
Machine Learning 14
-
Book Highlights
Java
/
I/O
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 8
-
Quick start S
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
R
Q
SETUP
p643 JavaMail API is an optional package for Java SE (it is included in Java EE). JavaBeans Activation Framework is also an extension, not part of the Java core API.
Add libraries:
- lib/javax.mai.jar
- lib/activation.jar
Add /lib folder to source path
HOST
p646 JavaMail API provides everything you need to send emails. The only property you have to set in order to send mail is mail.smtp.host
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
class App {
public static void main(String[] args) {
// Set SMTP server
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
Session session = Session.getDefaultInstance(props, null);
// Set message
String to = "myuser@yahoo.com";
String from = "myuser@yahoo.com";
String subject = "Java Mail Api - test";
Message msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Hello JavaMail Api!");
// Send message
Transport.send(msg);
System.out.println("Message sent");
} catch (MessagingException e) {e.printStackTrace();}
}
}
AUTH
p659 Most of the time, you do not need to provide username and password to send email ... when using your local SMTP server.
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;
class App {
public static void main(String[] args) {
// Set SMTP server - Simple (1)
/*
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
Session session = Session.getDefaultInstance(props, null);
*/
// Set SMTP server - Auth (2)
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.port", "587"); // TSL (465 SSLis deprecated)
props.put("mail.smtp.starttls.enable", "true"); // TSL
props.put("mail.smtp.connectiontimeout", "10000"); // 10s
props.put("mail.smtp.timeout", "10000");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myuser", "xxxxxxxxxxx");
// yahoo account / security / generate app password
}
});
// Set message
String to = "myuser@yahoo.com";
String from = "myuser@yahoo.com";
String subject = "Java Mail Api - test";
Message msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Hello JavaMail Api!");
// Send message
Transport.send(msg);
System.out.println("Message sent");
} catch (MessagingException e) {e.printStackTrace();}
}
}
➥ Questions
Last update: 315 days ago