Some problems and solutions in the process of using JavaMail to send letters

Source: Internet
Author: User

Http://www.blogjava.net/TrampEagle/archive/2006/05/26/48326.html

Today in the process of research javamail letter, there have been some small problems, and now summarized as follows, so as to avoid the latter to take some unnecessary detours, first the complete can run the code sample paste as follows:
Send 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"; Sender uses e-mail server to send email
String from = "Your own e-mail"; Origin of Outgoing mail (sender's mailbox)
String to = "Recipient Mailbox"; e-mail Destination (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"); So that you can pass the validation

Myauthenticator Myauth = new Myauthenticator ("Your own e-mail", "Your own mailbox 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.TO,
New InternetAddress (to));

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

Set the Content
Message.settext ("This is a test program for sending e-mails written in Java!") ");

Message.savechanges ();

Transport.send (message);

}
}

How to verify Sender 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 case is only the way to send e-mail when using 163 mailbox, because the host used is: smtp.163.com, as in source code: String host = "smtp.163.com"; The sender uses the e-mail server to send the email, if you use other e-mail, you must find the appropriate e-mail server on their mail server, for example, Sohu will use smtp.sohu.com, the specific situation, can be obtained from the mail server used. If the host is not used, that is, no props.put ("Mail.smtp.host", host) is set, then javax.mail.MessagingException:Could not connect to SMTP Host:localhost, port:25, exception. Of course, if you are not properly configured, the exception will still be thrown.

Some mail service systems do not need to verify the sender's authorization, so it is easy to use
Session session = Session.getdefaultinstance (props, null);
Without having to use
Props.put ("Mail.smtp.auth", "true");
Myauthenticator Myauth = new Myauthenticator ("Your own e-mail", "Your own mailbox password");
Session session = Session.getdefaultinstance (props, Myauth);

can send e-mail, this is a number of enterprises and institutions of internal e-mail system.
But for many of the portal's e-mail systems, such as: 163,sohu,yahoo, and so on, if it is still simple to use this will throw

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)

Such an exception requires you to have authorization verification, which is designed to prevent others from arbitrarily hair mail, but also to reduce the appearance of junk e-mail. That's when we're going to use
Props.put ("Mail.smtp.auth", "true");
Myauthenticator Myauth = new Myauthenticator ("Your own e-mail", "Your own mailbox password");
Session session = Session.getdefaultinstance (props, Myauth);

Here's a special note: When you use Session.getdefaultinstance, be sure to Props.put ("Mail.smtp.auth", "true"); Set to True, it defaults to False if you did not do this step, although you used session.getdefaultinstance (props, Myauth); and you did myauthenticator Myauth = new Myauthenticator ("Your own e-mail", "Your own mailbox 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)
Such an exception. I was in this step for a long time, later discovered the problem, it is depressed. But fortunately, finally solved.

In fact, the above practice is only a relatively simple one, there are many other ways of writing, such as:
Properties props = System.getproperties (); You can use
Properties Props = new properties (); to replace.

Transport.send (message); You can use the following code instead of
      String username = "Your e-mail user name";
      String Password = "Your e-mail 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 sending multiple e-mail messages at the same time.

There are some specific concepts, you can view the relevant official documents, when I query the information, found an article is written quite carefully, can be consulted: http://www.matrix.org.cn/resource/article/44/ 44101_javamail.html

Also attaches an example of sending e-mail using org.apache.commons.mail:
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");//Set up a mail server using e-mail
try {
Email.addto ("Recipient's 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 was a simple test of commons-email");
Email.send ();
}
catch (Emailexception ex) {
Ex.printstacktrace ();
}
}
}

Some problems and solutions in the process of using JavaMail to send letters

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.