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
- Exception in thread "main" Java.lang.noclassdeffounderror:com/sun/mail/util/lineinputstream
- At Javax.mail.Session.loadProvidersFromStream (Session.java:928)
- At javax.mail.session.access$(Session.java:174)
- At javax.mail.session$1.load (Session.java:870)
- At Javax.mail.Session.loadResource (Session.java:1084)
- At Javax.mail.Session.loadProviders (Session.java:889)
- At Javax.mail.session.<init> (Session.java:)
- At Javax.mail.Session.getInstance (Session.java:249)
- At Com.mail.TextMail.main (Textmail.java:)
I'm just pasting the code down here.
Java code
- Package com.mail;
- Import Java.util.Date;
- Import java.util.Properties;
- Import Javax.mail.Message;
- Import javax.mail.Session;
- Import Javax.mail.Transport;
- Import javax.mail.internet.InternetAddress;
- Import Javax.mail.internet.MimeMessage;
- Import javax.mail.internet.MimeUtility;
- If you use JDK1.4 above the run will appear Exception in thread "main" Java.lang.noclassdeffounderror:com/sun/mail/util/lineinputstream
- I need you to find Javaee.jar and delete the mail packet.
- Public class Textmail {
- public static void Main (string[] args) throws Exception {
- //Configuration file Object
- Properties props = new properties ();
- //Mailbox service address
- Props.put ( "Mail.smtp.host", "smtp.163.com");
- //whether to verify
- Props.put ("Mail.smtp.auth", "true");
- //Create a session
- Session session = Session.getinstance (props);
- //Open debugging, will print the contents of the Mailbox server reply
- Session.setdebug (true);
- Message message = New MimeMessage (session);
- //If the sender does not write to, then the JavaMail 550 Invalid User will appear .
- //If the sender writes and uses an inconsistent account, the 553 Mail from must equal authorized user will appear
- InternetAddress from = new InternetAddress ("[email protected]");
- From.setpersonal (Mimeutility.encodetext ("Java cockroach <[email protected]>"));
- Message.setfrom (from);
- InternetAddress to = new InternetAddress ("[email protected]");
- Message.setrecipient (Message.RecipientType.TO, to);
- Message.setsubject (Mimeutility.encodetext ("strong brother invited, who dares not from!" "));
- Message.settext ("Brother Qiang invited you to visit my blog: http://cuisuqiang.iteye.com/! ");
- Message.setsentdate (new Date ());
- Transport Transport = Session.gettransport ("SMTP");
- //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
- Transport.connect ("smtp.163.com", "test20120711120200", " test123456");
- Transport.sendmessage (Message, message.getallrecipients ());
- Transport.close ();
- System.out.println ("Send Complete");
- }
- }
It is important to note that there
Java code
- InternetAddress from = new InternetAddress ("[email protected]");
And
Java code
- Transport.connect ("smtp.163.com", "test20120711120200", " test123456");
The addresses of both mailboxes must be the same, or they will appear
Java code
- Exception in thread "main" javax.mail.MessagingException: 553 mail from must equal authorized user
- At Com.sun.mail.smtp.SMTPTransport.issueCommand (Smtptransport.java:1020)
- At Com.sun.mail.smtp.SMTPTransport.mailFrom (Smtptransport.java:716)
- At Com.sun.mail.smtp.SMTPTransport.sendMessage (Smtptransport.java:388)
- 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
- DEBUG:setDebug:JavaMail version 1.3. 1
- Debug:getprovider () returning Javax.mail.provider[transport,smtp,com.sun.mail.smtp.smtptransport,sun Microsystems , INC]
- DEBUG Smtp:useehlo True, Useauth true
- DEBUG smtp:trying to connect to host "Smtp.163.com", Port
- 163.com Anti-Spam GT for coremail System (163com[20111010])
- DEBUG smtp:connected to host "Smtp.163.com", Port:
- EHLO Cuisuqiang
- 250-mail
- 250-pipelining
- 250-auth LOGIN PLAIN
- 250-auth=login PLAIN
- 250-coremail 1uxr2xkj7kg0xki17xgru7i0s8fy2u3uj8cz28x1uuuuu7ic2i0y2urgdnesuca0xdruuuuj
- 250-starttls
- 8BITMIME
- DEBUG smtp:found extension "pipelining", arg " "
- DEBUG smtp:found extension "AUTH", arg "LOGIN PLAIN"
- DEBUG smtp:found extension "Auth=login", arg "PLAIN"
- DEBUG smtp:found extension "Coremail", Arg " 1uxr2xkj7kg0xki17xgru7i0s8fy2u3uj8cz28x1uuuuu7ic2i0y2urgdnesuca0xdruuuuj "
- DEBUG smtp:found extension "STARTTLS", arg " "
- DEBUG smtp:found extension "8BITMIME", arg " "
- DEBUG Smtp:attempt to authenticate
- AUTH LOGIN
- 334 Dxnlcm5hbwu6
- Dgvzddiwmtiwnzexmtiwmjaw
- 334 Ugfzc3dvcmq6
- dgvzddeymzq1ng==
- 235 Authentication Successful
- DEBUG Smtp:use8bit false
- MAIL from:<test20120711120200@163.com>
- + Mail OK
- RCPT to:<455213313@qq .com>
- + Mail OK
- DEBUG smtp:verified Addresses
- DEBUG SMTP: 455213313@qq. com
- DATA
- 354 END data with <CR><LF>.<CR><LF>
- Message-id: <14518194.1341995865564.javamail.administrator@cuisuqiang >
- date:wed, Jul: Notoginseng:+0800 (CST)
- From: "=? GBK? Q?java=d0=a1=c7=bf<[email protected]>?= "<test20120711120200@163.com>
- To: 455213313@qq. com
- Subject: =? GBK? b?x7+459h7x+ujrmutunkyu7tto6e=?=
- Mime-version: 1.0
- Content-type:text/plain; Charset=gbk
- Content-transfer-encoding:quoted-printable
- =c7=bf=b8=e7=d1=fb=c7=eb=c4=e3=b7=c3=ce=ca=ce=d2=b5=c4=b2=a9=bf=cd=a3=bahtt=
- P://cuisuqiang.iteye.com/=a3=a1
- .
- + Mail OK queued as SMTP2,DNGOWEB52UHCO_1PZMMMCA--. 937s2 1341995868
- 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