# Java Mail 1.4.4 - Email versenden (HOTMAIL SMTP auth)



## antrox (20. Mrz 2011)

Hallo,

ich moechte eine Email senden mit GWT. Der Teil den ihr nun seht ist auf dem Server und ist plain Java. Von Daher sind keine GWT Kenntnisse erfolderlich.

Ich nutze hierfuer JavaMail 1.4.4

Das ist was ich habe:


```
public void sendingnow2() throws UnsupportedEncodingException{ 
                String host = "smtp.live.com"; 
                String port = "587"; 

             Properties props = new Properties(); 

                 props.put("mail.smtp.host", host); 
                 props.put("mail.smtp.port", port); 
                 props.put("mail.smtp.starttls.enable", "true"); 
                 props.put("mail.smtp.tls", "true"); 
                 props.put("mail.smtp.user", "peter_listew...@hotmail.com"); 
                 props.put("password", "<MYPASSWORD>"); 

             Session session = Session.getDefaultInstance(props, null); 

                String msgBody = "kk works"; 

                try { 

                    Message msg = new MimeMessage(session); 
                    msg.setFrom(new 
InternetAddress("peter_listew...@hotmail.com", "lol")); 
                    msg.addRecipient(Message.RecipientType.TO, 
                                     new InternetAddress("piotr.listewnik@uni- 
oldenburg.de", "kkk")); 
                    msg.setSubject("funktioiert!"); 
                    msg.setText(msgBody); 
                    msg.saveChanges(); 
                    Transport.send(msg); 
                    System.out.println("k works"); 

                } catch (AddressException e) { 
                        System.out.println("excp 1"); 
                } catch (MessagingException e) { 
                        System.out.println("excp 2"); 
                        System.out.println(e.getMessage()); 
                } 
        }
```


exception: 

excp 2 
Can't send command to SMTP host (java.net.SocketException: Connection 
closed by remote host)


------

Weiß jemand was ich falsch mache?

gruß


----------



## wervh543r6 (20. Mrz 2011)

prop.put("mail.transport.protocol","smtp");


Zum debuggen auch

prop.put("mail.debug","true");


----------



## wervh543r6 (20. Mrz 2011)

Auch 

  prop.put("mail.password");


mail vergessen


----------



## antrox (20. Mrz 2011)

For additional info see: file://C:/Users/xcc/AppData/Local/Temp/gwt-update-2.2.0.html
Loading META-INF/javamail.providers from jar:file:/C:/wrkspc_swp/LC/war/WEB-INF/lib/appengine-api-1.0-sdk-1.3.4.jar!/META-INF/javamail.providers
DEBUG: loading new provider protocol=gm, className=com.google.appengine.api.mail.stdimpl.GMTransport, vendor=null, version=null
Loading META-INF/javamail.providers from jar:file:/C:/wrkspc_swp/LC/war/WEB-INF/lib/imap.jar!/META-INF/javamail.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
Loading META-INF/javamail.providers from jar:file:/C:/wrkspc_swp/LC/war/WEB-INF/lib/pop3.jar!/META-INF/javamail.providers
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsy stems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
Loading META-INF/javamail.providers from jar:file:/C:/wrkspc_swp/LC/war/WEB-INF/lib/smtp.jar!/META-INF/javamail.providers
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
Loading javamail.default.providers from jar:file:/C:/wrkspc_swp/LC/war/WEB-INF/lib/mail.jar!/META-INF/javamail.default.providers
DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@1cbd2c5; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.live.com", port 587, isSSL false
220 BLU0-SMTP56.blu0.hotmail.com Microsoft ESMTP MAIL Service, Version: 6.0.3790.4675 ready at  Sun, 20 Mar 2011 10:34:12 -0700 
DEBUG SMTP: connected to host "smtp.live.com", port: 587

EHLO xcc-PC
250-BLU0-SMTP56.blu0.hotmail.com Hello [79.202.59.160]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-TLS
250-STARTTLS
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "TLS", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "OK", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO xcc-PC
250-BLU0-SMTP56.blu0.hotmail.com Hello [79.202.59.160]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-AUTH LOGIN PLAIN
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "OK", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<peter_listewnik@hotmail.com>
550 5.7.3 Requested action aborted; user not authenticated
DEBUG SMTP: got response code 550, with response: 550 5.7.3 Requested action aborted; user not authenticated

RSET
DEBUG SMTP: EOF: [EOF]
QUIT


----------



## antrox (20. Mrz 2011)

550 5.7.3 Requested action aborted; user not authenticated



```
String host = "smtp.live.com";
		String port = "587";
		 
	     Properties props = new Properties();
	     
		 props.put("mail.smtp.host", host);
		 props.put("mail.smtp.port", port);
		 
		 props.put("mail.transport.protocol","smtp");
		 props.put("mail.debug","true");
		 
		 props.put("mail.smtp.starttls.enable", "true");
		 props.put("mail.smtp.tls", "true");
		 props.put("mail.smtp.user", "peter_listewnik@hotmail.com");
		 props.put("mail.password", "<MYPASSWORD>");
		 
	     Session session = Session.getDefaultInstance(props, null);
```

