Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class to sent mail from application
- * Date : 17.09.19
- * Last Modified: 17.09.19
- */
- package Test;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- /**
- *
- * @author Fahim
- */
- public class JavaMail {
- public static void sendMail(String recepient) throws MessagingException{
- System.out.println("Preparing to sent email");
- Properties properties = new Properties();
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.starttls.enable", "true");
- properties.put("mail.smtp.host", "smtp.gmail.com");
- properties.put("mail.smtp.port", "587");
- String password = "xxxxx";
- Session session = Session.getInstance(properties,new Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication(myAccountEmail, password);
- }
- });
- Message massage = prepareMessage(session, myAccountEmail, recepient);
- Transport.send(massage);
- System.out.println("Email sent successfully");
- }
- private static Message prepareMessage(Session session, String myAccountEmail, String recepient) {
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(myAccountEmail));
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
- message.setSubject("My first Email from java app");
- message.setText("Hey there, \nThis a automated Email. Created by the application. \n regards from Fahim");
- return message;
- } catch (Exception ex) {
- Logger.getLogger(JavaMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement