Java Foundation-javamail

Source: Internet
Author: User

Emailauthenticator class

Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;

public class Emailauthenticator extends Authenticator
{
Private String username;
Private String userpsd;
Public Emailauthenticator (string username, string userpsd)
{
This.username = Username;
THIS.USERPSD = USERPSD;
}
Public String GetUserName ()
{
return username;
}
public void Setusername (String username)
{
This.username = Username;
}
Public String GETUSERPSD ()
{
return USERPSD;
}
public void setuserpsd (String userpsd)
{
THIS.USERPSD = USERPSD;
}
@Override
Protected Passwordauthentication getpasswordauthentication ()
{
return Super.getpasswordauthentication ();
}
}

Main Method Test

Import James. Emailauthenticator;

Import java.util.Properties;
Import javax.mail.*;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;

public class Test {
public static void Main (string[] args) throws Exception {
This is the mail server address
String SMTP = "smtp.163.com";
User e-mail address, with their own mailbox Ah
String from = "[email protected]";
Mailbox offset mailbox to send
String to = "[email protected]";
Constructs the user authentication authentication object, the parameter is the user name and the password
Emailauthenticator auth = new Emailauthenticator ("Test", "password");
Get System Properties Object
Properties props = System.getproperties ();
Set up the mail server and whether authentication
Props.put ("Mail.smtp.host", SMTP);
/* If written as such props.put ("Mail.smtp.auth", true);
* Can explode com.sun.mail.smtp.SMTPSendFailedException: abnormal
* should be written as Props.put ("Mail.smtp.auth", "true");
* */
Props.put ("Mail.smtp.auth", "true");
Get Session Object
Session session = Session.getinstance (props, (Authenticator) auth);
Establish MimeMessage object with session as parameter
Here MIME is the class that supports this protocol and is used to describe the contents of the message
MimeMessage msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress (from));
Msg.addrecipient (Message.recipienttype.to,new internetaddress (to));
Msg.setsubject ("title");
String s = "content";
Msg.settext (s);
After setting the MimeMessage, the transport object is established from the session.
Transport objects are used to send message information, gettransport parameters
Indicates the protocol used to send the message.
Transport Transport = Session.gettransport ("SMTP");
Transport.connect (SMTP, "[email protected]", "password");
Transport.sendmessage (Msg,msg.getallrecipients ());
Transport.close ();
System.out.println ("Mail sent successfully");
}
}

If it's myeclipse, you need a mail.jar bag.

When You send a message using JavaMail, if you receive a " java.lang.noclassdeffounderror:com/sun/mail/util/ Lineinputstream"Such a mistake,
javax.mail and javax.activation These two packages are already in the javaEE5 of the Basic package, is The MyEclipse has been brought in by itself. But the method inside Errors due to inconsistencies with the latest JDK's JavaMail package
Workaround:
Enter the MyEclipse installation directory (this is the MyEclipse installation directory on my machine, maybe you are different)
C:\Users\Administrator\MyEclipse 6.0\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.0.1. Zmyeclipse601200710\data\libraryset\ee_5
This directory, you can see the Javaee.jar package, double-click the use of compression software to open the package file, and then into the Javax folder, delete the mail directory and activation directory, may also be a jar package.
After the deletion, in the running code, there will be no error, of course, not the entire Javaee.jar package to delete, he also has a lot of other functions, such as servlet, if there are references to other functions, or will error.
Finally add the latest Mail.jar and Activation.jar are ok.

Java Foundation-javamail

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.