vergesse ich irgendwas?

Hier die Daten von Hotmail:

 Posteingangsserver:	pop3.live.com (SSL; Port 995)
 Postausgangsserver:	smtp.live.com (STARTTLS; Port 587)
 Benutzername:	Vollständige Hotmail-E-Mail-Adresse
 Besonderheiten:	Verwendet SMTP-Authentifizierung. POP3 und SMTP laufen über TLS- bzw. SSL-Verschlüsselung.


----------



## wervh543r6 (20. Mrz 2011)

Die Authentifizierung fehlt



try
        {
        Properties prop=System.getProperties();
        prop.put("mail.transport.protocol","smtp");
        prop.put("mail.smtp.auth","true");
        prop.put("mail.smtp.host",.....);
        prop.put("mail.smtp.port",.......);
        prop.put("mail.user",......);
        prop.put("mail.password",.......);
        //prop.put("mail.debug","true");
        javax.mail.Session ses1=null;
        if(SMTP_AUTHENTICATION)
            {
            javax.mail.Authenticator auth=null;
            auth=new javax.mail.Authenticator()
            {
            @Override
            public PasswordAuthentication getPasswordAuthentication()
                {
                return new PasswordAuthentication(kontodata.getUsername(),kontodata.getDecodedPasswort());
                }

            };
            ses1=Session.getInstance(prop,auth);
            }
        else
            {
            ses1=Session.getInstance(prop);
            }
        MimeMessage msg=new MimeMessage(ses1);
        msg.setFrom(new InternetAddress("Absender"));

        InternetAddress[] internetadressen=new InternetAddress[1];
             internetadressen[0]=new InternetAddress("Empfänger");

        msg.addRecipients(Message.RecipientType.TO,internetadressen);
        msg.setSubject("Subject");
        Multipart multipart=new MimeMultipart();
        BodyPart messagepart=new MimeBodyPart();
        messagepart.setText("text");
        multipart.addBodyPart(messagepart);
        msg.setSentDate(new Date());
        Transport.send(msg);
        }
    catch(MessagingException e)
        {
        .....
         }


----------



## antrox (20. Mrz 2011)

puuh 

das was da steht, das geht so absolut gar nicht. ich bekomme das auch nicht wirklich angepasst ^^

kannst du das anpassen auf meins. ich tu mich echt schwer damit, ich bin aber dennoch jetzt immer noch dabei 

gruß


----------



## antrox (20. Mrz 2011)

warte hab gleich was hier


----------



## wervh543r6 (20. Mrz 2011)

```
javax.mail.Authenticator auth=null;
            auth=new javax.mail.Authenticator()
            {
            @Override
            public PasswordAuthentication getPasswordAuthentication()
                {
                return new PasswordAuthentication("USERNAME","PASSWORT");
                }

            };
            session=Session.getInstance(prop,auth);
```


----------



## antrox (20. Mrz 2011)

folgendes funtioniert!  dankeschoen 


```
public void sendingnow() throws UnsupportedEncodingException{

		 
		 Properties props = System.getProperties();
		 
		 props.put("mail.smtp.host", "smtp.live.com");
		 props.put("mail.smtp.port", "587");
		 
		 props.put("mail.transport.protocol","smtp");
		 props.put("mail.smtp.auth", "true");
		 props.put("mail.smtp.starttls.enable", "true");
		 props.put("mail.smtp.tls", "true");
		 props.put("mail.smtp.user", "peter_listewnik@hotmail.com");
		 props.put("mail.password", "<MYPASSWORD>");
		 
		 props.put("mail.debug","true");

		 javax.mail.Authenticator auth = null;
		 auth = new javax.mail.Authenticator(){
			 @Override
			 public PasswordAuthentication getPasswordAuthentication()
			 {
			 return new PasswordAuthentication("peter_listewnik@hotmail.com","<MYPASSWORD>");
			 }

			 };
			 
	     Session session = Session.getDefaultInstance(props, auth);
	     
	        String msgBody = "kk works";

	        try {
	        	
	            Message msg = new MimeMessage(session);
	            msg.setFrom(new InternetAddress("peter_listewnik@hotmail.com", "lol"));
	            msg.addRecipient(Message.RecipientType.TO,
	                             new InternetAddress("piotr.listewnik@uni-oldenburg.de", "kkk"));
	            msg.setSubject("funktioiert!");
	            msg.setText(msgBody);
	            msg.saveChanges();
	            Transport.send(msg);
	            System.out.println("k works");

	        } catch (AddressException e) {
	        	System.out.println("excp 1");
	        } catch (MessagingException e) {
	        	System.out.println("excp 2");
	        	System.out.println(e.getMessage());
	        }
	}
```


----------

