Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Properties;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class SendMailBySite {
- public static void main(String[] args) {
- String host="mail.javatpoint.com";
- final String user="sonoojaiswal@javatpoint.com";//change accordingly
- final String password="xxxxx";//change accordingly
- String to="sonoojaiswal1987@gmail.com";//change accordingly
- //Get the session object
- Properties props = new Properties();
- props.put("mail.smtp.host",host);
- props.put("mail.smtp.auth", "true");
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user,password);
- }
- });
- //Compose the message
- try {
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(user));
- message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
- message.setSubject("javatpoint");
- message.setText("This is simple program of sending email using JavaMail API");
- //send the message
- Transport.send(message);
- System.out.println("message sent successfully...");
- } catch (MessagingException e) {e.printStackTrace();}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement