Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,160,458 members, 7,843,393 topics. Date: Wednesday, 29 May 2024 at 01:41 AM

Sending E-maill With JAVA - Programming - Nairaland

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

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)

(1) (Reply)

Sending E-maill With JAVA by D34lw4p(m): 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

1 Like

Re: Sending E-maill With JAVA by APHATHEOLOGY(m): 6:40am On Jun 13, 2016
#TeamNCT

(1) (Reply)

Meet The Dazzling Flying Machines Of The Future / Urgent!!!!!! I Need Contact Of A Person In Computer Village Who Knows About Prog / What's the Spec of your workstation?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.