Ejemplo n.º 1
0
 /**
  * Send Email. Use string array to represents attachments file names.
  *
  * @see #sendEmail(String, String, String[], String[], String[], String, File[])
  */
 private static void sendEmail(String smtpHost, MailMessage mail, boolean isAnonymousEmail) {
   if (mail == null) {
     throw new IllegalArgumentException("Param mail can not be null.");
   }
   String[] fileNames = mail.getFileNames();
   // only needs to check the param: fileNames, other params would be checked through
   // the override method.
   File[] files = null;
   if (fileNames != null && fileNames.length > 0) {
     files = new File[fileNames.length];
     for (int i = 0; i < files.length; i++) {
       File file = new File(fileNames[i]);
       files[i] = file;
     }
   }
   sendEmail(
       smtpHost,
       mail.getSubject(),
       mail.getFrom(),
       mail.getTos(),
       mail.getCcs(),
       mail.getBccs(),
       mail.getContent(),
       files,
       isAnonymousEmail);
 }
Ejemplo n.º 2
0
 /**
  * Send anonymous email. Note that although we could give any address as from address, (for
  * example: <b>'[email protected]' is valid</b>), the from of MailMessage should always be the correct format
  * of email address(for example the <b>'aaaa' is invalid</b>). Otherwise an exception would be
  * thrown say that username is invalid.
  *
  * @param mail The MailMessage object which contains at least all the required attributes to be
  *     sent.
  */
 public static void sendAnonymousEmail(MailMessage mail) {
   String dns = "dns://";
   Hashtable<String, String> env = new Hashtable<String, String>();
   env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
   env.put(Context.PROVIDER_URL, dns);
   String[] tos = mail.getTos();
   try {
     DirContext ctx = new InitialDirContext(env);
     for (String to : tos) {
       String domain = to.substring(to.indexOf('@') + 1);
       // Get MX(Mail eXchange) records from DNS
       Attributes attrs = ctx.getAttributes(domain, new String[] {"MX"});
       if (attrs == null || attrs.size() <= 0) {
         throw new java.lang.IllegalStateException(
             "Error: Your DNS server has no Mail eXchange records!");
       }
       @SuppressWarnings("rawtypes")
       NamingEnumeration servers = attrs.getAll();
       String smtpHost = null;
       boolean isSend = false;
       StringBuffer buf = new StringBuffer();
       // try all the mail exchange server to send the email.
       while (servers.hasMore()) {
         Attribute hosts = (Attribute) servers.next();
         for (int i = 0; i < hosts.size(); ++i) {
           // sample: 20 mx2.qq.com
           smtpHost = (String) hosts.get(i);
           // parse the string to get smtpHost. sample: mx2.qq.com
           smtpHost = smtpHost.substring(smtpHost.lastIndexOf(' ') + 1);
           try {
             sendEmail(smtpHost, mail, true);
             isSend = true;
             return;
           } catch (Exception e) {
             LOGGER.error("", e);
             buf.append(e.toString()).append("\r\n");
             continue;
           }
         }
       }
       if (!isSend) {
         throw new java.lang.IllegalStateException("Error: Send email error." + buf.toString());
       }
     }
   } catch (NamingException e) {
     LOGGER.error("", e);
   }
 }