469,954 Members | 1,791 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,954 developers. It's quick & easy.

send mail using smtp to hotmail

I have a classe to send mails. It runs on yahoo well but with hotmail I never receive the mails.I do not receive an error neither.

I would need some strange header to make the hotmail like my email????

My code is:

Expand|Select|Wrap|Line Numbers
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.text.*;
  5. import javax.naming.*;
  6. import javax.naming.directory.*;
  7.  
  8. public class SmtpSimple
  9. {
  10.   private DataOutputStream os  = null;
  11.   private BufferedReader   is  = null;
  12.   private String           sRt = "";
  13.   private final String CRLF = "\r\n";
  14.  
  15.   private final void writeRead( boolean bReadAnswer,
  16.                                 String  sAnswerMustStartWith,
  17.                                 String  sWrite )
  18.   throws IOException, Exception
  19.   {
  20.       System.out.println("Mando->"+sWrite);
  21.     if( null != sWrite && 0 < sWrite.length() )
  22.     {
  23.       sRt += sWrite;
  24.       os.writeBytes( sWrite );
  25.     }
  26.     if( bReadAnswer )
  27.     {
  28.       String sRd = is.readLine() + "\n";
  29.       System.out.println("Recibo->"+sRd);
  30.       sRt += sRd;
  31.       if( null != sAnswerMustStartWith
  32.           && 0 < sAnswerMustStartWith.length()
  33.           && !sRd.startsWith( sAnswerMustStartWith ) )
  34.         throw new Exception( sRt );
  35.     }
  36.   }
  37.  
  38.   public synchronized final String sendEmail( String sSmtpServer,
  39.                                               String sFromAdr, String sFromRealName,
  40.                                               String sToAdr,   String sToRealName,
  41.                                               String sSubject, String sText )
  42.   throws IOException, Exception
  43.   {
  44.     sRt = "";
  45.     if( null == sSmtpServer  || 0 >= sSmtpServer.length() ||
  46.         null == sFromAdr     || 0 >= sFromAdr.length()    ||
  47.         null == sToAdr       || 0 >= sToAdr.length()      ||
  48.         (  (null == sSubject || 0 >= sSubject.length())
  49.         && (null == sText    || 0 >= sText.length())  )   )
  50.       throw new Exception( "Invalid Parameters for SmtpSimple.sendEmail()." );
  51.     if( null == sFromRealName || 0 >= sFromRealName.length() )  sFromRealName = sFromAdr;
  52.     if( null == sToRealName   || 0 >= sToRealName.length() )    sToRealName   = sToAdr;
  53.     Socket so = new Socket( sSmtpServer, 25 );
  54.     os =        new DataOutputStream( so.getOutputStream() );
  55.     is =        new BufferedReader(
  56.                 new InputStreamReader( so.getInputStream() ) );
  57.     so.setSoTimeout( 10000 );
  58.     writeRead( true, "220", null );
  59.     writeRead( true, "250", "HELO " + sSmtpServer + CRLF );
  60.     writeRead( true, "250", "MAIL FROM:<" + sFromAdr + "> "+ CRLF );
  61.     writeRead( true, "250", "RCPT TO:<" + sToAdr + ">"+ CRLF );
  62.     writeRead( true, "354", "DATA"+ CRLF );
  63.     writeRead( false, null, "Return-Path: root@plexus.kodics.de\n" );//necesaria para hotmail
  64.     writeRead( false, null, "Date: " + getDateString()+ "\n" );
  65.     writeRead( false, null, "From: " + sFromRealName + " <" + sFromAdr + ">"+ "\n" );
  66.     writeRead( false, null, "To: " + sToRealName + " <" + sToAdr + ">"+ "\n" );
  67.     writeRead( false, null, "Subject: " + sSubject + "\n");
  68.     writeRead( false, null, "Mime-Version: 1.0"+ "\n" );
  69.     writeRead( false, null, "X-Mailer: PHP v5.1"+ "\n" );
  70.     writeRead( false, null, "Content-Type: text/html; charset=iso-8859-1"+ "\n" );
  71.     writeRead( false, null, "Message-ID: <"+(new java.util.Date()).getTime()+"@plexus.kodics.de>"+ "\n" );//necesaria para hotmail
  72.     writeRead( false, null, "Content-Transfer-Encoding: 7bit"+ "\n" );
  73.     writeRead( false, null, "\n");
  74.     writeRead( false, null, "<html><body><table><tr><td>Mensaje de prueba</td></tr></table></body></html>");
  75.     //writeRead( false, null, base64encode("<html><body><table><tr><td>Mensaje de prueba</td></tr></table></body></html>\n"));
  76.     writeRead( true, "250",  CRLF+"."+ CRLF );
  77.     writeRead( true, "221", "QUIT"+ CRLF );
  78.     is.close();
  79.     os.close();
  80.     so.close();
  81.     is = null;
  82.     os = null;
  83.     return sRt;
  84.   }
  85.  
  86.   public static void main( String[] args )
  87.   {
  88.     String sSmtpServer = "";
  89.     String sFromAdr = "root@plexus.kodics.de";
  90.     String sFromRealName = "root";
  91.     String sToAdr1 = "jlconde15@hotmail.com";
  92.     String sToAdr2 = "lillae25@yahoo.de";
  93.     String sToAdr3 = "jlconde@globalia-sistemas.com";
  94.     String sToRealName = "Jose";
  95.     String sSubject = "subject text/html";
  96.     String sText = "texto body";
  97.     Iterator it;
  98.     try
  99.     {
  100.       SmtpSimple smtp = new SmtpSimple();
  101.  
  102.  
  103.       it = getServers(sToAdr1).iterator();
  104.       while (it.hasNext())
  105.       {
  106.         try
  107.         {
  108.             sSmtpServer = (String) it.next();
  109.             smtp.sendEmail( sSmtpServer, sFromAdr, sFromRealName, sToAdr1, sToRealName, sSubject, sText);
  110.             break;
  111.         }
  112.         catch (Exception e)
  113.         {
  114.             System.out.println("Error con servidor "+sSmtpServer+" ->"+e);
  115.         }
  116.       }
  117.  
  118.  
  119.  
  120.       /*
  121.       it = getServers(sToAdr2).iterator();
  122.       while (it.hasNext())
  123.       {
  124.         try
  125.         {
  126.             sSmtpServer = (String) it.next();
  127.             smtp.sendEmail( sSmtpServer, sFromAdr, sFromRealName, sToAdr2, sToRealName, sSubject, sText);
  128.             break;
  129.         }
  130.         catch (Exception e)
  131.         {
  132.             System.out.println("Error con servidor "+sSmtpServer+" ->"+e);
  133.         }
  134.       }
  135.  
  136.       */
  137.  
  138.       /*
  139.       it = getServers(sToAdr3).iterator();
  140.       while (it.hasNext())
  141.       {
  142.         try
  143.         {
  144.             sSmtpServer = (String) it.next();
  145.             smtp.sendEmail( sSmtpServer, sFromAdr, sFromRealName, sToAdr3, sToRealName, sSubject, sText);
  146.             break;
  147.         }
  148.         catch (Exception e)
  149.         {
  150.             System.out.println("Error con servidor "+sSmtpServer+" ->"+e);
  151.         }
  152.       }
  153.       */
  154.  
  155.  
  156.     }
  157.     catch( Exception ex )
  158.     {
  159.       System.out.println( "Error:\n" + ex );
  160.       System.exit( 2 );
  161.     }
  162.     System.exit( 0 );
  163.   }
  164.  
  165.   public static Vector getServers(String email) throws Exception{
  166.         Vector mxserver=new Vector();
  167.         String hostName;
  168.  
  169.         if (email.indexOf("@") == -1)
  170.         {
  171.             throw new Exception("Error E-mail address");
  172.         }
  173.         hostName = email.substring(email.indexOf("@")+1);
  174.         String mailer=hostName,ip="";
  175.  
  176.         try{
  177.             Hashtable env = new Hashtable();
  178.             env.put("java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory");
  179.             DirContext ictx = new InitialDirContext( env );
  180.             Attributes attrs = ictx.getAttributes( hostName, new String[] { "MX" });
  181.             Attribute attr1 = attrs.get( "MX" );
  182.             Vector mxweight=new Vector();
  183.             int weight=999,max=0;
  184.             for (int i=0;i<attr1.size();i++){
  185.                 String mxrec=(String)attr1.get(i);
  186.                 Integer theweight=new Integer(mxrec.substring(0,mxrec.indexOf(" ")));
  187.                 String theserver=mxrec.substring(mxrec.indexOf(" ")+1);
  188.                 if (theserver.endsWith(".")) theserver=theserver.substring(0,theserver.length()-1);
  189.                 if (theweight.compareTo(weight)<0) {
  190.                   mxweight.add(0,theweight);
  191.                   mxserver.add(0,theserver);
  192.                   weight=theweight;
  193.                 } else {
  194.                     boolean filled=false;
  195.                     for (int j=0;j<mxweight.size();j++){
  196.                         if (((Integer)mxweight.elementAt(j)).compareTo(theweight)>0) {
  197.                             mxweight.add(j,theweight);
  198.                             mxserver.add(j,theserver);
  199.                             filled=true;
  200.                             break;
  201.                         }
  202.                     }
  203.                     if (!filled) {
  204.                             mxweight.add(mxweight.size(),theweight);
  205.                             mxserver.add(mxserver.size(),theserver);
  206.                     }
  207.                 }
  208.             }
  209.         }catch (Exception e) {
  210.             throw new Exception(e.getMessage());
  211.         }
  212.         if (mxserver.size()==0) mxserver.add(hostName);
  213.         return mxserver;
  214.     }
  215.  
  216.     static String getDateString(){
  217.         SimpleDateFormat formatter= new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'XXXXX'",Locale.US);
  218.         formatter.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
  219.         String str=formatter.format(new java.util.Date().getTime());
  220.         int pos = 0,start=0;
  221.         StringBuffer dateStrBuf=new StringBuffer(str);
  222.         for (pos = start + 25; dateStrBuf.charAt(pos) != 'X'; pos++) ;
  223.         Calendar calendar=Calendar.getInstance();
  224.         int offset = calendar.get(Calendar.ZONE_OFFSET) +calendar.get(Calendar.DST_OFFSET);
  225.         if (offset < 0) {
  226.             dateStrBuf.setCharAt(pos++, '-');
  227.             offset = (-offset);
  228.         } else dateStrBuf.setCharAt(pos++, '+');
  229.         int rawOffsetInMins = offset / 60 / 1000; // offset from GMT in mins
  230.         int offsetInHrs = rawOffsetInMins / 60;
  231.         int offsetInMins = rawOffsetInMins % 60;
  232.         dateStrBuf.setCharAt(pos++, Character.forDigit((offsetInHrs/10), 10));
  233.         dateStrBuf.setCharAt(pos++, Character.forDigit((offsetInHrs%10), 10));
  234.         dateStrBuf.setCharAt(pos++, Character.forDigit((offsetInMins/10), 10));
  235.         dateStrBuf.setCharAt(pos++, Character.forDigit((offsetInMins%10), 10));
  236.         return dateStrBuf.toString()+ " (CEST)";
  237.     }
  238.  
  239.     public static String base64encode(String plain) {
  240.         int maxturns;
  241.         StringBuffer sb=new StringBuffer();
  242.         byte[] enc=new byte[3];
  243.         boolean end=false;
  244.         for(int i=0,j=0; !end; i++) {
  245.             char _ch=plain.charAt(i);
  246.             if(i==plain.length()-1) end=true;
  247.             enc[j++]=(byte)plain.charAt(i);
  248.             if(j==3 || end) {
  249.                 int res;
  250.                 res=(enc[0] << 16)+(enc[1] << 8)+enc[2];
  251.                 int b;
  252.                 int lowestbit=18-(j*6);
  253.                 for(int toshift=18; toshift>=lowestbit; toshift-=6) {
  254.                     b=res >>> toshift;
  255.                     b&=63;
  256.                     if(b>=0 && b<26) sb.append((char)(b+65));
  257.                     if(b>=26 && b<52) sb.append((char)(b+71));
  258.                     if(b>=52 && b<62) sb.append((char)(b-4));
  259.                     if(b==62) sb.append('+');
  260.                     if(b==63) sb.append('/');
  261.                     if(sb.length()%76==0) sb.append("\r\n");
  262.                 }
  263.                 if(end) {
  264.                     if(j==1) sb.append("==");
  265.                     if(j==2) sb.append('=');
  266.                 }
  267.                 enc[0]=0; enc[1]=0; enc[2]=0;
  268.                 j=0;
  269.             }
  270.         }
  271.         return sb.toString();
  272.     }
  273. }
  274.  
Thanks!
Oct 10 '06 #1
0 5724

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by erdem kemer | last post: by
4 posts views Thread by PawelR | last post: by
6 posts views Thread by John J. Hughes II | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
4 posts views Thread by Rick | last post: by
4 posts views Thread by =?Utf-8?B?dHBhcmtzNjk=?= | last post: by
7 posts views Thread by oopsbabies | last post: by
16 posts views Thread by =?Utf-8?B?Q2hlZg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.