Subversion Repositories javautils

Compare Revisions

No changes between revisions

Regard whitespace Rev 2 → Rev 3

/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/sender/RawMailSenderPlainTextTest.java
0,0 → 1,342
package de.viathinksoft.utils.mail.sender;
 
import static org.junit.Assert.fail;
 
import java.net.ConnectException;
 
import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
 
import org.junit.Test;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.InvalidMailAddressException;
import de.viathinksoft.utils.mail.sender.PlainTextMailSender;
 
public class RawMailSenderPlainTextTest {
@Test
public void testPostMailNullPointer() throws MessagingException,
AuthentificateDataIncompleteException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
try {
mailsender.sendMail();
fail();
} catch (AddressException e) {
} catch (MessagingException e) {
// if ((e.getCause() != null) && (e.getCause().getNextException() ==
// ConnectException)) {
if ((e.getCause() != null)
&& (e.getCause().getClass() == ConnectException.class)) {
// Wir erwarten eine Exception, da wir davon ausgehen, dass auf
// Localhost kein SMTP-Server läuft!
} else {
e.printStackTrace();
fail();
}
}
 
// -----------------------------------------------------
 
try {
mailsender.setRecipient((EMailAddress)null);
fail();
} catch (InvalidMailAddressException e1) {
}
 
try {
mailsender.sendMail();
fail();
} catch (AddressException e) {
} catch (MessagingException e) {
// if ((e.getCause() != null) && (e.getCause().getNextException() ==
// ConnectException)) {
if ((e.getCause() != null)
&& (e.getCause().getClass() == ConnectException.class)) {
// Wir erwarten eine Exception, da wir davon ausgehen, dass auf
// Localhost kein SMTP-Server läuft!
} else {
e.printStackTrace();
fail();
}
}
// -----------------------------------------------------
 
try {
mailsender.setRecipient((String)null);
fail();
} catch (InvalidMailAddressException e1) {
}
 
try {
mailsender.sendMail();
fail();
} catch (AddressException e) {
} catch (MessagingException e) {
// if ((e.getCause() != null) && (e.getCause().getNextException() ==
// ConnectException)) {
if ((e.getCause() != null)
&& (e.getCause().getClass() == ConnectException.class)) {
// Wir erwarten eine Exception, da wir davon ausgehen, dass auf
// Localhost kein SMTP-Server läuft!
} else {
e.printStackTrace();
fail();
}
}
 
// -----------------------------------------------------
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
 
mailsender.setSmtpHost(null);
mailsender.setSmtpUsername(null);
mailsender.setSmtpPassword(null);
mailsender.setSmtpPort(-1);
 
try {
mailsender.sendMail();
fail();
} catch (MessagingException e) {
// if ((e.getCause() != null) && (e.getCause().getNextException() ==
// ConnectException)) {
if ((e.getCause() != null)
&& (e.getCause().getClass() == ConnectException.class)) {
// Wir erwarten eine Exception, da wir davon ausgehen, dass auf
// Localhost kein SMTP-Server läuft!
} else {
e.printStackTrace();
fail();
}
}
 
// -----------------------------------------------------
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
mailsender.setSmtpUsername(null);
mailsender.setSmtpPassword(null);
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setSmtpAuth(true);
// mailsender.setSmtpAuthUser(null);
// mailsender.setSmtpAuthPass(null);
 
try {
mailsender.sendMail();
fail();
} catch (AuthentificateDataIncompleteException e) {
}
 
// -----------------------------------------------------
 
mailsender.setSmtpAuth(false);
 
try {
mailsender.setMailFrom((EMailAddress)null);
fail();
} catch (InvalidMailAddressException e1) {
}
mailsender.setSubject(null);
mailsender.setMessage(null);
 
try {
mailsender.sendMail();
} catch (AuthenticationFailedException e) {
// Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
// nicht.
}
// -----------------------------------------------------
 
try {
mailsender.setMailFrom((String)null);
fail();
} catch (InvalidMailAddressException e1) {
}
 
try {
mailsender.sendMail();
} catch (AuthenticationFailedException e) {
// Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
// nicht.
}
 
// -----------------------------------------------------
 
mailsender.setSmtpAuth(true);
mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
 
mailsender.sendMail();
}
 
@Test
public void testPostMailBlank()
throws AuthentificateDataIncompleteException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 1");
mailsender.setMessage("JUnit-Test 1 von Compuglobal");
 
try {
mailsender.sendMail();
fail();
} catch (MessagingException e) {
// if ((e.getCause() != null) && (e.getCause().getNextException() ==
// ConnectException)) {
if ((e.getCause() != null)
&& (e.getCause().getClass() == ConnectException.class)) {
// Wir erwarten eine Exception, da wir davon ausgehen, dass auf
// Localhost kein SMTP-Server läuft!
} else {
e.printStackTrace();
fail();
}
}
}
 
@Test
public void testPostMailWithData() throws MessagingException,
AuthentificateDataIncompleteException, InvalidMailAddressException
{
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 2");
mailsender.setMessage("JUnit-Test 2 von Compuglobal");
 
try {
mailsender.sendMail();
} catch (AuthenticationFailedException e) {
// Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
// nicht.
}
}
 
