Last active
July 25, 2020 11:42
-
-
Save ashimjk/970fb979719894b70b5ecb06c81c4bde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.mail.javamail.JavaMailSenderImpl; | |
import org.springframework.mail.javamail.MimeMessageHelper; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.PasswordAuthentication; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Properties; | |
/* | |
* REF : https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html | |
*/ | |
public class Email { | |
public static void main(String[] args) { | |
sendEmail( | |
"", | |
"", | |
"testing email", | |
"", | |
"" | |
); | |
} | |
public static void sendEmail(String emailFrom, | |
String emailTo, | |
String message, | |
String username, | |
String password) { | |
try { | |
JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); | |
mailSender.setHost("smtp.gmail.com"); | |
mailSender.setPort(465); | |
mailSender.setUsername(username); | |
mailSender.setPassword(password); | |
Properties props = mailSender.getJavaMailProperties(); | |
props.put("mail.debug", true); | |
props.put("mail.smtp.ssl.enable", true); | |
props.put("mail.smtp.starttls.enable", true); | |
props.put("mail.smtp.auth", true); | |
MimeMessage mimeMessage = mailSender.createMimeMessage(); | |
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false); | |
helper.setFrom(new InternetAddress(username, emailFrom)); | |
helper.setTo(emailTo); | |
helper.setSubject("testing"); | |
helper.setText(message, true); | |
mailSender.send(mimeMessage); | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
} | |
public static void sendMail(String emailFrom, | |
String emailTo, | |
String message, | |
String username, | |
String password) { | |
Properties prop = new Properties(); | |
prop.put("mail.smtp.auth", "true"); | |
prop.put("mail.debug", "true"); | |
prop.put("mail.smtps.ssl.enable", "true"); | |
prop.put("mail.smtp.starttls.enable", "true"); | |
prop.put("mail.smtp.host", "smtp.gmail.com"); | |
prop.put("mail.smtp.port", "587"); | |
Session session = Session.getInstance(prop, new javax.mail.Authenticator() { | |
@Override | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(username, password); | |
} | |
}); | |
Message msg = new MimeMessage(session); | |
try { | |
msg.setFrom(new InternetAddress(emailFrom, emailFrom)); | |
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo)); | |
msg.setSubject("Testing Email From"); | |
msg.setContent(message, "text/html; charset=utf-8"); | |
Transport.send(msg); | |
} catch (UnsupportedEncodingException | MessagingException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment