Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sendmail;
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
- /**
- * Created by caesar-84 on 9/28/16.
- */
- public class SslGmailSender
- {
- private String username;
- private String password;
- private Properties props;
- public SslGmailSender(String username, String password)
- {
- this.username = username;
- this.password = password;
- props = new Properties();
- 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.auth", "true");
- props.put("mail.smtp.port", "465");
- }
- public void send(String subject, String text, String from, String to)
- {
- Session thisSession = Session.getInstance(props, new Authenticator()
- {
- public PasswordAuthentication getPasswordAuthentification()
- {
- return new PasswordAuthentication(SslGmailSender.this.username, SslGmailSender.this.password);
- }
- });
- try
- {
- MimeMessage message = new MimeMessage(thisSession);
- message.setFrom(new InternetAddress(from));
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- message.setSubject(subject);
- message.setText(text);
- Transport.send(message);
- System.out.println("Yor e-mail has been sent.");
- }
- catch (MessagingException ex)
- {
- ex.printStackTrace();
- }
- }
- public SslGmailSender(String username, String password)
- {
- this.username = username;
- this.password = password;
- props = new Properties();
- 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.auth", "true");
- props.put("mail.smtp.port", "465");
- }
- public void send(String subject, String text, String from, String to)
- {
- Session thisSession = Session.getInstance(props, new Authenticator()
- {
- public PasswordAuthentication getPasswordAuthentification()
- {
- return new PasswordAuthentication(SslGmailSender.this.username, SslGmailSender.this.password);
- }
- });
- try
- {
- MimeMessage message = new MimeMessage(thisSession);
- message.setFrom(new InternetAddress(from));
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- message.setSubject(subject);
- message.setText(text);
- Transport.send(message);
- System.out.println("Yor e-mail has been sent.");
- }
- catch (MessagingException ex)
- {
- ex.printStackTrace();
- }
- }
- package sendmail;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- * Created by caesar-84 on 9/28/16.
- */
- public class Main
- {
- public static void main(String[] args)
- {
- String username = "";
- String password = "";
- String subject = "";
- String text = "";
- String to = "";
- try(BufferedReader console = new BufferedReader(new InputStreamReader(System.in)))
- {
- System.out.print("Your e-mail: ");
- username = console.readLine();
- System.out.print("Password: ");
- password = console.readLine();
- System.out.print("Recipient: ");
- to = console.readLine();
- System.out.print("Message subject: ");
- subject = console.readLine();
- System.out.println("Text to send (type "-END" to finish):");
- String typedText = "";
- StringBuilder sb = new StringBuilder();
- while (!typedText.contains("-END"))
- {
- typedText = console.readLine();
- sb.append(typedText).append("n");
- }
- sb.delete(sb.length() - 4, sb.length());
- text = sb.toString();
- }
- catch (IOException ex){ex.getStackTrace();}
- String from = username;
- SslGmailSender thisSender = new SslGmailSender(username, password);
- thisSender.send(subject, text, from, to);
- }
- }
- catch (IOException ex){ex.getStackTrace();}
- String from = username;
- SslGmailSender thisSender = new SslGmailSender(username, password);
- thisSender.send(subject, text, from, to);
- }
Add Comment
Please, Sign In to add comment