# Email versenden mit Java



## Kazbah (5. Jan 2014)

Guten Tag,
Ich habe versucht einen Code zu schreiben um via Jave emails zu versenden bekomme aber immer wieder diesen Fehler:


```
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. z42sm161018700eeo.17 - gsmtp

	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2133)	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2133)
	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2133)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1630)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1132)
	at emaildemo.EmailSender.sendMail(EmailSender.java:49)
	at emaildemo.EmailDemo.main(EmailDemo.java:22)
```

Ich habe zwei verschiedene Klassen erstellt:
EmailDemo


```
package emaildemo;

/**
 *
 * @author Kazbah
 */
public class EmailDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String [] to ={"test1@gmail.com"};
        
        if(EmailSender.sendMail
            ("test2@gmail.com",
                    "password",
                    "message to reciepents",
                    to))System.out.println("email sent");
        else System.out.println("Error: email was not send");
    }
    

    
}
```

EmailSender


```
package emaildemo;


import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Kazbah
 */
public class EmailSender {
    public static boolean sendMail(String from, String password, String message, String to[]){
        String host = "smtp.gmail.com";
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "ture");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user",from);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.port",587);
        props.put("mail.smtp.auth","true");
        Session session = Session.getDefaultInstance(props,null);
        MimeMessage mimeMessage = new MimeMessage(session);
        try{
            mimeMessage.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];
            for(int i = 0;i<to.length;i++){
                toAddress[i] = new InternetAddress(to[i]);
            }
            for(int i = 0;i<toAddress.length;i++){
                mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }
            // add Subject
            mimeMessage.setSubject("mailDemo Test");
            // set message to mimeMessage
            mimeMessage.setText(message);
            Transport transport = session.getTransport("smtp");
            transport.connect(host,from,password);
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            transport.close();
            return true;
        }catch(MessagingException me){
            me.printStackTrace();
            
        }
        return false;
    }
}
```

Freue mich auf jeden Vorschlag


----------



## ARadauer (5. Jan 2014)

ja musst dich vorher authentifizieren...
Google Mail meldet: Must issue a STARTTLS command first

hilft dir das?
Must issue a STARTTLS command first. Sending email with Java and Google Apps - Stack Overflow


----------



## Kazbah (5. Jan 2014)

komm nicht wieter :S


----------



## Sen-Mithrarin (6. Jan 2014)

guck dir mal bitte zeile 20 ganz genau an

props.put("mail.smtp.starttls.enable", "ture");

fällt dir was auf ? ich hoffe ...


ansonsten für weitere infos : E-Mails mit JavaMail versenden @ tutorials.de: Tutorial, Anleitung & Hilfe

manchmal wäre copy'n'paste echt besser


----------



## Kazbah (6. Jan 2014)

Jop vielen dank!


----------

