Last active
February 9, 2019 15:23
-
-
Save DeBukkIt/2f85a83339aee915ecd56dfb3e7adf06 to your computer and use it in GitHub Desktop.
Quelltext für mein YouTube-Tutorial, Thema: E-Mails versenden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.debukkitsblog.mailsenden; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Date; | |
import java.util.Properties; | |
import javax.mail.Authenticator; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.PasswordAuthentication; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
public class StartMailSender { | |
public static void main(String[] args) { | |
String username = Zugangsdaten.USERNAME; | |
String password = Zugangsdaten.PASSWORD; | |
MailSender sender = new MailSender(); | |
sender.login("smtpServer.anbieter.de", "465", username, password); | |
try { | |
sender.send("absender@provider.com", "Absender Name", "empfaenger@provider.com", "Test Test Test Betreff", | |
"Überall dieselbe alte Leier.\r\n\r\nDas Layout ist fertig, der Text lässt auf sich warten. " | |
+ "Damit das Layout nun nicht nackt im Raume steht und sich klein und leer vorkommt, " | |
+ "springe ich ein: der Blindtext. Genau zu diesem Zwecke erschaffen, immer im Schatten " | |
+ "meines großen Bruders »Lorem Ipsum«, freue ich mich jedes Mal, wenn Sie ein paar Zeilen " | |
+ "lesen. Denn esse est percipi - Sein ist wahrgenommen werden."); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static class MailSender { | |
protected Session mailSession; | |
public void login(String smtpHost, String smtpPort, String username, String password) { | |
Properties props = new Properties(); | |
props.put("mail.smtp.host", smtpHost); | |
props.put("mail.smtp.socketFactory.port", smtpPort); | |
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.port", smtpPort); | |
Authenticator auth = new Authenticator() { | |
@Override | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(username, password); | |
} | |
}; | |
this.mailSession = Session.getDefaultInstance(props, auth); | |
System.out.println("Eingeloggt."); | |
} | |
public void send(String senderMail, String senderName, String receiverAddresses, String subject, String message) | |
throws MessagingException, IllegalStateException, UnsupportedEncodingException { | |
if (mailSession == null) { | |
throw new IllegalStateException("Du musst dich zuerst einloggen (login()-Methode)"); | |
} | |
MimeMessage msg = new MimeMessage(mailSession); | |
msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); | |
msg.addHeader("format", "flowed"); | |
msg.addHeader("Content-Transfer-Encoding", "8bit"); | |
msg.setFrom(new InternetAddress(senderMail, senderName)); | |
msg.setReplyTo(InternetAddress.parse(senderMail, false)); | |
msg.setSubject(subject, "UTF-8"); | |
msg.setText(message, "UTF-8"); | |
msg.setSentDate(new Date()); | |
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverAddresses, false)); | |
System.out.println("Versende E-Mail..."); | |
Transport.send(msg); | |
System.out.println("E-Mail versendet."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment