Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
- public class SendEmail {
- public static void main(String... args) {
- SendEmail sendEmail=new SendEmail();
- sendEmail.send();
- }
- public void send() {
- // Recipient's email ID needs to be mentioned.
- String to = "talehsmail@gmail.com";
- // Sender's email ID needs to be mentioned
- String from = "nurlanagayev307@gmail.com";
- // Assuming you are sending email from localhost
- String host = "smtp.gmail.com";
- // Get system properties
- Properties properties = System.getProperties();
- // Setup mail server
- properties.setProperty("mail.smtp.host", host);
- properties.put("mail.smtp.port", "587");
- properties.put("mail.smtp.starttls.enable","true");
- properties.put("mail.smtp.auth", "true"); // If you need to authenticate
- properties.put("mail.smtp.socketFactory.port", 587);
- properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- properties.put("mail.smtp.socketFactory.fallback", "false");
- // Get the default Session object.
- Session session = Session.getDefaultInstance(properties);
- 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");
- // Send message
- Transport t = session.getTransport("smtps");
- t.connect(host, "nurlanagayev307@gmail.com", "kn-8r0th3r5");
- System.out.println("Sent message successfully....");
- } catch (MessagingException mex) {
- mex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement