Guten Tag,
Ich habe versucht einen Code zu schreiben um via Jave emails zu versenden bekomme aber immer wieder diesen Fehler:
Ich habe zwei verschiedene Klassen erstellt:
EmailDemo
EmailSender
Freue mich auf jeden Vorschlag
Ich habe versucht einen Code zu schreiben um via Jave emails zu versenden bekomme aber immer wieder diesen Fehler:
Java:
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
Java:
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
Java:
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
Zuletzt bearbeitet: