473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 6244

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

Similar topics

6
by: erdem kemer | last post by:
i cant send mail to yahoo mail or hotmail while i can send my other mail accounts (pop3) is it becouse yahoo and hotmail is web-based mail here is the code MailMessage mailMsg = new...
4
by: PawelR | last post by:
Hi, everbody. How send e-mail if smtp need authorization. If authorization is not required send email is not problem but I use SmtpMail.Send(myMail) PawelR
6
by: John J. Hughes II | last post by:
I have a service that needs to send e-mail alerts. I have been attempting to use the System.Net.Mail function from .NET but this seems to require the IIS be installed and running. Since some of...
3
by: Nathan Sokalski | last post by:
I have an ASP.NET page that sends a Mail.MailMessage to several email addresses (all mine). However, one of the addresses, the one ending in @verizon.net, does not seem to be recieving the message...
4
by: Rick | last post by:
Hi guys one question, i have this code, it works for a smtp that doesn't ask for autentification using System.Web.Mail; MailMessage mail = new MailMessage(); mail.To="email@hotmail.com"; ...
1
by: oliu321 | last post by:
Hi, I am trying to write some codes to send emails through a SMTP server. I wrote a C++ version using pure socket programming and SMTP protocol, a VB version using CDO and a C# version using...
4
by: =?Utf-8?B?dHBhcmtzNjk=?= | last post by:
I have a web page that at the click of a button must send a bunch (1000+) emails. Each email is sent individually. I have the code working fine, using Mail Message classes and smtp and all that. ...
7
by: oopsbabies | last post by:
Hello everyone, I am using Apache 1.3.33 as the web server and PHP version 4.3.10. My machine is using Windows XP 2002 professional edition which comes with a Windows firewall. I am using McAfee...
16
by: =?Utf-8?B?Q2hlZg==?= | last post by:
I can use outlook2003 to send email,but I cann't use this code below to send email. Please help me to test this code and instruct me how to solve this problem in detail. software...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.