Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class SendLetter {
- public static void main(String[] args) throws AddressException, MessagingException {
- try {
- String host = "smtp.gmail.com";
- String user = "vershansky1990@gmail.com";
- String pass = "1eb789azZvV";
- String to = "vershansky1990@gmail.com";
- String from = "vershansky1990@gmail.com";
- String subject = "Heloooo";
- String messageText = "from Java";
- boolean sessionDebug = false;
- Properties props = System.getProperties();
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", host);
- props.put("mail.smtp.port", "587");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.required", "true");
- java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
- Session mailSession = Session.getDefaultInstance(props, null);
- mailSession.setDebug(sessionDebug);
- Message msg = new MimeMessage(mailSession);
- msg.setFrom(new InternetAddress(from));
- InternetAddress[] address = {new InternetAddress(to)};
- msg.setRecipients(Message.RecipientType.TO, address);
- msg.setSubject(subject);
- msg.setSentDate(new Date());
- msg.setText(messageText);
- Transport transport = mailSession.getTransport("smtp");
- transport.connect(host, user, pass);
- transport.sendMessage(msg, msg.getAllRecipients());
- transport.close();
- System.out.println("Message send successfully");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement