₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,242 members, 8,420,933 topics. Date: Friday, 05 June 2026 at 02:30 PM

Toggle theme

Sending E-maill With JAVA - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingSending E-maill With JAVA (1174 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 FulamanI Want To Create An AI (artificial Intelligence) With Java234

A 10-year Discovered Security Problem On Instagram Application And Got $10,000Programmers Please Help Me OutDrive UNLIMITED Genuine Real Traffic To Your Website