Send mail java
Here is an example of how to send an email using Java:
Using JavaMail API
You need to add the JavaMail API to your project. You can download it from here.
Here is an example code:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) {
// Set up the email server
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-host");
props.put("mail.smtp.port", "your-smtp-port");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// Create a session
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email-username", "your-email-password");
}
});
// Create a message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-username"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email-username"));
message.setSubject("Test email");
message.setText("This is a test email");
// Send the message
Transport.send(message);
}
}
Using Apache Commons Email
You need to add the Apache Commons Email library to your project. You can download it from here.
Here is an example code:
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class SendEmail {
public static void main(String[] args) {
// Set up the email server
Email email = new SimpleEmail();
email.setHostName("your-smtp-host");
email.setPort(25);
email.setAuthenticator(new DefaultAuthenticator("your-email-username", "your-email-password"));
email.setSSLOnConnect(true);
// Set up the message
email.setFrom("your-email-username");
email.setSubject("Test email");
email.setMsg("This is a test email");
// Send the message
try {
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
}
Using Java 8's built-in email API
Java 8 and later versions have a built-in email API. Here is an example code:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) {
// Set up the email server
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-host");
props.put("mail.smtp.port", "your-smtp-port");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// Create a session
Session session = Session.getInstance(props);
// Create a message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-username"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email-username"));
message.setSubject("Test email");
message.setText("This is a test email");
// Send the message
Transport.send(message);
}
}
Make sure to replace the placeholders (your-smtp-host
, your-smtp-port
, your-email-username
, your-email-password
, recipient-email-username
) with your actual email server settings and email addresses.