Java und Outlook Mail-Versand

StillerRegen

Mitglied
Guten Tag an Alle,
ich habe glaube eine Schwierige Anforderung
Ich möchte mit meiner Outlook E-Mail an andere und an mich selbst eine Mail mit vorgefertigtem Text versenden.
Das soll relativ klein sein, da ich noch nie sowas gemacht habe und ich mich 0 damit auskenne, ist das relativ schwierig für mich.
Die Mails sind in einer Datenbanktabelle die ich schon in einer Liste "Person" gespeichert habe.
und es soll nach if (!p.geburtstag.equals(MonthDay.now())) gemacht werden
Wüsste da jemand wie man das machen könnte?

Java:
Person.add(new Person( lastName, firstName, MonthDay.of(gmonth, gday), mail));

    DateFormat format = new SimpleDateFormat("dd.MM");
            String currentDate = null;
            Calendar calendar = Calendar.getInstance();
            currentDate = format.format(calendar.getTime());
            Date date = new Date();
            currentDate = format.format(date);

            boolean einerHatGeburtstag = false;
            for (Person p : Person) {
                if (p.geburtstag.equals(MonthDay.now())) {
                    System.out.println("\n"+p.vorname + " hat heute Geburtstag!" + " "+ currentDate);
                    einerHatGeburtstag = true;
                }
            }
            if (einerHatGeburtstag) {
                for (Person p : Person) {
                    if (!p.geburtstag.equals(MonthDay.now())) {
                                                        <------------------    // Hier soll es der Code stehen mit dem Outlook E-Mail Versand//
                        System.out.println("\n"+p.vorname+"  "+p.name +"  "+p.Mail+" hat heute nicht Geburtstag");
                    }
                }
            }
 
Danke Simon, aber ich verwende doch jetzt Yahoo, leider gibt er mir bei Session´s ein paar Fehler aus :

Java:
    if (einerHatGeburtstag) {
                for (Person p : Person) {
                    if (!p.geburtstag.equals(MonthDay.now())) {
                        
                        String to = p.Mail;
                        String from ="TestMail@yahoo.de";
                        final String username = "TesMail@yahoo.de";
                          final String password = "TestPassword123";
                        String SMTP_HOST_NAME = "smtp.mail.yahoo.com";
                        
                         Properties props = new Properties();

                            props.put("mail.transport.protocol", "smtp");
                            props.put("mail.smtp.host", SMTP_HOST_NAME);
                            props.put("mail.smtp.auth", "true");
                            props.put("mail.smtp.port", "465");
                            
                            
                            Session session = Session.getInstance(props, <---- Session cannot be resolved
                                    new javax.mail.Authenticator() { <---- javax.mail cannot be resolved to a type
                                       protected PasswordAuthentication getPasswordAuthentication() {
                                          return new PasswordAuthentication(username, password); <--- The Constructor PasswordAuthentication (String, String) is undefined
                                  }
                                    });
 
Hey Simon,
habe ein Code geschrieben, der so zwar fehlerfrei ist, aber wenn ich ihn Starte mit viele Fehler ausgibt:

Java:
String from = "TestMail@yahoo.de";
                         String pass ="TestPassword123";
               
                       String to = "Tester2@yahoo.de";
                       String host = "smtp.mail.yahoo.com";

                       // Get system properties
                       Properties properties = System.getProperties();
                       // Setup mail server
                       properties.put("mail.smtp.starttls.enable", "true");
                       properties.put("mail.smtp.host", host);
                       properties.put("mail.smtp.user", from);
                       properties.put("mail.smtp.password", pass);
                       properties.put("mail.smtp.port", "587");
                       properties.put("mail.smtp.auth", "true");

                       // Get the default Session object.
                       Session session = Session.getDefaultInstance(properties);

                       try{
                          // Create a default MimeMessage object.
                          MimeMessage message = new MimeMessage(session);

                          // Set From: header field of the header.
                          message.setFrom(new InternetAddress(from));

                          // Set To: header field of the header.
                          message.addRecipient(Message.RecipientType.TO,
                                                   new InternetAddress(to));

                          // Set Subject: header field
                          message.setSubject("This is the Subject Line!");

                          // Now set the actual message
                          message.setText(p.name+" "+p.vorname+" "+"hat heute Geburtstag");

                          // Send message
                          Transport transport = session.getTransport("smtp");
                          transport.connect(host, from, pass);
                          transport.sendMessage(message, message.getAllRecipients());
                          transport.close();
                          System.out.println("Sent message successfully....");
                       }catch (MessagingException mex) {
                          mex.printStackTrace();
                       }
                    }


com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.mail.yahoo.com, 587; timeout -1;
  nested exception is:
    java.net.UnknownHostException: smtp.mail.yahoo.com

    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at Geburtstag.Verbindung.main(Verbindung.java:144)
Caused by: java.net.UnknownHostException: smtp.mail.yahoo.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
    ... 4 more


@Edit: Ich muss das Später vertesten, der kommt an keine Connection, weil ich in einem "Geschützten Netzwerk" drinnen bin, werde mich morgen dazu äußern, könntest du dir den Code trotzdem mal ansehen und vlt. schon ein Resumé abgeben, dass es so ok ist?
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück