Go JavaMail Mail-Send a text message and some problem description

Source: Internet
Author: User
Tags starttls

The jar package that needs to be downloaded:

Javamail:http://www.oracle.com/technetwork/java/javamail/index.html

Jaf:http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html

The packages included are: Mail.jar Activation.jar

You have to prepare a mailbox account, just use NetEase's Bar, and then open the SMTP function

Some of the port information about NetEase refer to the following:

Each company is not the same, the use of SSL and not the same is not practical

Using the development and compilation environment is JDK1.4, if you are using the JDK1.5 environment, then your environment needs to modify some things, find Javaee.jar delete inside the mial package, otherwise there will be conflict

Otherwise the error will be:

Java code
  1. Exception in thread "main" Java.lang.noclassdeffounderror:com/sun/mail/util/lineinputstream
  2. At Javax.mail.Session.loadProvidersFromStream (Session.java:928)
  3. At javax.mail.session.access$(Session.java:174)
  4. At javax.mail.session$1.load (Session.java:870)
  5. At Javax.mail.Session.loadResource (Session.java:1084)
  6. At Javax.mail.Session.loadProviders (Session.java:889)
  7. At Javax.mail.session.<init> (Session.java:)
  8. At Javax.mail.Session.getInstance (Session.java:249)
  9. At Com.mail.TextMail.main (Textmail.java:)

I'm just pasting the code down here.

