Java Mail SMTP und TLS

BaseBallBatBoy

Erfahrenes Mitglied
Hallo!

Ich möchte mit dem Java Mail API eine Mail mit Attachment versenden. Dummerweise kann ich aber nie connecten. Der Fehler lautet:

javax.mail.MessagingException: Could not connect to SMTP host: *******, port: 25; nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

Hier noch mein Code. Seht ihr einen Fehler?
Code:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {
	
	String d_email = "*******", //unkenntlich gemachte Versendermail
	d_password = "********!", //unkenntlich gemachtes PW
	d_host = "*******", //unkenntlich gemachter Server
	d_port = "25",
	m_subject = "Testing",
	m_text = "Hey, this is the testing email.";
	
	public SendMail(String to, String fileAttachment) throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.user", d_email);
		props.put("mail.smtp.host", d_host);
		props.put("mail.smtp.port", d_port);
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", d_port);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		try {
			Authenticator auth = new SMTPAuthenticator();
			Session session = Session.getInstance(props, auth);
	
			// Define message
		    MimeMessage msg = new MimeMessage(session);
		    msg.setFrom(new InternetAddress(d_email));
		    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			msg.setSubject(m_subject);
	
		    // create the message part 
		    MimeBodyPart messageBodyPart = new MimeBodyPart();
	
		    //fill message
		    messageBodyPart.setText(m_text);
	
		    Multipart multipart = new MimeMultipart();
		    multipart.addBodyPart(messageBodyPart);
	
		    // Part two is attachment
		    messageBodyPart = new MimeBodyPart();
		    DataSource source = new FileDataSource(fileAttachment);
		    messageBodyPart.setDataHandler(new DataHandler(source));
		    messageBodyPart.setFileName(fileAttachment);
		    multipart.addBodyPart(messageBodyPart);
	
		    // Put parts in message
		    msg.setContent(multipart);
	
		    // Send the message
		    Transport.send(msg);
		} catch (Exception mex) {
			mex.printStackTrace();
		}
	}
	
	private class SMTPAuthenticator extends javax.mail.Authenticator {
		public PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(d_email, d_password);
		}
	}
	
	public static void main(String[] args) {
		String to = "*********"; //unkenntlich gemachte Empfängermail
		String fileAttachment = "C:/path/to/my/attachment/myPDF.pdf";
				
		try {
			new SendMail(to, fileAttachment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Danke schonmal für eure Mühen und Gruss
BBBB
 
Ich hab die Lösung selbst gefunden.
Man muss nur die drei Zeilen mit den Properties bezüglich der Socket-Factory entfernen, dann funktioniert es.
 

Neue Beiträge

Zurück