Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.mail.Authenticator;
- import javax.mail.PasswordAuthentication;
- import java.util.Properties;
- public class sendMailing{
- private static final String SMTP_HOST_NAME = "smtp.gmail.com";
- private static final String SMTP_AUTH_USER = "username";//Give your gmail id
- private static final String SMTP_AUTH_PWD = "******";give ur gmail password
- public static void main(String[] args) throws Exception{
- new sendMailing().test();
- }
- public void test() throws Exception{
- Properties props = new Properties();
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.smtp.host", SMTP_HOST_NAME);
- props.put("mail.smtp.port","465");
- props.put("mail.smtp.starttls.enable","true");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.socketFactory.fallback", "false");
- Authenticator auth = new SMTPAuthenticator();
- Session mailSession = Session.getInstance(props, auth);
- // uncomment for debugging infos to stdout
- mailSession.setDebug(true);
- Transport transport = mailSession.getTransport();
- MimeMessage message = new MimeMessage(mailSession);
- message.setSubject("Test mail");
- message.setContent("Hi" ,"text/plain");
- message.setFrom(new InternetAddress(from));
- message.addRecipient(Message.RecipientType.TO,
- new InternetAddress(to));
- transport.connect();
- transport.sendMessage(message,
- message.getRecipients(Message.RecipientType.TO));
- transport.close();
- }
- private class SMTPAuthenticator extends javax.mail.Authenticator {
- public PasswordAuthentication getPasswordAuthentication() {
- String username = SMTP_AUTH_USER;
- String password = SMTP_AUTH_PWD;
- return new PasswordAuthentication(username, password);
- }
- }
- }
Add Comment
Please, Sign In to add comment