₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,219 members, 8,510,718 topics. Date: Friday, 30 October 2026 at 10:23 PM

Toggle theme

Sending E-maill With JAVA - Programming - Nairaland

Nairaland Forum › Science/Technology › Programming › Sending E-maill With JAVA (1191 Views)

1 Reply

Sending E-maill With JAVA by D34lw4p(op): 7:54am On Jan 26, 2016
Sending Email from Java Code

Sending email from Java is simple.

We need to install Java Mail Jar and set its path in our program’s classpath.

The basic properties are set in the code and we are good to send email as mentioned in the code below:


import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String [] args)
{
String to ="recipient@ gmail.com";
String from ="sender@ gmail.com";
String host ="localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);

try{
MimeMessage message =newMimeMessage(session);
message.setFrom(newInternetAddress(from));
message.addRecipient(Message.RecipientType.TO,newInternetAddress(to));
message.setSubject("My Email Subject"wink;
message.setText("My Message Body"wink;
Transport.send(message);
System.out.println("Sent successfully!"wink;
}
catch(MessagingException ex) {
ex.printStackTrace();
}
}
}




Join the coders forum: www.nct.com.ng
Re: Sending E-maill With JAVA by APHATHEOLOGY(m): 6:40am On Jun 13, 2016
#TeamNCT
1 Reply

Creating A Proxy Server With Java. • Learning To Program With Java by the Fulaman • I Want To Create An AI (artificial Intelligence) With Java • 2 • 3 • 4

Please, Help: Want To Build An Online Learning Platform With Laravel • How True Is This About Facebook New Policy? (PHOTOS) • Jsf Hosting