Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. import java.util.Properties;  
  2. import javax.mail.*;  
  3. import javax.mail.internet.*;  
  4.  
  5. public class SendMailBySite {  
  6.  public static void main(String[] args) {  
  7.  
  8.   String host="mail.javatpoint.com";  
  9.   final String user="sonoojaiswal@javatpoint.com";//change accordingly  
  10.   final String password="xxxxx";//change accordingly  
  11.    
  12.   String to="sonoojaiswal1987@gmail.com";//change accordingly  
  13.  
  14.    //Get the session object  
  15.    Properties props = new Properties();  
  16.    props.put("mail.smtp.host",host);  
  17.    props.put("mail.smtp.auth", "true");  
  18.      
  19.    Session session = Session.getDefaultInstance(props,  
  20.     new javax.mail.Authenticator() {  
  21.       protected PasswordAuthentication getPasswordAuthentication() {  
  22.     return new PasswordAuthentication(user,password);  
  23.       }  
  24.     });  
  25.  
  26.    //Compose the message  
  27.     try {  
  28.      MimeMessage message = new MimeMessage(session);  
  29.      message.setFrom(new InternetAddress(user));  
  30.      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
  31.      message.setSubject("javatpoint");  
  32.      message.setText("This is simple program of sending email using JavaMail API");  
  33.        
  34.     //send the message  
  35.      Transport.send(message);  
  36.  
  37.      System.out.println("message sent successfully...");  
  38.    
  39.      } catch (MessagingException e) {e.printStackTrace();}  
  40.  }  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement