Some Problems and Solutions in the process of sending emails using javamail

Source: Internet
Author: User

Today, I encountered some minor problems in studying javamail's mail sending process, which is summarized as follows to avoid unnecessary detours, first, paste the complete code example that can run normally as follows:
Email source code:
Package com. hyq. test;

Import java. util. properties;
Import javax. Mail .*;
Import javax. Mail. Internet .*;

Public class mailexample {

Public static void main (string ARGs []) throws exception {

String host = "smtp.163.com"; // The sender uses the email address server.
String from = "your own email address"; // the sender's email address)
String to = "recipient's mailbox"; // destination of the email (recipient's mailbox)

// Get System Properties
Properties props = system. getproperties ();

// Setup Mail Server
Props. Put ("mail. SMTP. Host", host );

// Get session
Props. Put ("mail. SMTP. Auth", "true "); //

Myauthenticator myauth = new myauthenticator ("your inbox", "your inbox password ");
Session session = session. getdefaultinstance (props, myauth );

// Session. setdebug (true );

// Define message
Mimemessage message = new mimemessage (session );

// Set the FROM address
Message. setfrom (New internetaddress (from ));

// Set the to address
Message. addrecipient (message. recipienttype.,
New internetaddress ());

// Set the subject
Message. setsubject ("test program! ");

// Set the content
Message. settext ("this is a test program for sending emails written in Java! ");

Message. savechanges ();

Transport. Send (Message );

}
}

How to verify the sender's Permissions
Package com. hyq. test;

Import javax. Mail. passwordauthentication;

Class myauthenticator
Extends javax. Mail. authenticator {
Private string struser;
Private string strpwd;
Public myauthenticator (string user, string password ){
This. struser = user;
This. strpwd = password;
}

Protected passwordauthentication getpasswordauthentication (){
Return new passwordauthentication (struser, strpwd );
}
}

Note: The above example only shows how to send an email when using the 163 mailbox, because the host used is smtp.163.com, for example, in the source code: String host = "smtp.163.com "; // The sender uses the email address server for sending mails. If other emails are used for sending, the sender must find the corresponding email address on the email server. For example, Sohu must use smtp.sohu.com, the specific circumstances can be obtained from the email server. If the host is not used, that is, props is not performed. put ("mail. SMTP. host ", host); set, then javax will be thrown. mail. messagingexception: cocould not connect to SMTP host: localhost, Port: 25; Exception. Of course, if you do not configure it correctly, this exception will still be thrown.

Some email service systems do not need to authenticate the sender's authorization, so it is easy to use
Session session = session. getdefaultinstance (props, null );
Instead of using
Props. Put ("mail. SMTP. Auth", "true ");
Myauthenticator myauth = new myauthenticator ("your inbox", "your inbox password ");
Session session = session. getdefaultinstance (props, myauth );

You can then send an email. This is mostly the internal email system of some enterprises and institutions.
However, for email systems on many portal websites, such as 163, Sohu, and Yahoo

Com. Sun. Mail. SMTP. smtpsendfailedexception: 553 authentication is required, smtp8, wkjadxuaycafmnze8bwtia =. 32705s2

At com. Sun. Mail. SMTP. smtptransport. issuesendcommand (smtptransport. Java: 1388)

At com. Sun. Mail. SMTP. smtptransport. mailfrom (smtptransport. Java: 959)

At com. Sun. Mail. SMTP. smtptransport. sendmessage (smtptransport. Java: 583)

At javax. Mail. Transport. send0 (transport. Java: 169)

At javax. Mail. Transport. Send (transport. Java: 98)

This exception requires you to perform authorization verification. Its purpose is to prevent other people from sending emails randomly. This is also to reduce the appearance of spam. At this time, we will use
Props. Put ("mail. SMTP. Auth", "true ");
Myauthenticator myauth = new myauthenticator ("your inbox", "your inbox password ");
Session session = session. getdefaultinstance (props, myauth );

Here is another thing worth special attention: When you use session. when getdefaultinstance, you must set props. put ("mail. SMTP. auth "," true "); if it is set to true, the default value is false. If you do not do this step, although you use session. getdefaultinstance (props, myauth);, you do know myauthenticator myauth = new myauthenticator ("your inbox", "your inbox password"); but it will still throw
Com. Sun. Mail. SMTP. smtpsendfailedexception: 553 authentication is required, smtp8, wkjadxja2sbrm3zefv0gia =. 40815s2

At com. Sun. Mail. SMTP. smtptransport. issuesendcommand (smtptransport. Java: 1388)

At com. Sun. Mail. SMTP. smtptransport. mailfrom (smtptransport. Java: 959)

At com. Sun. Mail. SMTP. smtptransport. sendmessage (smtptransport. Java: 583)

At javax. Mail. Transport. send0 (transport. Java: 169)

At javax. Mail. Transport. Send (transport. Java: 98)
This exception. I spent a long time in this step. Later I found this problem, and it was very depressing. But fortunately, it was finally solved.

In fact, the above method is only a relatively simple one, and there are many other ways to write, such:
Properties props = system. getproperties (); can be used
Properties props = new properties.

Transport. Send (Message); you can use the following code to replace
String username = "your email address username ";
String Password = "your email password ";
Message. savechanges (); // implicit with send ()
Transport transport = session. gettransport ("SMTP ");
Transport. Connect ("mail.htf.com.cn", username, password );
Transport. sendmessage (message, message. getallrecipients ());
Transport. Close ();
This method is useful when multiple emails are sent at the same time.

There are some specific concepts, you can view the relevant official documentation, When I query information, found an article written quite carefully, can be referred to: http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html

The following is an example of using org. Apache. commons. Mail to send an email:
Import org. Apache. commons. Mail. simpleemail;
Import org. Apache. commons. Mail .*;

Public class testcommon {
Public testcommon (){
}
Public static void main (string [] ARGs ){
Simpleemail email = new simpleemail ();
Email. sethostname ("smtp.163.com"); // you can specify the email sending server.
Try {
Email. addto ("recipient mailbox ");
Email. setauthentication ("sender's mailbox", "sender's mailbox password ");
Email. setfrom ("sender's mailbox ");
Email. setsubject ("test Apache. commons. Mail Message ");
Email. setmsg ("this is a simple test of commons-email ");
Email. Send ();
}
Catch (emailexception ex ){
Ex. printstacktrace ();
}
}
}

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.