/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/PlainTextMailSender.java |
---|
3,27 → 3,24 |
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 { |
public void setMailFrom(String mailFrom) { |
this.setMailFrom(new EMailAddress(mailFrom)); |
} |
public void setMailFrom(EMailAddress mailFrom) throws InvalidMailAddressException { |
if (mailFrom == null) throw new InvalidMailAddressException(); |
public void setMailFrom(EMailAddress mailFrom) { |
super.setMailFrom(mailFrom.getMailAddressPunycodedDomain()); |
} |
public void setRecipient(String recipient) throws InvalidMailAddressException { |
public void setRecipient(String recipient) { |
this.setRecipient(new EMailAddress(recipient)); |
} |
public void setRecipient(EMailAddress recipient) throws InvalidMailAddressException { |
if (recipient == null) throw new InvalidMailAddressException(); |
public void setRecipient(EMailAddress recipient) { |
super.setRecipient(recipient.getMailAddressPunycodedDomain()); |
} |
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/sender/RawMailSender.java |
---|
14,8 → 14,6 |
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"; |
40,8 → 38,7 |
return mailFrom; |
} |
public void setMailFrom(String mailFrom) throws InvalidMailAddressException { |
if (mailFrom == null) throw new InvalidMailAddressException(); |
public void setMailFrom(String mailFrom) { |
this.mailFrom = mailFrom.trim(); |
} |
49,8 → 46,7 |
return recipient; |
} |
public void setRecipient(String recipient) throws InvalidMailAddressException { |
if (recipient == null) throw new InvalidMailAddressException(); |
public void setRecipient(String recipient) { |
this.recipient = recipient.trim(); |
} |
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/syntaxchecker/MailSyntaxChecker.java |
---|
5,11 → 5,13 |
import java.util.regex.Pattern; |
import de.viathinksoft.utils.mail.EMailAddress; |
import de.viathinksoft.utils.mail.InvalidMailAddressException; |
/** |
* This class is not stable. For a good syntax check, please use the classes of |
* Dominic Sayers or Cal Henderson. |
* |
* @author Daniel Marschall |
* @version 0.1 |
* |
*/ |
public class MailSyntaxChecker { |
104,8 → 106,7 |
return true; |
} |
public static boolean isMailValid(String email) |
throws InvalidMailAddressException { |
public static boolean isMailValid(String email) { |
return isMailValid(new EMailAddress(email)); |
} |
135,19 → 136,17 |
} |
// 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("."))) { |
if (localPart.length() == 0) { |
return false; |
} |
if (localPart.startsWith(".") || localPart.endsWith(".")) { |
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("."))) { |
if (domainPart.startsWith(".") || domainPart.endsWith(".")) { |
return false; |
} |
160,7 → 159,8 |
String ip = ""; // TODO |
if (CHECK_DNS) { |
if (!checkDns(ip)) return false; |
if (!checkDns(ip)) |
return false; |
} |
} else if (preg_match("^\\["+REGEX_IP+"\\]$", domainPart)) { |
// domainPart is [<IP>] |
168,7 → 168,8 |
String ip = ""; // TODO |
if (CHECK_DNS) { |
if (!checkDns(ip)) return false; |
if (!checkDns(ip)) |
return false; |
} |
} else { |
if (!preg_match("^[A-Za-z0-9\\-\\.]+$", domainPart)) { |
181,7 → 182,8 |
} |
if (CHECK_DNS) { |
if (!checkDns(domainPart)) return false; |
if (!checkDns(domainPart)) |
return false; |
} |
} |
191,8 → 193,8 |
localPart.replaceAll("\\\\", "").replaceAll("@", "") )) { |
// character not valid in local part unless |
// local part is quoted |
if (!preg_match("^\"(\\\\\"|[^\"])+\"$", |
localPart.replaceAll("\\\\", "").replaceAll("@", "") )) { |
if (!preg_match("^\"(\\\\\"|[^\"])+\"$", localPart.replaceAll( |
"\\\\", "").replaceAll("@", ""))) { |
return false; |
} |
} |
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/EMailAddress.java |
---|
2,8 → 2,6 |
import java.net.IDN; |
import de.viathinksoft.utils.mail.syntaxchecker.MailSyntaxChecker; |
/** |
* |
* This class parses an email address (trims whitespaces from it) and stores it |
122,30 → 120,25 |
* 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 { |
public EMailAddress(String eMailAddress) { |
super(); |
if (eMailAddress == null) { |
throw new InvalidMailAddressException(); |
} |
// Zuerst trimmen (z.B. für Formulardaten) |
eMailAddress = eMailAddress.trim(); |
// 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(); |
// Wir splitten dann beim At-Zeichen (@) |
String localPart = ""; |
String domainPart = ""; |
int atIndex = eMailAddress.lastIndexOf('@'); |
if (atIndex == -1) { |
localPart = eMailAddress; |
domainPart = ""; |
} else { |
localPart = eMailAddress.substring(0, atIndex); |
domainPart = eMailAddress.substring(atIndex + 1); |
} |
// Temporary we store the values here. |
String localPart = res[0]; |
String domainPart = res[1]; |
// We parse the local part. |
if (localPart == null) |
191,14 → 184,18 |
// Methods |
/** |
* Returns the email address with punycoded domain name and TLD. You should use |
* this method to send emails. |
* 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() { |
if (this.domainPartPunycode.equals("")) { |
return this.localPart; |
} else { |
return this.localPart + "@" + this.domainPartPunycode; |
} |
} |
/** |
* Returns the email address with internationalized domain names and TLD. |
206,8 → 203,12 |
* @return The email address with internationalized domain name and TLD. |
*/ |
public String getMailAddressUnicode() { |
if (this.domainPartUnicode.equals("")) { |
return this.localPart; |
} else { |
return this.localPart + "@" + this.domainPartUnicode; |
} |
} |
/** |
* Returns a string which represents the mail address. If the constant |
279,27 → 280,9 |
*/ |
@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 ---------- |
/** |
310,8 → 293,9 |
* @return Boolean which shows if the string is not yet punicoded. |
*/ |
protected static boolean isUnicode(String str) { |
if (str == null) |
if (str == null) { |
return false; |
} |
return (!IDN.toASCII(str).equals(str)); |
} |
323,8 → 307,9 |
* @return Boolean which shows if the string is punycoded or not. |
*/ |
protected static boolean isPunycode(String str) { |
if (str == null) |
if (str == null) { |
return false; |
} |
return (!IDN.toUnicode(str).equals(str)); |
} |
} |
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/EMailPreprocessor.java |
---|
12,7 → 12,7 |
*/ |
public class EMailPreprocessor { |
public static String preprocess(String eMailAddress) throws InvalidMailAddressException { |
public static String preprocess(String eMailAddress) { |
EMailAddress email = new EMailAddress(eMailAddress); |
return email.getMailAddressPunycodedDomain(); |
/ViaThinkSoft Java Utils/src/de/viathinksoft/utils/mail/InvalidMailAddressException.java |
---|
1,5 → 1,6 |
package de.viathinksoft.utils.mail; |
@Deprecated |
public class InvalidMailAddressException extends Exception { |
private static final long serialVersionUID = -3748914913077717465L; |