0% found this document useful (0 votes)
16 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)
16 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 = "wakandacorp686868@gmail.com";


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 = "khuathop68@gmail.com";
String subject = "[JavaMail] - Demo sent email";
String text = "Thank you for using JavaMail";
new JavaMail().sentEmail(toEmail, subject, text);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy