0% found this document useful (0 votes)
17 views

Java Mail

This Java code shows how to send an email using JavaMail API. It imports necessary classes, defines a sentEmail method that takes email address, subject, and text as parameters, configures mail server properties and authenticator, creates message, sets from, to, subject, text, and sends the email.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Mail

This Java code shows how to send an email using JavaMail API. It imports necessary classes, defines a sentEmail method that takes email address, subject, and text as parameters, configures mail server properties and authenticator, creates message, sets from, to, subject, text, and sends the email.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import javax.mail.

*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class JavaMail {

public void sentEmail(String toEmail, String subject, String text) {

final String MAIL = "[email protected]";


final String PASSWORD = "wakanda12345";

// Config
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");

// Authenticator
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MAIL, PASSWORD);
}
});

// Mail info
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(MAIL));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toEmail));
message.setSubject(subject);
message.setText(text);

Transport.send(message);
System.out.println("Message sent successfully...");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {


String toEmail = "[email protected]";
String subject = "[JavaMail] - Demo sent email";
String text = "Thank you for using JavaMail";
new JavaMail().sentEmail(toEmail, subject, text);
}
}

You might also like