Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package ProjektG2;
  2.  
  3. import java.util.*;
  4. import javax.mail.Folder;
  5. import javax.mail.Message;
  6. import javax.mail.Message.RecipientType;
  7. import javax.mail.MessagingException;
  8. import javax.mail.Session;
  9. import javax.mail.Store;
  10. import javax.mail.Transport;
  11. import javax.mail.internet.AddressException;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeMessage;
  14. import javax.swing.JOptionPane;
  15.  
  16. /**
  17. *
  18. * @author edith och alicia KOD HÄMTAD FRÅN
  19. * https://buddhimawijeweera.wordpress.com/2011/02/09/sendreceiveemailsjava/
  20. */
  21. public class Mail {
  22.  
  23. private String anvandarnamn;
  24. private String losenord;
  25. private String skickaHost;
  26. private int skickaPort;
  27. private String fran;
  28. private String till;
  29. private String amne;
  30. private String text;
  31.  
  32. //UPPDATERAR KONTOUPPGIFTER
  33. public void sattKontoUppgifter(String anvandarnamn, String losenord) {
  34.  
  35. //UPPDATERAR FÄLTEN MED INPAR
  36. this.anvandarnamn = anvandarnamn;
  37. this.losenord = losenord;
  38.  
  39. }
  40.  
  41. //SKICKAR UT MAIL TILL ANVÄNDARE FRÅN JAVAGRUPP2@GMAIL.COM
  42. public void skickaGmail(String fran, String till, String amne, String text) {
  43.  
  44. //UPPDATERAR FÄLTEN MED INPAR
  45. this.fran = fran;
  46. this.till = till;
  47. this.amne = amne;
  48. this.text = text;
  49.  
  50. //GMAIL HOST OCH GMAIL PORT
  51. this.skickaHost = "smtp.gmail.com";
  52. this.skickaPort = 465;
  53.  
  54. Properties props = new Properties();
  55. props.put("mail.smtp.host", this.skickaHost);
  56. props.put("mail.smtp.port", String.valueOf(this.skickaPort));
  57. props.put("mail.smtp.user", this.anvandarnamn);
  58. props.put("mail.smtp.password", this.losenord);
  59. props.put("mail.smtp.auth", "true");
  60. Session session1 = Session.getDefaultInstance(props);
  61. Message simpleMessage = new MimeMessage(session1); //MIME stands for Multipurpose Internet Mail Extensions
  62. InternetAddress fromAddress = null;
  63. InternetAddress toAddress = null;
  64.  
  65. try {
  66. fromAddress = new InternetAddress(this.fran);
  67. toAddress = new InternetAddress(this.till);
  68.  
  69. } catch (AddressException e) {
  70. e.printStackTrace();
  71. }
  72.  
  73. try {
  74.  
  75. simpleMessage.setFrom(fromAddress);
  76. simpleMessage.setRecipient(RecipientType.TO, toAddress);
  77. simpleMessage.setSubject(this.amne);
  78. simpleMessage.setText(this.text);
  79. Transport transport = session1.getTransport("smtps");
  80. transport.connect(this.skickaHost, skickaPort, this.anvandarnamn, this.losenord);
  81. transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
  82. transport.close();
  83.  
  84. } catch (MessagingException e) {
  85.  
  86. e.printStackTrace();
  87. }
  88.  
  89. }
  90.  
  91. //LOGGAR IN PÅ KONTOT FÖR ATT KUNNA SKICKA MAIL
  92. public static void start(String mailTill, String amne, String valkommen) {
  93.  
  94. //AVSÄNDARENS ADRESS
  95. String mailFran = new String("javagrupp2@gmail.com");
  96.  
  97. //INLOGGNINGSUPPGIFTER TILL ANVSÄNDARENS MAIL
  98. String senderPassword = new String("adminadmin123");
  99. String senderUserName = new String("javagrupp2");
  100.  
  101. //SKAPAR ETT OBJEKT AV EN GMAILCLIENT
  102. Mail newGmailClient = new Mail();
  103. //SÄTTER KONTOUPPGIFTER
  104. newGmailClient.sattKontoUppgifter(senderUserName, senderPassword);
  105. //SKICKAR GMAIL MED METODEN SKICKAGMAIL:S INFO
  106. newGmailClient.skickaGmail(mailFran, mailTill, amne, valkommen);
  107.  
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement