How to go send java mail from localhost
To send a Java mail from localhost, you'll need to follow these steps:
Step 1: Set up your Java project
-
Create a new Java project in your preferred IDE (e.g., Eclipse, IntelliJ IDEA, NetBeans).
-
Add the Java Mail API (javax.mail) to your project. You can do this by adding the following dependency to your
pom.xml
file (if you're using Maven):<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.6.2</version> </dependency>
Step 2: Configure your mail server
-
Since you're sending mail from localhost, you'll need to configure a mail server to send the mail. You can use a local mail server like
smtp4dev
ormailhog
. -
For
smtp4dev
, download the executable from here and run it. This will start a local SMTP server on port 2525. -
For
mailhog
, download the executable from here and run it. This will start a local SMTP server on port 1025.
Step 3: Write your Java code
-
Create a new Java class that will send the mail. For example, let's call it
SendMail.java
. -
Import the necessary classes:
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
-
Create a
Properties
object to configure your mail server:Properties props = new Properties(); props.put("mail.smtp.host", "localhost"); // or "smtp4dev" or "mailhog" props.put("mail.smtp.port", "2525"); // or "1025" props.put("mail.smtp.auth", "true");
-
Create a
Session
object using theProperties
object:Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your-email-address", "your-password"); } });
-
Create a
MimeMessage
object to send the mail:MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("your-email-address")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-address")); message.setSubject("Test Mail"); message.setText("This is a test mail sent from localhost");
-
Send the mail using the
Transport
object:Transport.send(message);
Step 4: Run your Java code
-
Compile and run your
SendMail.java
class. -
The mail should be sent to the recipient's email address.
Here's the complete code:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "2525");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email-address", "your-password");
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-address"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-address"));
message.setSubject("Test Mail");
message.setText("This is a test mail sent from localhost");
Transport.send(message);
}
}
Remember to replace your-email-address
, your-password
, and recipient-email-address
with your actual email credentials and the recipient's email address.