@Test
public void testPostMailWithDataAndOrigin() throws MessagingException,
AuthentificateDataIncompleteException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setMailFrom(TestConfiguration.getMailFrom());
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 3");
mailsender.setMessage("JUnit-Test 3 von Compuglobal");
 
try {
mailsender.sendMail();
} catch (AuthenticationFailedException e) {
// Diese Fehlermeldung KANN vom SMTP geworfen werden. MUSS aber
// nicht.
}
}
 
@Test
public void testPostMailWithDataAndOriginAndSmtpAuthIncompleteUserAndPwd()
throws MessagingException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
// mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
// mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setMailFrom(TestConfiguration.getMailFrom());
 
mailsender.setSmtpAuth(true);
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 4a");
mailsender.setMessage("JUnit-Test 4a von Compuglobal");
 
try {
mailsender.sendMail();
 
fail();
} catch (AuthentificateDataIncompleteException e) {
}
}
 
@Test
public void testPostMailWithDataAndOriginAndSmtpAuthIncompleteUser()
throws MessagingException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
// mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setMailFrom(TestConfiguration.getMailFrom());
 
mailsender.setSmtpAuth(true);
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 4b");
mailsender.setMessage("JUnit-Test 4b von Compuglobal");
 
try {
mailsender.sendMail();
 
fail();
} catch (AuthentificateDataIncompleteException e) {
}
}
 
@Test
public void testPostMailWithDataAndOriginAndSmtpAuthIncompletePwd()
throws MessagingException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
// mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setMailFrom(TestConfiguration.getMailFrom());
 
mailsender.setSmtpAuth(true);
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 4c");
mailsender.setMessage("JUnit-Test 4c von Compuglobal");
 
try {
mailsender.sendMail();
 
fail();
} catch (AuthentificateDataIncompleteException e) {
}
}
 