Java code
  1. Package com.mail;
  2. Import Java.util.Date;
  3. Import java.util.Properties;
  4. Import Javax.mail.Message;
  5. Import javax.mail.Session;
  6. Import Javax.mail.Transport;
  7. Import javax.mail.internet.InternetAddress;
  8. Import Javax.mail.internet.MimeMessage;
  9. Import javax.mail.internet.MimeUtility;
  10. If you use JDK1.4 above the run will appear Exception in thread "main" Java.lang.noclassdeffounderror:com/sun/mail/util/lineinputstream
  11. I need you to find Javaee.jar and delete the mail packet.
  12. Public class Textmail {
  13. public static void Main (string[] args) throws Exception {
  14. //Configuration file Object
  15. Properties props = new properties ();
  16. //Mailbox service address
  17. Props.put ( "Mail.smtp.host", "smtp.163.com");
  18. //whether to verify
  19. Props.put ("Mail.smtp.auth", "true");
  20. //Create a session
  21. Session session = Session.getinstance (props);
  22. //Open debugging, will print the contents of the Mailbox server reply
  23. Session.setdebug (true);
  24. Message message = New MimeMessage (session);
  25. //If the sender does not write to, then the JavaMail 550 Invalid User will appear .
  26. //If the sender writes and uses an inconsistent account, the 553 Mail from must equal authorized user will appear
  27. InternetAddress from = new InternetAddress ("[email protected]");
  28. From.setpersonal (Mimeutility.encodetext ("Java cockroach <[email protected]>"));
  29. Message.setfrom (from);
  30. InternetAddress to = new InternetAddress ("[email protected]");
  31. Message.setrecipient (Message.RecipientType.TO, to);
  32. Message.setsubject (Mimeutility.encodetext ("strong brother invited, who dares not from!"  "));
  33. Message.settext ("Brother Qiang invited you to visit my blog: http://cuisuqiang.iteye.com/!  ");
  34. Message.setsentdate (new Date ());
  35. Transport Transport = Session.gettransport ("SMTP");
  36. //Specific you use the SMTP address and port of the mailbox, should be in the mailbox to view, if using SSL, NetEase port should be 465/994
  37. Transport.connect ("smtp.163.com", "test20120711120200", " test123456");
  38. Transport.sendmessage (Message, message.getallrecipients ());
  39. Transport.close ();
  40. System.out.println ("Send Complete");
  41. }
  42. }

It is important to note that there

Java code
    1. InternetAddress from = new InternetAddress ("[email protected]");

And

Java code
    1. Transport.connect ("smtp.163.com", "test20120711120200", " test123456");

The addresses of both mailboxes must be the same, or they will appear

Java code
    1. Exception in thread "main" javax.mail.MessagingException: 553 mail from must equal authorized user
    2. At Com.sun.mail.smtp.SMTPTransport.issueCommand (Smtptransport.java:1020)
    3. At Com.sun.mail.smtp.SMTPTransport.mailFrom (Smtptransport.java:716)
    4. At Com.sun.mail.smtp.SMTPTransport.sendMessage (Smtptransport.java:388)
    5. At Com.mail.TextMail.main (Textmail.java: $)

You check the Internet is to say that user authentication is not correct, in fact, everyone is in accordance with someone else's code copy, and then modify the user and password, how can not be wrong!

I have this account and password can be used, I am sorry netease!

Since I set up print debugging above, you will see the following in the console:

Java code
  1. DEBUG:setDebug:JavaMail version 1.3. 1
  2. Debug:getprovider () returning Javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,sun Microsystems , INC]
  3. DEBUG Smtp:useehlo True, Useauth true
  4. DEBUG smtp:trying to connect to host "Smtp.163.com", Port
  5. 163.com Anti-Spam GT for coremail System (163com[20111010])
  6. DEBUG smtp:connected to host "Smtp.163.com", Port:
  7. EHLO Cuisuqiang
  8. 250-mail
  9. 250-pipelining
  10. 250-auth LOGIN PLAIN
  11. 250-auth=login PLAIN
  12. 250-coremail 1uxr2xkj7kg0xki17xgru7i0s8fy2u3uj8cz28x1uuuuu7ic2i0y2urgdnesuca0xdruuuuj
  13. 250-starttls
  14. 8BITMIME
  15. DEBUG smtp:found extension "pipelining", arg " "
  16. DEBUG smtp:found extension "AUTH", arg "LOGIN PLAIN"
  17. DEBUG smtp:found extension "Auth=login", arg "PLAIN"
  18. DEBUG smtp:found extension "Coremail", Arg " 1uxr2xkj7kg0xki17xgru7i0s8fy2u3uj8cz28x1uuuuu7ic2i0y2urgdnesuca0xdruuuuj "
  19. DEBUG smtp:found extension "STARTTLS", arg " "
  20. DEBUG smtp:found extension "8BITMIME", arg " "
  21. DEBUG Smtp:attempt to authenticate
  22. AUTH LOGIN
  23. 334 Dxnlcm5hbwu6
  24. Dgvzddiwmtiwnzexmtiwmjaw
  25. 334 Ugfzc3dvcmq6
  26. dgvzddeymzq1ng==
  27. 235 Authentication Successful
  28. DEBUG Smtp:use8bit false
  29. MAIL from:<test20120711120200@163.com>
  30. + Mail OK
  31. RCPT to:<455213313@qq .com>
  32. + Mail OK
  33. DEBUG smtp:verified Addresses
  34. DEBUG SMTP: 455213313@qq. com
  35. DATA
  36. 354 END data with <CR><LF>.<CR><LF>
  37. Message-id: <14518194.1341995865564.javamail.administrator@cuisuqiang >
  38. date:wed, Jul: Notoginseng:+0800 (CST)
  39. From: "=? GBK? Q?java=d0=a1=c7=bf<[email protected]>?= "<test20120711120200@163.com>
  40. To: 455213313@qq. com
  41. Subject: =? GBK? b?x7+459h7x+ujrmutunkyu7tto6e=?=
  42. Mime-version: 1.0
  43. Content-type:text/plain; Charset=gbk
  44. Content-transfer-encoding:quoted-printable
  45. =c7=bf=b8=e7=d1=fb=c7=eb=c4=e3=b7=c3=ce=ca=ce=d2=b5=c4=b2=a9=bf=cd=a3=bahtt=
  46. P://cuisuqiang.iteye.com/=a3=a1
  47. .
  48. + Mail OK queued as SMTP2,DNGOWEB52UHCO_1PZMMMCA--. 937s2 1341995868
  49. QUIT

These are some of the interactive information between the program and the Mail service provider!

Please go to Iteye to see my original:http://cuisuqiang.iteye.com

or support my personal blog, address:http://www.javacui.com

Go JavaMail Mail-Send a text message and some problem description

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.