This example shows how to send an email in Java using Java Mail API.
To send the message using JavaMail API, SMTP server properties are required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import javax.mail.Session; import javax.mail.Message; import javax.mail.Transport; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.InternetAddress; import java.util.Properties; public class Main { public static void main(String[] args) { String from = "user@some-domain.com"; String to = "user@some-domain.com"; String subject = "Hi There..."; String text = "How are you?"; Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.some-domain.com"); properties.put("mail.smtp.port", "25"); Session session = Session.getDefaultInstance(properties, null); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setText(text); Transport.send(message); } } |