Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. import java.util.Properties;
  2. import javax.activation.*;
  3. import javax.mail.*;
  4.  
  5. public class MailProjectClass {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. final String username = "your.mail.id@gmail.com";
  10. final String password = "your.password";
  11.  
  12. Properties props = new Properties();
  13. props.put("mail.smtp.auth", true);
  14. props.put("mail.smtp.starttls.enable", true);
  15. props.put("mail.smtp.host", "smtp.gmail.com");
  16. props.put("mail.smtp.port", "587");
  17.  
  18. Session session = Session.getInstance(props,
  19. new javax.mail.Authenticator() {
  20. protected PasswordAuthentication getPasswordAuthentication() {
  21. return new PasswordAuthentication(username, password);
  22. }
  23. });
  24.  
  25. try {
  26.  
  27. Message message = new MimeMessage(session);
  28. message.setFrom(new InternetAddress("from.mail.id@gmail.com"));
  29. message.setRecipients(Message.RecipientType.TO,
  30. InternetAddress.parse("to.mail.id@gmail.com"));
  31. message.setSubject("Testing Subject");
  32. message.setText("PFA");
  33.  
  34. MimeBodyPart messageBodyPart = new MimeBodyPart();
  35.  
  36. Multipart multipart = new MimeMultipart();
  37.  
  38. messageBodyPart = new MimeBodyPart();
  39. String file = "path of file to be attached";
  40. String fileName = "attachmentName";
  41. DataSource source = new FileDataSource(file);
  42. messageBodyPart.setDataHandler(new DataHandler(source));
  43. messageBodyPart.setFileName(fileName);
  44. multipart.addBodyPart(messageBodyPart);
  45.  
  46. message.setContent(multipart);
  47.  
  48. System.out.println("Sending");
  49.  
  50. Transport.send(message);
  51.  
  52. System.out.println("Done");
  53.  
  54. } catch (MessagingException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59.  
  60. Properties props = new java.util.Properties();
  61. props.put("mail.smtp.host", "yourHost");
  62. props.put("mail.smtp.port", "yourHostPort");
  63. props.put("mail.smtp.auth", "true");
  64. props.put("mail.smtp.starttls.enable", "true");
  65.  
  66.  
  67. // Session session = Session.getDefaultInstance(props, null);
  68. Session session = Session.getInstance(props,
  69. new javax.mail.Authenticator() {
  70. protected PasswordAuthentication getPasswordAuthentication() {
  71. return new PasswordAuthentication("user", "password");
  72. }
  73. });
  74.  
  75.  
  76. Message msg = new MimeMessage(session);
  77. try {
  78. msg.setFrom(new InternetAddress(mailFrom));
  79. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
  80. msg.setSubject("your subject");
  81.  
  82. Multipart multipart = new MimeMultipart();
  83.  
  84. MimeBodyPart textBodyPart = new MimeBodyPart();
  85. textBodyPart.setText("your text");
  86.  
  87. MimeBodyPart attachmentBodyPart= new MimeBodyPart();
  88. DataSource source = new FileDataSource(attachementPath); // ex : "C:\test.pdf"
  89. attachmentBodyPart.setDataHandler(new DataHandler(source));
  90. attachmentBodyPart.setFileName(fileName); // ex : "test.pdf"
  91.  
  92. multipart.addBodyPart(textBodyPart); // add the text part
  93. multipart.addBodyPart(attachmentBodyPart); // add the attachement part
  94.  
  95. msg.setContent(multipart);
  96.  
  97.  
  98. Transport.send(msg);
  99. } catch (MessagingException e) {
  100. LOGGER.log(Level.SEVERE,"Error while sending email",e);
  101. }
  102.  
  103. package com.mkyong.common;
  104.  
  105.  
  106. import javax.mail.MessagingException;
  107. import javax.mail.internet.MimeMessage;
  108.  
  109. import org.springframework.core.io.FileSystemResource;
  110. import org.springframework.mail.MailParseException;
  111. import org.springframework.mail.SimpleMailMessage;
  112. import org.springframework.mail.javamail.JavaMailSender;
  113. import org.springframework.mail.javamail.MimeMessageHelper;
  114.  
  115. public class MailMail
  116. {
  117. private JavaMailSender mailSender;
  118. private SimpleMailMessage simpleMailMessage;
  119.  
  120. public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
  121. this.simpleMailMessage = simpleMailMessage;
  122. }
  123.  
  124. public void setMailSender(JavaMailSender mailSender) {
  125. this.mailSender = mailSender;
  126. }
  127.  
  128. public void sendMail(String dear, String content) {
  129.  
  130. MimeMessage message = mailSender.createMimeMessage();
  131.  
  132. try{
  133. MimeMessageHelper helper = new MimeMessageHelper(message, true);
  134.  
  135. helper.setFrom(simpleMailMessage.getFrom());
  136. helper.setTo(simpleMailMessage.getTo());
  137. helper.setSubject(simpleMailMessage.getSubject());
  138. helper.setText(String.format(
  139. simpleMailMessage.getText(), dear, content));
  140.  
  141. FileSystemResource file = new FileSystemResource("/home/abdennour/Documents/cv.pdf");
  142. helper.addAttachment(file.getFilename(), file);
  143.  
  144. }catch (MessagingException e) {
  145. throw new MailParseException(e);
  146. }
  147. mailSender.send(message);
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement