Network selection of the [javamail online e-mail all the code, including the identification part]

  Have just written a 163 examples of the use of precisely. 

  Import java.util.Properties; 
  Import java.util.Date; 
  Import javax.mail .*; 
  Import javax.mail.internet .*; 
  Import javax.activation .*; 

  (Public class Test7 

  / ** 
  * @ Param args 
  * / 
  Public static void main (String [] args) throws Exception ( 
  Sendhtml (args); 
  ) 

  Public static void sendhtml (String argv []) throws Exception ( 

  String to = "XX@XX.XX" 
  Subject = "null" 
  From = "XXX@163.com" 
  Cc = null, 
  Bcc = null; 
  String mailhost = "smtp.163.com"; 
  String username = "XXX"; 
  String password = "XXX"; 

  Boolean debug = false; 
  Properties props = System.getProperties (); 
  Props.put ( "mail.smtp.auth", "true"); 
  SMTPAuth auth = new SMTPAuth (username and password); 

  If (mailhost! = Null) 
  Props.put ( "mail.smtp.host" mailhost); 

  / / Get a Session object 
  Session session = Session.getDefaultInstance (props, auth); 
  If (debug) 
  Session.setDebug (true); 

  / / Construct the message 
  Message msg = new MimeMessage (session); 
  If (from! = Null) 
  Msg.setFrom (new InternetAddress (from)); 
  Else 
  Msg.setFrom (); 

  Msg.setRecipients ( 
Message.RecipientType.TO,
  InternetAddress.parse (to, false)); 
  If (cc! = Null) 
  Msg.setRecipients ( 
Message.RecipientType.CC,
  InternetAddress.parse (cc, false)); 
  If (bcc! = Null) 
  Msg.setRecipients ( 
Message.RecipientType.BCC,
  InternetAddress.parse (bcc, false)); 

  Subject = new Date (). ToLocaleString (); 
  Msg.setSubject (subject); 

  MimeBodyPart mbp1 = new MimeBodyPart (); 
  String html = 
  "<! DOCTYPE HTML PUBLIC \" - / / W3C / / DTD HTML 4.0 Transitional / / EN \ ">" 
  + "<html>" 
  + "<head> <title> </ Title> </ head>" 
  + "<body>" 
  + "<b> See the following jpg: it is a car! </ B> <br/>" 
  + "<a Href=http://www.a.com/a.jsp> hello </ a> <br/>" 
  + "<IMG Src=cid:7e2a34e1.jpg> <br/>" 
  + "<b> End of jpg </ b>" 
  + "</ Body>" 
  + "</ Html>"; 

  Mbp1.setContent (html, and "text / html"); 

  FileDataSource fds = new FileDataSource ( "c: / 7e2a34e1.jpg"); 
  MimeBodyPart mbp2 = new MimeBodyPart (); 

  Mbp2.setFileName (fds.getName ()); 
  Mbp2.setText ( "This is a beautiful car!"); 
  Mbp2.setDataHandler (new DataHandler (fds)); 
  Mbp2.setHeader ( "Content-ID", "<" + fds.getName () + ">"); 
  Mbp2.setDisposition (null); 

  MimeMultipart mp = new MimeMultipart ( "related"); / / alternative 
  Mp.addBodyPart (mbp1); 
  Mp.addBodyPart (mbp2); 
  Msg.setContent (mp); 

  Msg.setSentDate (new Date ()); 
  Transport.send (msg); 
  System.out.println (mp.getCount ()); 
  System.out.println ( "\ nMail was sent successfully."); 

  ) 

  ) 
  (Class SMTPAuth extends javax.mail.Authenticator 
  Private String user, password; 
  Public SMTPAuth (String u, String p) ( 
  User = u; 
  Password = p; 
  ) 
  Public void getuserinfo (String getuser, String getpassword) ( 
  User = getuser; 
  Password = getpassword; 
  ) 
  Protected javax.mail.PasswordAuthentication getPasswordAuthentication () ( 
  Return new javax.mail.PasswordAuthentication (user, password); 
  ) 
  ) 
  ================================================== =================== 

  See you use 21 CN.com mail sent, I changed my procedures. 
  I have to test you through the process. 
  Password encrypted. 

  Import java.util.Properties; 
  Import java.util.Date; 
  Import javax.mail .*; 
  Import javax.mail.internet .*; 
  Import javax.activation .*; 
  (Public class Test7 

  Public static void main (String [] args) throws Exception ( 
  Sendhtml (args); 
  ) 

  Public static void sendhtml (String argv []) throws Exception ( 

  String to = "zhouxy@teklink.net" 
  Subject = "null" 
  From = "XX@21cn.com" 
  Cc = null, 
  Bcc = null; 
  String mailhost = "smtp.21cn.com"; 
  String username = "XX"; 
  String password = "XX"; 

  Boolean debug = false; 
  Properties props = System.getProperties (); 
  Props.put ( "mail.smtp.auth", "true"); 
  SMTPAuth auth = new SMTPAuth (username and password); 
  If (mailhost! = Null) 
  Props.put ( "mail.smtp.host" mailhost); 

  / / Get a Session object 
  Session session = Session.getDefaultInstance (props, auth); 
  If (debug) 
  Session.setDebug (true); 

  / / Construct the message 
  Message msg = new MimeMessage (session); 
  If (from! = Null) 
  Msg.setFrom (new InternetAddress (from)); 
  Else 
  Msg.setFrom (); 

  Msg.setRecipients ( 
Message.RecipientType.TO,
  InternetAddress.parse (to, false)); 
  If (cc! = Null) 
  Msg.setRecipients ( 
Message.RecipientType.CC,
  InternetAddress.parse (cc, false)); 
  If (bcc! = Null) 
  Msg.setRecipients ( 
Message.RecipientType.BCC,
  InternetAddress.parse (bcc, false)); 

  Subject = new Date (). ToLocaleString (); 
  Msg.setSubject (subject); 

  MimeBodyPart mbp1 = new MimeBodyPart (); 
  String html = 
  "<! DOCTYPE HTML PUBLIC \" - / / W3C / / DTD HTML 4.0 Transitional / / EN \ ">" 
  + "<html>" 
  + "<head> <title> </ Title> </ head>" 
  + "<body>" 
  + "<b> See the following jpg: it is a car! </ B> <br/>" 
  + "<a Href=http://www.a.com/a.jsp> hello </ a> <br/>" 
  + "<IMG Src=cid:7e2a34e1.jpg> <br/>" 
  + "<b> End of jpg </ b>" 
  + "</ Body>" 
  + "</ Html>"; 

  Mbp1.setContent (html, and "text / html"); 

  FileDataSource fds = new FileDataSource ( "c: / 7e2a34e1.jpg"); 
  MimeBodyPart mbp2 = new MimeBodyPart (); 

  Mbp2.setFileName (fds.getName ()); 
  Mbp2.setText ( "This is a beautiful car!"); 
  Mbp2.setDataHandler (new DataHandler (fds)); 
  Mbp2.setHeader ( "Content-ID", "<" + fds.getName () + ">"); 
  Mbp2.setDisposition (null); 

  MimeMultipart mp = new MimeMultipart ( "related"); / / alternative 
  Mp.addBodyPart (mbp1); 
  Mp.addBodyPart (mbp2); 
  Msg.setContent (mp); 

  Msg.setSentDate (new Date ()); 
  Transport.send (msg); 
  System.out.println (mp.getCount ()); 
  System.out.println ( "\ nMail was sent successfully."); 

  ) 

  ) 
  (Class SMTPAuth extends javax.mail.Authenticator 
  Private String user, password; 
  Public SMTPAuth (String u, String p) ( 
  User = u; 
  Password = p; 
  ) 
  Public void getuserinfo (String getuser, String getpassword) ( 
  User = getuser; 
  Password = getpassword; 
  ) 
  Protected javax.mail.PasswordAuthentication getPasswordAuthentication () ( 
  Return new javax.mail.PasswordAuthentication (user, password); 
  ) 

  ) 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply