How to send mail from java application
There are several ways to send mail from a Java application. Here are a few options:
1. Using JavaMail API
The JavaMail API is a popular and widely-used library for sending emails from Java applications. Here's an example of how to use it:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[] args) {
// Set up the mail 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");
// 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.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
message.setSubject("Test Email");
message.setText("This is a test email");
// Send the message
Transport.send(message);
}
}
2. Using Spring Framework
If you're using the Spring Framework in your Java application, you can use the JavaMailSender
class to send emails. Here's an example:
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreprocessor;
import org.springframework.mail.javamail.MimeMessagePreprocessor;
public class SendMail {
@Autowired
private JavaMailSender mailSender;
public void sendMail() {
MimeMessagePreprocessor preprocessor = new MimeMessagePreprocessor();
preprocessor.setFrom("your-email-username");
preprocessor.setTo("recipient-email-username");
preprocessor.setSubject("Test Email");
preprocessor.setText("This is a test email");
mailSender.send(preprocessor);
}
}
3. Using Apache Commons Email
The Apache Commons Email library is another popular option for sending emails from Java applications. Here's an example:
import org.apache.commons.email.Email;
import org.apache.commons.email.EmailException;
import org.apache.commons.email.SimpleEmail;
public class SendMail {
public static void main(String[] args) {
// Create an email object
Email email = new SimpleEmail();
// Set the from address
email.setFrom("your-email-username");
// Set the to address
email.addTo("recipient-email-username");
// Set the subject
email.setSubject("Test Email");
// Set the message body
email.setMsg("This is a test email");
// Send the email
try {
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
}
4. Using Java 8's java.util.mail
package
Java 8 introduced a new java.util.mail
package that provides a simpler way to send emails. Here's an example:
import java.util.Properties;
import java.util.logging.Logger;
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 SendMail {
public static void main(String[] args) {
// Set up the mail 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");
// Create a session
Session session = Session.getInstance(props);
// Create a message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-username"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
message.setSubject("Test Email");
message.setText("This is a test email");
// Send the message
Transport.send(message);
}
}
These are just a few examples of how to send mail from a Java application. The specific approach you choose will depend on your requirements and the libraries you're using in your project.