473,883 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

send mail using smtp to hotmail

1 New Member
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 6353

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

Similar topics

6
5078
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 MailMessage(); mailMsg.From = from; from = from + from;
4
3806
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
3269
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 my customers are not really happy with having the IIS installed, not being used except to send e-mail this is becoming a problem. I have also tried using a little program from code project for sending e-mail which works great on older servers...
3
1840
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 even though the other addresses are. I know the spelling is correct because I have tried sending messages from my other accounts to this address and have recieved them successfully. I also do not know of any junkmail filter that would delete the...
4
1607
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"; mail.Subject="descr C#";
1
3216
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 System.Net.Mail. Now all of them works great with any email account but hotmail. So here is the C# code: MailAddress from = new MailAddress("ZZZ@CCC.com", "XXX"); MailAddress to = new MailAddress("SSS@hotmail.com", "YYY");
4
7691
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. But sometimes even when I'm just sending one e-mail the web page takes a minutes or so to post. Can anyone tell me why it is so slow, or how to fix the problem? To address this I am thinking of just creating a queue table to write the emails...
7
7758
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 free edition for anti-virus. I use an ISP provider from my country and according to them I do not need to perform authentication while sending mails through their SMTP address. Thus I am using external SMTP server. I don't have IIS installed...
16
4765
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 environment: VS2005 + XP.-- I have disabled firewall hardware enviornmnet:telcom's modem connects hub,hub connects two computers.。-- I also tried to connect computer to modem directly,but I can
0
9948
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7980
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6008
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.