Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.activation.*;
- public class SendEmailCheck {
- public static void main(String [] args) throws Exception {
- // Recipient's email ID needs to be mentioned.
- String to = "to@gmail.com";//Receipt mail id
- // Sender's email ID needs to be mentioned
- final String from = "from@gmail.com";// Sender email id
- // Assuming you are sending email from localhost
- // String host = "localhost:8080";
- // Get system properties
- //Properties properties = System.getProperties();
- Properties props=new Properties();
- // Setup mail server
- // properties.setProperty("mail.smtp.host", host);
- props.put("mail.smtp.auth", "true");
- //props.put("mail.smtp.starttle.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.port", "465");
- // Get the default Session object.
- //Session session = Session.getDefaultInstance(properties);
- Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication(from,"*****");//*****-sender password
- }
- });
- try {
- // Create a default MimeMessage object.
- MimeMessage message = new MimeMessage(session);
- // Set From: header field of the header.
- message.setFrom(new InternetAddress(from));
- // Set To: header field of the header.
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- // Set Subject: header field
- message.setSubject("This is the Subject Line!");
- // Now set the actual message
- // message.setText("This is actual message","text/html");
- message.setContent(new String("This is actual message".getBytes(), "iso-8859-1"), "text/html; charset=\"iso-8859-1\"");
- Transport.send(message);
- System.out.println("Sent message successfully....");
- }catch (Exception mex) {
- mex.printStackTrace();
- //throw new RuntimeException(mex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement