Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.spring.mail.service.impl;
- import com.spring.mail.service.MailService;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.RequestBody;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.BodyPart;
- import javax.mail.Multipart;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.*;
- import java.io.UnsupportedEncodingException;
- import java.util.Properties;
- @Service
- public class MailServiceImpl implements MailService {
- @Override
- public Object sendMail(@RequestBody String to) {
- // Set required configs
- String from = "from_mail@gmail.com";
- String host = "smtp.gmail.com";
- String port = "587";
- String user = "from_mail_username@gmail.com";
- String password = "from_mail_password";
- // Set system properties
- Properties properties = System.getProperties();
- properties.put("mail.smtp.auth", "true");
- properties.setProperty("mail.smtp.host", host);
- properties.setProperty("mail.smtp.port", port);
- properties.setProperty("mail.smtp.user", user);
- properties.setProperty("mail.smtp.password", password);
- properties.setProperty("mail.smtp.starttls.enable", "true");
- // Get the default Session object.
- Session session = Session.getDefaultInstance(properties);
- try {
- // Create a default MimeMessage object.
- MimeMessage message = new MimeMessage(session);
- // Set from email address
- message.setFrom(new InternetAddress(from, "TechPool"));
- // Set the recipient email address
- message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
- // Set email subject
- message.setSubject("Mail Subject");
- // Initiate body of email address
- BodyPart messageBodyPart = new MimeBodyPart();
- // Set email body
- messageBodyPart.setText("This is a message from TechPool");
- // Initiate class to send media
- Multipart multipart = new MimeMultipart();
- multipart.addBodyPart(messageBodyPart);
- messageBodyPart = new MimeBodyPart();
- // Set path for attachment
- String filename = "ubuntu.jpg";
- // Bind the attachment
- DataSource source = new FileDataSource(filename);
- messageBodyPart.setDataHandler(new DataHandler(source));
- // Set file name into email
- messageBodyPart.setFileName(filename);
- multipart.addBodyPart(messageBodyPart);
- message.setContent(multipart);
- // Set configs for sending email
- Transport transport = session.getTransport("smtp");
- transport.connect(host, from, password);
- // Send email
- transport.sendMessage(message, message.getAllRecipients());
- transport.close();
- System.out.println("done");
- return "Email Sent! Check Inbox!";
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (AddressException e) {
- e.printStackTrace();
- } catch (javax.mail.MessagingException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment