Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- public class MailFunctions {
- public static Session session;
- public static void init() {
- final String username = "emailAddress";
- final String password = "emailPW";
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- session = Session.getInstance(props, new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- }
- public static void sendMail(String toMail, String subject, String text) {
- try {
- Message message = new MimeMessage(session);
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail));
- message.setSubject(subject);
- MimeBodyPart messageBodyPart = new MimeBodyPart();
- // Fill the message
- messageBodyPart.setText(text, "UTF-8", "html");
- Multipart multipart = new MimeMultipart();
- multipart.addBodyPart(messageBodyPart);
- // Put parts in message
- message.setContent(multipart);
- Transport.send(message);
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }
- /* pom dependecny
- <dependency>
- <groupId>javax.mail</groupId>
- <artifactId>mail</artifactId>
- <version>1.4.7</version>
- </dependency>
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement