Javax.mail Call Enterprise Mailbox Send mail, call personal mailbox Send mail

Source: Internet
Author: User

Package com;


Import java.util.Properties;


Import Javax.mail.Authenticator;
Import Javax.mail.BodyPart;
Import Javax.mail.Message;
Import Javax.mail.Multipart;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;


Import Com.abcchina.PopupAuthenticator;


public class Send {
public static void Main (string[] args) throws Exception {
Properties Props = new properties ();


String host = "smtp.ym.163.com"; 163 SMTP server for free enterprise mailboxes
String from = "[email protected]"; My business e-mail address
String to = "[email protected]"; e-mail address to be sent to
String username = "Web";
String Password = "******"; //
Props.put ("Mail.smtp.auth", "true");
Props.put ("Mail.smtp.host", host);
String content = "Hello";
String subject= "This is a good news 1";


The authentication is the use of the mailbox full spelling password instead of the user password this should pay attention to
Authenticator Smtpauth = new Popupauthenticator (
From, password);

Session session = Session.getdefaultinstance (props, Smtpauth);
Session.setdebug (TRUE);
MimeMessage message = new MimeMessage (session);
Message.setfrom (New InternetAddress (from));
Message.addrecipient (Message.RecipientType.TO, New internetaddress (to));
BodyPart MDP = new MimeBodyPart ();
Mdp.setcontent (Content, "text/html;charset=gb2312");
Multipart mm = new Mimemultipart ();
Mm.addbodypart (MDP);
Message.setcontent (mm);
Message.setsubject (SubJect);
Message.savechanges ();
Transport Transport = Session.gettransport ("SMTP");
Transport.connect (host, username, password);//link with the user name and password instead of the full spelling password of the mailbox to note
Transport.sendmessage (Message, message.getallrecipients ());
Transport.close ();
}

}


=======================================================

Package Com.abcchina;


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


public class Popupauthenticator extends Authenticator {
String Username=null;
String Password=null;


Public Popupauthenticator () {}

Public popupauthenticator (String username,string password){
This.username=username;
This.password=password;
}


Public passwordauthentication Performcheck (String user,string pass) {
Username = user;
Password = pass;
return Getpasswordauthentication ();
}


Protected Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (username, password);
}


}



What do not understand the direct comment can!!!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Javax.mail Call Enterprise Mailbox Send mail, call personal mailbox Send mail

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.