@Test
public void testPostMailWithDataAndOriginAndSmtpAuthComplete()
throws MessagingException, AuthentificateDataIncompleteException, InvalidMailAddressException {
PlainTextMailSender mailsender = new PlainTextMailSender();
 
mailsender.setSmtpHost(TestConfiguration.getSmtpHost());
mailsender.setSmtpUsername(TestConfiguration.getSmtpUsername());
mailsender.setSmtpPassword(TestConfiguration.getSmtpPassword());
mailsender.setSmtpPort(TestConfiguration.getSmtpPort());
 
mailsender.setMailFrom(TestConfiguration.getMailFrom());
 
mailsender.setSmtpAuth(true);
 
mailsender.setRecipient(TestConfiguration.getSpamAddress());
mailsender.setSubject("JUnit-Test 4d");
mailsender.setMessage("JUnit-Test 4d von Compuglobal");
 
mailsender.sendMail();
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/sender/TestConfiguration.java
0,0 → 1,37
package de.viathinksoft.utils.mail.sender;
 
// Statische Klasse, die unsere Konfiguration für Tests verwaltet.
// Ginge auch als Singleton
 
public final class TestConfiguration {
public static String getSpamAddress() {
// Gott habe Mitleid mit dem Eigentümer dieser E-Mail-Adresse...
return "b973768@owlpic.com";
 
// Eine Wegwerfadresse für manuelle Tests kann auf
// www.10minutemail.com erstellt werden.
}
public static String getSmtpHost() {
return "";
}
public static String getSmtpUsername() {
return "";
}
public static String getSmtpPassword() {
return "";
}
public static int getSmtpPort() {
return 25;
}
public static String getMailFrom() {
return getSpamAddress();
}
private TestConfiguration() {
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/test/de/viathinksoft/utils/mail/EMailAddressTest.java
0,0 → 1,153
package de.viathinksoft.utils.mail;
 
import static org.junit.Assert.*;
 
import java.net.IDN;
 
import org.junit.Test;
 
public class EMailAddressTest {
private static final String ExamplePunycode = "xn--zckzah"; // Japanese IDN Test TLD
private static final String ExampleUnicode = IDN.toUnicode(ExamplePunycode);
@Test
public void testAddressParsing() throws InvalidMailAddressException {
try {
new EMailAddress(null);
fail();
} catch (InvalidMailAddressException e) {
}
try {
new EMailAddress("");
fail();
} catch (InvalidMailAddressException e) {
}
try {
new EMailAddress("bla");
fail();
} catch (InvalidMailAddressException e) {
}
EMailAddress a;
// a = new EMailAddress("@");
// assertEquals(a.getLocalPartUnicode(), "");
// // assertEquals(a.getLocalPartASCII(), "");
// assertEquals(a.getDomainPartUnicode(), "");
// assertEquals(a.getDomainPartASCII(), "");
// assertEquals(a.getTldUnicode(), "");
// assertEquals(a.getTldASCII(), "");
// assertEquals(a.getTldUnicode(), "");
// assertEquals(a.getMailAddressASCII(), "");
try {
new EMailAddress("@");
// Es wird InvalidMailAddressException anstelle von
// local="" und domain="" ausgegeben,
// weil .split nicht so wie .explode reagiert
fail();
} catch (InvalidMailAddressException e) {
}
 
a = new EMailAddress("local@domain");
assertEquals(a.getLocalPart(), "local");
// assertEquals(a.getLocalPartASCII(), "local");
assertEquals(a.getDomainPartUnicode(), "domain");
assertEquals(a.getDomainPartPunycode(), "domain");
assertEquals(a.getTldUnicode(), "");
assertEquals(a.getTldPunycode(), "");
assertEquals(a.toString(), "local@domain");
assertEquals(a.getMailAddressUnicode(), "local@domain");
assertEquals(a.getMailAddressPunycodedDomain(), "local@domain");
 
a = new EMailAddress("local@domain.tld");
assertEquals(a.getLocalPart(), "local");
// assertEquals(a.getlocalPartASCII(), "local");
assertEquals(a.getDomainPartUnicode(), "domain.tld");
assertEquals(a.getDomainPartPunycode(), "domain.tld");
assertEquals(a.getTldUnicode(), "tld");
assertEquals(a.getTldPunycode(), "tld");
assertEquals(a.toString(), "local@domain.tld");
assertEquals(a.getMailAddressUnicode(), "local@domain.tld");
assertEquals(a.getMailAddressPunycodedDomain(), "local@domain.tld");
a = new EMailAddress("local@"+ExampleUnicode+".jp");
assertEquals(a.getLocalPart(), "local");
// assertEquals(a.getlocalPartASCII(), "local");
assertEquals(a.getDomainPartUnicode(), ExampleUnicode+".jp");
assertEquals(a.getDomainPartPunycode(), ExamplePunycode+".jp");
assertEquals(a.getTldUnicode(), "jp");
assertEquals(a.getTldPunycode(), "jp");
assertEquals(a.getMailAddressUnicode(), "local@"+ExampleUnicode+".jp");
assertEquals(a.getMailAddressPunycodedDomain(), "local@"+ExamplePunycode+".jp");
EMailAddress.USE_UNICODE_AS_STANDARD = true;
assertEquals(a.toString(), "local@"+ExampleUnicode+".jp");
EMailAddress.USE_UNICODE_AS_STANDARD = false;
assertEquals(a.toString(), "local@"+ExamplePunycode+".jp");
 
a = new EMailAddress("local@example."+ExampleUnicode);
assertEquals(a.getLocalPart(), "local");
// assertEquals(a.getlocalPartASCII(), "local");
assertEquals(a.getDomainPartUnicode(), "example."+ExampleUnicode);
assertEquals(a.getDomainPartPunycode(), "example."+ExamplePunycode);
assertEquals(a.getTldUnicode(), ExampleUnicode);
assertEquals(a.getTldPunycode(), ExamplePunycode);
assertEquals(a.getMailAddressUnicode(), "local@example."+ExampleUnicode);
assertEquals(a.getMailAddressPunycodedDomain(), "local@example."+ExamplePunycode);
EMailAddress.USE_UNICODE_AS_STANDARD = true;
assertEquals(a.toString(), "local@example."+ExampleUnicode);
EMailAddress.USE_UNICODE_AS_STANDARD = false;
assertEquals(a.toString(), "local@example."+ExamplePunycode);
}
@Test
public void testIsUnicode() {
assertFalse(EMailAddress.isUnicode(null));
assertFalse(EMailAddress.isUnicode(""));
assertFalse(EMailAddress.isUnicode(ExamplePunycode));
assertTrue(EMailAddress.isUnicode(ExampleUnicode));
}
@Test
public void testIsPunycode() {
assertFalse(EMailAddress.isPunycode(null));
assertFalse(EMailAddress.isPunycode(""));
assertTrue(EMailAddress.isPunycode(ExamplePunycode));
assertFalse(EMailAddress.isPunycode(ExampleUnicode));
}
@Test
public void testClone() throws InvalidMailAddressException, CloneNotSupportedException {
EMailAddress a = new EMailAddress("local@example."+ExampleUnicode);
EMailAddress b = (EMailAddress) a.clone();
assertFalse(a == b);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
assertEquals(a.getDomainPartPunycode(), b.getDomainPartPunycode());
assertEquals(a.getDomainPartUnicode(), b.getDomainPartUnicode());
assertEquals(a.getLocalPart(), b.getLocalPart());
assertEquals(a.getMailAddressPunycodedDomain(), b.getMailAddressPunycodedDomain());
assertEquals(a.getMailAddressUnicode(), b.getMailAddressUnicode());
assertEquals(a.getTldPunycode(), b.getTldPunycode());
assertEquals(a.getTldUnicode(), b.getTldUnicode());
EMailAddress.USE_UNICODE_AS_STANDARD = true;
assertEquals(a.toString(), b.toString());
EMailAddress.USE_UNICODE_AS_STANDARD = false;
assertEquals(a.toString(), b.toString());
}
@Test
public void testEquals() throws InvalidMailAddressException {
EMailAddress a = new EMailAddress("local@example."+ExampleUnicode);
EMailAddress b = new EMailAddress("local@example."+ExampleUnicode);
assertFalse(a == b);
assertTrue(a.equals(b));
assertTrue(b.equals(a));
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/.classpath
1,6 → 1,7
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="C:/eclipse/httpcomponents-client-4.0.1/lib/apache-mime4j-0.6.jar"/>
<classpathentry kind="lib" path="C:/eclipse/httpcomponents-client-4.0.1/lib/commons-codec-1.3.jar"/>
8,5 → 9,8
<classpathentry kind="lib" path="C:/eclipse/httpcomponents-client-4.0.1/lib/httpclient-4.0.1.jar"/>
<classpathentry kind="lib" path="C:/eclipse/httpcomponents-client-4.0.1/lib/httpcore-4.0.1.jar"/>
<classpathentry kind="lib" path="C:/eclipse/httpcomponents-client-4.0.1/lib/httpmime-4.0.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="/QSetWebApp/data/TomcatLib/activation.jar"/>
<classpathentry kind="lib" path="/QSetWebApp/data/TomcatLib/mail.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/AuthentificateDataIncompleteException.java
0,0 → 1,7
package de.viathinksoft.utils.mail.sender;
 
public class AuthentificateDataIncompleteException extends Exception {
private static final long serialVersionUID = 4081036989112713674L;
 
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/PlainTextMailSender.java
0,0 → 1,52
package de.viathinksoft.utils.mail.sender;
 
import javax.mail.MessagingException;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.InvalidMailAddressException;
 
public class PlainTextMailSender extends RawMailSender {
// --- E-Mail-Adressobjekt benutzen (dekodiert automatisch den Punycode)
public void setMailFrom(String mailFrom) throws InvalidMailAddressException {
this.setMailFrom(new EMailAddress(mailFrom));
}
 
public void setMailFrom(EMailAddress mailFrom) throws InvalidMailAddressException {
if (mailFrom == null) throw new InvalidMailAddressException();
super.setMailFrom(mailFrom.getMailAddressPunycodedDomain());
}
 
public void setRecipient(String recipient) throws InvalidMailAddressException {
this.setRecipient(new EMailAddress(recipient));
}
 
public void setRecipient(EMailAddress recipient) throws InvalidMailAddressException {
if (recipient == null) throw new InvalidMailAddressException();
super.setRecipient(recipient.getMailAddressPunycodedDomain());
}
// --- PlainText Implementieren
private String message;
 
public String getMessage() {
return message;
}
 
public void setMessage(String message) {
this.message = message;
if (this.message == null)
this.message = "";
}
 
protected void generateMailObject() throws MessagingException,
AuthentificateDataIncompleteException {
 
super.generateMailObject();
msg.setContent(message, "text/plain");
}
 
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/RawMailSender.java
0,0 → 1,206
package de.viathinksoft.utils.mail.sender;
 
// Ref:
// http://openbook.galileocomputing.de/javainsel8/javainsel_18_012.htm#mjb306e4632c440d0524d00f224d4fa1bb - Kapitel 18.12.6
// http://java.sun.com/developer/onlineTraining/JavaMail/contents.html
 
import java.util.Properties;
 
import javax.mail.Authenticator;
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;
 
import de.viathinksoft.utils.mail.InvalidMailAddressException;
 
abstract public class RawMailSender {
 
private static final String TRANSPORT_PROTOCOL = "smtp";
 
protected Session session;
protected Message msg;
protected Properties props;
protected Authenticator auth;
 
private String smtpHost = "localhost";
private String smtpUsername = "";
private String smtpPassword = "";
private int smtpPort = 25;
private boolean smtpAuth = false;
// private String smtpAuthUser = "";
// private String smtpAuthPass = "";
private String recipient = "";
private String subject = "";
private String mailFrom = "";
 
public String getMailFrom() {
return mailFrom;
}
 
public void setMailFrom(String mailFrom) throws InvalidMailAddressException {
if (mailFrom == null) throw new InvalidMailAddressException();
this.mailFrom = mailFrom.trim();
}
 
public String getRecipient() {
return recipient;
}
 
public void setRecipient(String recipient) throws InvalidMailAddressException {
if (recipient == null) throw new InvalidMailAddressException();
this.recipient = recipient.trim();
}
 
public String getSubject() {
return subject;
}
 
public void setSubject(String subject) {
this.subject = subject;
if (this.subject == null)
this.subject = "";
}
 
public String getSmtpHost() {
return smtpHost;
}
 
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
if (this.smtpHost == null)
this.smtpHost = "";
}
 
public String getSmtpUsername() {
return smtpUsername;
}
 
public void setSmtpUsername(String smtpUsername) {
this.smtpUsername = smtpUsername;
if (this.smtpUsername == null)
this.smtpUsername = "";
}
 
public String getSmtpPassword() {
return smtpPassword;
}
 
public void setSmtpPassword(String smtpPassword) {
this.smtpPassword = smtpPassword;
if (this.smtpPassword == null)
this.smtpPassword = "";
}
 
public boolean isSmtpAuth() {
return smtpAuth;
}
 
public void setSmtpAuth(boolean smtpAuth) {
this.smtpAuth = smtpAuth;
}
 
// public String getSmtpAuthUser() {
// return smtpAuthUser;
// }
//
// public void setSmtpAuthUser(String smtpAuthUser) {
// this.smtpAuthUser = smtpAuthUser;
// if (this.smtpAuthUser == null) this.smtpAuthUser = "";
// }
//
// public String getSmtpAuthPass() {
// return smtpAuthPass;
// }
//
// public void setSmtpAuthPass(String smtpAuthPass) {
// this.smtpAuthPass = smtpAuthPass;
// if (this.smtpAuthPass == null) this.smtpAuthPass = "";
// }
 
public int getSmtpPort() {
return smtpPort;
}
 
public void setSmtpPort(int smtpPort) {
this.smtpPort = smtpPort;
}
 
protected void generateProperties()
throws AuthentificateDataIncompleteException {
props = new Properties();
 
// http://72.5.124.55/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
if ((smtpHost != null) && (!"".equals(smtpHost))) {
props.put("mail.smtp.host", smtpHost);
}
props.setProperty("mail.transport.protocol", TRANSPORT_PROTOCOL);
props.setProperty("mail.smtp.port", "" + smtpPort);
if ((smtpUsername != null) && (!"".equals(smtpUsername))) {
props.setProperty("mail.smtp.user", smtpUsername);
}
 
props.put("mail.smtp.auth", smtpAuth);
if (smtpAuth) {
String smtpAuthUser = smtpUsername;
String smtpAuthPass = smtpPassword;
 
if ((smtpAuthUser != null) && (!"".equals(smtpAuthUser))) {
if ((smtpAuthPass != null) && (!"".equals(smtpAuthPass))) {
auth = new SmtpAuthenticator(smtpAuthUser, smtpAuthPass);
} else {
throw new AuthentificateDataIncompleteException();
}
} else {
throw new AuthentificateDataIncompleteException();
}
} else {
auth = null;
}
 
if ((mailFrom != null) && (!"".equals(mailFrom))) {
props.setProperty("mail.smtp.from", mailFrom);
}
}
 
protected void generateSession() {
// http://blog.dafuer.de/2006/08/22/javamail-access-to-default-session-denied/
// final Session session = Session.getDefaultInstance(props, auth);
session = Session.getInstance(props, auth);
}
 
protected void generateMailObject() throws MessagingException,
AuthentificateDataIncompleteException {
 
/* final Message */msg = new MimeMessage(session);
 
final InternetAddress addressTo = new InternetAddress(recipient);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
 
if ((mailFrom != null) && (!"".equals(mailFrom))) {
final InternetAddress addressFrom = new InternetAddress(mailFrom);
msg.setFrom(addressFrom);
}
}
 
protected void doSend() throws MessagingException {
Transport tr = session.getTransport(TRANSPORT_PROTOCOL);
tr.connect(smtpHost, smtpPort, smtpUsername, smtpPassword);
msg.saveChanges(); // don't forget this
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
}
 
public void sendMail() throws MessagingException,
AuthentificateDataIncompleteException {
 
// Damit die Funktionalität später erweitert oder verändert werden kann!
generateProperties();
generateSession();
generateMailObject();
doSend();
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/SmtpAuthenticator.java
0,0 → 1,30
package de.viathinksoft.utils.mail.sender;
 
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
 
class SmtpAuthenticator extends Authenticator {
// Ref: http://forum.javacore.de/viewtopic.php?p=60996#60996
 
String pass = "";
String login = "";
 
public SmtpAuthenticator() {
super();
}
 
public SmtpAuthenticator(String login, String pass) {
super();
 
this.login = login;
this.pass = pass;
}
 
public PasswordAuthentication getPasswordAuthentication() {
if ("".equals(pass)) {
return null;
} else {
return new PasswordAuthentication(login, pass);
}
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/syntaxchecker/MailSyntaxChecker.java
0,0 → 1,219
package de.viathinksoft.utils.mail.syntaxchecker;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.regex.Pattern;
 
import de.viathinksoft.utils.mail.EMailAddress;
import de.viathinksoft.utils.mail.InvalidMailAddressException;
 
/**
*
* @author Daniel Marschall
*
*/
public class MailSyntaxChecker {
private static final String REGEX_IP = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
 
// Führt eine Prüfung der E-Mail-Adresse gemäß SMTP-Spezifikation RFC 5321
// aus
private static final boolean CHECK_SMTP_SIZE_LIMITS = false;
 
// Führt eine Prüfung der TLD gemäß IANA-Daten aus
private static final boolean CHECK_TLD_RECOGNIZED = true;
 
// Führt eine DNS-Prüfung durch
private static final boolean CHECK_DNS = true;
 
// http://data.iana.org/TLD/tlds-alpha-by-domain.txt
// Version 2010052500, Last Updated Tue May 25 14:07:02 2010 UTC
private static final HashSet<String> RECOGNIZED_TLDS_PUNYCODE = hmaker(new String[] {
"AC", "AD", "AE", "AERO", "AF", "AG", "AI", "AL", "AM", "AN", "AO",
"AQ", "AR", "ARPA", "AS", "ASIA", "AT", "AU", "AW", "AX", "AZ",
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BIZ", "BJ", "BM",
"BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CAT",
"CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO",
"COM", "COOP", "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ",
"DK", "DM", "DO", "DZ", "EC", "EDU", "EE", "EG", "ER", "ES", "ET",
"EU", "FI", "FJ", "FK", "FM", "FO", "FR", "GA", "GB", "GD", "GE",
"GF", "GG", "GH", "GI", "GL", "GM", "GN", "GOV", "GP", "GQ", "GR",
"GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU",
"ID", "IE", "IL", "IM", "IN", "INFO", "INT", "IO", "IQ", "IR",
"IS", "IT", "JE", "JM", "JO", "JOBS", "JP", "KE", "KG", "KH", "KI",
"KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI",
"LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME",
"MG", "MH", "MIL", "MK", "ML", "MM", "MN", "MO", "MOBI", "MP",
"MQ", "MR", "MS", "MT", "MU", "MUSEUM", "MV", "MW", "MX", "MY",
"MZ", "NA", "NAME", "NC", "NE", "NET", "NF", "NG", "NI", "NL",
"NO", "NP", "NR", "NU", "NZ", "OM", "ORG", "PA", "PE", "PF", "PG",
"PH", "PK", "PL", "PM", "PN", "PR", "PRO", "PS", "PT", "PW", "PY",
"QA", "RE", "RO", "RS", "RU", "RW", "SA", "SB", "SC", "SD", "SE",
"SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST",
"SU", "SV", "SY", "SZ", "TC", "TD", "TEL", "TF", "TG", "TH", "TJ",
"TK", "TL", "TM", "TN", "TO", "TP", "TR", "TRAVEL", "TT", "TV",
"TW", "TZ", "UA", "UG", "UK", "US", "UY", "UZ", "VA", "VC", "VE",
"VG", "VI", "VN", "VU", "WF", "WS", "XN--0ZWM56D",
"XN--11B5BS3A9AJ6G", "XN--80AKHBYKNJ4F", "XN--9T4B11YI5A",
"XN--DEBA0AD", "XN--G6W251D", "XN--HGBK6AJ7F53BBA",
"XN--HLCJ6AYA9ESC7A", "XN--JXALPDLP", "XN--KGBECHTV",
"XN--MGBAAM7A8H", "XN--MGBERP4A5D4AR", "XN--P1AI", "XN--WGBH1C",
"XN--ZCKZAH", "YE", "YT", "ZA", "ZM", "ZW", });
 
private static boolean checkSmtpSizeLimits(EMailAddress email) {
// RFC 5321: 4.5.3.1.1. Local-part Längenbegrenzung bei SMTP: 64
// Byte
// QUE: Soll das auch als Punicode-Variante geprüft werden?
if ((email.getLocalPart().length() > 64)
|| (email.getLocalPart().length() < 1)) {
return false;
}
 
// RFC 5321: 4.5.3.1.2. Domain-part Längenbegrenzung bei SMTP: 255
// Byte
if ((email.getDomainPartPunycode().length() > 255)
|| (email.getDomainPartPunycode().length() < 1)) {
return false;
}
 
// RFC 5321: 4.5.3.1.5. Reply-Line Längenbegrenzung bei SMTP: 512
// Byte. Laut
// http://de.wikipedia.org/wiki/E-Mail-Adresse#L.C3.A4nge_der_E-Mail-Adresse
// folgt daraus: Länge der MailAddresse ist 254 Bytes.
if (email.getMailAddressPunycodedDomain().length() > 254) {
return false;
}
 
return true;
}
 
private static boolean checkTldRecognized(EMailAddress email) {
// TODO: Mailadressen sind aber auch als ...@[IP] gültig. Dann keine
// TLD!
return RECOGNIZED_TLDS_PUNYCODE.contains(email.getTldPunycode()
.toUpperCase());
}
 
private static boolean preg_match(String regex, String data) {
return Pattern.compile(regex).matcher(data).matches();
}
private static boolean checkDns(String domainOrIP) {
// TODO
return true;
}
 
public static boolean isMailValid(String email)
throws InvalidMailAddressException {
return isMailValid(new EMailAddress(email));
}
 
/**
* Checks if an E-Mail-Address is valid
*
* @param email
* @return
*/
public static boolean isMailValid(EMailAddress email) {
if (CHECK_SMTP_SIZE_LIMITS) {
if (!checkSmtpSizeLimits(email))
return false;
}
 
// Begin RFC-Checks
 
final String address = email.getMailAddressUnicode();
final String localPart = email.getLocalPart();
final String domainPart = email.getDomainPartPunycode();
 
// Weder localPart noch domainPart dürfen zwei aufeinanderfolgende
// Punkte besitzen.
 
if (address.contains("..")) {
return false;
}
 
// localPart darf keine Punkte am Anfang oder Ende besitzen
if (localPart.length() == 0) return false;
String lpFirstChar = localPart.substring(0, 1);
String lpLastChar = localPart.substring(localPart.length()-1);
if (lpFirstChar.equals(".") || (lpLastChar.equals("."))) {
return false;
}
 
// domainPart darf keine Punkte am Anfang oder Ende besitzen
 
String dpFirstChar = domainPart.substring(0, 1);
String dpLastChar = domainPart.substring(domainPart.length()-1);
 
if (dpFirstChar.equals(".") || (dpLastChar.equals("."))) {
return false;
}
 
// domainPart prüfen
if (preg_match("^"+REGEX_IP+"$", domainPart)) {
// domainPart is <IP>
// QUE: Ist das überhaupt gemäß RFC gültig?
String ip = ""; // TODO
if (CHECK_DNS) {
if (!checkDns(ip)) return false;
}
} else if (preg_match("^\\["+REGEX_IP+"\\]$", domainPart)) {
// domainPart is [<IP>]
String ip = ""; // TODO
if (CHECK_DNS) {
if (!checkDns(ip)) return false;
}
} else {
if (!preg_match("^[A-Za-z0-9\\-\\.]+$", domainPart)) {
return false;
}
 
if (CHECK_TLD_RECOGNIZED) {
if (!checkTldRecognized(email))
return false;
}
if (CHECK_DNS) {
if (!checkDns(domainPart)) return false;
}
}
// localPart prüfen
if (!preg_match("^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$",
localPart.replaceAll("\\\\", "").replaceAll("@", "") )) {
// character not valid in local part unless
// local part is quoted
if (!preg_match("^\"(\\\\\"|[^\"])+\"$",
localPart.replaceAll("\\\\", "").replaceAll("@", "") )) {
return false;
}
}
// TODO: Weitere Tests gemäß RFC?
return true;
}
 
/**
* build a HashSet from a array of String literals.
*
* @param list
* array of strings
*
* @return HashSet you can use to test if a string is in the set.
*/
private static HashSet<String> hmaker(String[] list) {
HashSet<String> map = new HashSet<String>(Math.max(
(int) (list.length / .75f) + 1, 16));
map.addAll(Arrays.asList(list));
return map;
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/EMailAddress.java
0,0 → 1,330
package de.viathinksoft.utils.mail;
 
import java.net.IDN;
 
import de.viathinksoft.utils.mail.syntaxchecker.MailSyntaxChecker;
 
/**
*
* This class parses an email address (trims whitespaces from it) and stores it
* in its original form as well as store a RFC-compatible punycoded domainpart.
* So, if you enter a Unicode-Mail-Address you can easily access the trimmed and
* punycoded domain-part mail-address. Warning! This class does NOT check if the
* email address is fully valid. Please use the syntax checker class for this.
*
* @author Daniel Marschall
*
*/
public class EMailAddress {
 
// Constants
 
/**
* This constant is used by toString() and tells if whether
* getMailAddressUnicode() or getMailAddressPunycodedDomain() should be
* returned.
*/
static boolean USE_UNICODE_AS_STANDARD = false;
// Attributes
 
/**
* The local part of our parsed mail address. (Part before "@") It is
* allways Unicode, since the mail servers have to take care about it. Even
* if Unicode mail addresses will become popular in future, the local part
* will probably not punycoded.
*/
private String localPart;
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Unicode form.
*/
private String domainPartUnicode;
 
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Punycode (ASCII) form.
*/
private String domainPartPunycode;
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Unicode form.
*/
private String tldUnicode;
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Punycode form.
*/
private String tldPunycode;
 
// Getter and Setter
 
/**
* The local part of our parsed mail address. (Part before "@") It is
* allways Unicode, since the mail servers have to take care about it. Even
* if Unicode mail addresses will become popular in future, the local part
* will probably not punycoded.
*
* @return The local part
*/
public String getLocalPart() {
return localPart;
}
 
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Unicode form.
*
* @return The domain part in Unicode.
*/
public String getDomainPartUnicode() {
return domainPartUnicode;
}
 
/**
* The domain part of our parsed mail address (part after "@") inclusive our
* top level domain (TLD). It is in its Punycode (ASCII) form.
*
* @return The domain part in Punycode.
*/
public String getDomainPartPunycode() {
return domainPartPunycode;
}
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Unicode form.
*
* @return The TLD in Unicode.
*/
public String getTldUnicode() {
return tldUnicode;
}
 
/**
* The top level domain (COM, ORG, BIZ...) of our parsed mail address. The
* dot is not included. It is in its Punycode form.
*
* @return The TLD in Punycode.
*/
public String getTldPunycode() {
return tldPunycode;
}
 
// Constructors
 
/**
* Creates an email address object out of an email address string.
*
* @param eMailAddress
* bare computer email address. e.g. roedyg@mindprod.com No
* "Roedy Green" <roedyg@mindprod.com> style addresses. No local
* addresses, e.g. roedy.
* @throws InvalidMailAddressException
*/
public EMailAddress(String eMailAddress) throws InvalidMailAddressException {
super();
 
if (eMailAddress == null) {
throw new InvalidMailAddressException();
}
 
// Zuerst trimmen (z.B. für Formulardaten) Wir splitten dann beim
// At-Zeichen (@) und berücksichtigen ein escaped-At
// (\@)
String[] res = eMailAddress.trim().split("(?<!\\\\)@");
 
// @-sign was not used once
if (res.length != 2) {
throw new InvalidMailAddressException();
}
 
// Temporary we store the values here.
 
String localPart = res[0];
String domainPart = res[1];
 
// We parse the local part.
 
if (localPart == null)
localPart = "";
this.localPart = localPart;
 
// We parse the domainPart and allocate punycode and unicode fields.
 
if (domainPart == null)
domainPart = "";
if (isUnicode(domainPart)) {
this.domainPartUnicode = domainPart;
this.domainPartPunycode = IDN.toASCII(domainPart);
} else /* if (isPunycode(domainPart)) */{
this.domainPartUnicode = IDN.toUnicode(domainPart);
this.domainPartPunycode = domainPart;
}
 
// We additionally parse the TLD and also determinate if it is punycode
// or not.
 
int dotIdx;
 
dotIdx = this.domainPartUnicode.lastIndexOf('.');
if (dotIdx >= 0) {
this.tldUnicode = this.domainPartUnicode.substring(dotIdx + 1);
} else {
// We do not throw an exception here because it could be an email to
// a network computer or an IP address.
this.tldUnicode = "";
}
 
dotIdx = this.domainPartPunycode.lastIndexOf('.');
if (dotIdx >= 0) {
this.tldPunycode = this.domainPartPunycode.substring(dotIdx + 1);
} else {
// We do not throw an exception here because it could be an email to
// a network computer or an IP address.
this.tldPunycode = "";
}
}
 
// Methods
 
/**
* Returns the email address with punycoded domain name and TLD. You should use
* this method to send emails.
*
* @return The email address with punycoded domain name and TLD.
*/
public String getMailAddressPunycodedDomain() {
return this.localPart + "@" + this.domainPartPunycode;
}
 
/**
* Returns the email address with internationalized domain names and TLD.
*
* @return The email address with internationalized domain name and TLD.
*/
public String getMailAddressUnicode() {
return this.localPart + "@" + this.domainPartUnicode;
}
 
/**
* Returns a string which represents the mail address. If the constant
* USE_UNICODE_AS_STANDARD is true, the internationalized domain names will
* not translated into the corresponding Punycode. If false, then not.
*
* @return The string which represents the mail address. Warning! Since this
* method is rather designed to show a formatted mail address, it
* should NOT be used to send emails. Please only use this function
* if you want to output.
*/
@Override
public String toString() {
if (USE_UNICODE_AS_STANDARD) {
return this.getMailAddressUnicode();
} else {
return this.getMailAddressPunycodedDomain();
}
}
 
/**
* Checks if an object is equal to our email address object.
*
* @return Boolean which describes if it is equal or not.
*/
@Override
public boolean equals(Object obj) {
// Initial checks
 
if (this == obj)
return true;
if (obj == null)
return false;
if (obj.getClass() != getClass())
return false;
 
// Compare the fields
 
if (!this.domainPartPunycode
.equals(((EMailAddress) obj).domainPartPunycode)) {
return false;
}
if (!this.domainPartUnicode
.equals(((EMailAddress) obj).domainPartUnicode)) {
return false;
}
if (!this.localPart.equals(((EMailAddress) obj).localPart)) {
return false;
}
if (!this.tldUnicode.equals(((EMailAddress) obj).tldUnicode)) {
return false;
}
if (!this.tldPunycode.equals(((EMailAddress) obj).tldPunycode)) {
return false;
}
 
// Everything's fine ^^
 
return true;
 
// return this.toString().equals(obj.toString());
}
 
/**
* Creates a deep copy of the email address object.
*
* @return A new instance of the email address object with the same
* properties.
*/
@Override
protected EMailAddress clone() throws CloneNotSupportedException {
try {
return new EMailAddress(this.toString());
} catch (InvalidMailAddressException e) {
return null;
}
}
 
/**
* Asks the mail syntax checker if the current mail address is valid or not.
* Warning! This check is NOT performed automatically. There is no guarantee
* that the syntax check is 100% correct. There might be mail address which
* are valid but marked as invalid (because server disobeyed RFC rules etc)
* and mail addresses which are invalid but marked valid (e.g. simply if
* they were not assigned).
*
* @return Boolean which represents if the mail address is valid or not.
*/
public boolean isSyntaxValid() {
return MailSyntaxChecker.isMailValid(this);
}
 
// ---------- STATIC FUNCTIONS ----------
 
/**
* Determinates if a given string can be converted into Punycode.
*
* @param str
* The string which should be checked
* @return Boolean which shows if the string is not yet punicoded.
*/
protected static boolean isUnicode(String str) {
if (str == null)
return false;
return (!IDN.toASCII(str).equals(str));
}
 
/**
* Determinates if a given string is in Punycode format.
*
* @param str
* The string which should be checked
* @return Boolean which shows if the string is punycoded or not.
*/
protected static boolean isPunycode(String str) {
if (str == null)
return false;
return (!IDN.toUnicode(str).equals(str));
}
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/InvalidMailAddressException.java
0,0 → 1,7
package de.viathinksoft.utils.mail;
 
public class InvalidMailAddressException extends Exception {
 
private static final long serialVersionUID = -3748914913077717465L;
 
}
Property changes:
Added: svn:mime-type
+text/plain
\ No newline at end of property