JAVA mail, java mail

Source: Internet
Author: User
Tags mailmessage

JAVA mail, java mail

Email sending

 

1. Write an attribute required for MailSendProper class Encapsulation

Import java. util. Properties;

Public class MailSendProper {
Private String Host; // the ip address of the email sending server
Private String Port; // The Port number of the email sending server.
Private String SendAddress; // the address of the sender
Private String ReceiveAddress; // email recipient's address
Private String username; // username used to log on to the sender's mailbox
Private String password; // The password used to log on to the sender's mailbox.
Private boolean isvalidate = true; // whether authentication is required
Private String subject; // mail title
Private String content; // email content

Public String getSubject (){
Return subject;
}
Public void setSubject (String subject ){
This. subject = subject;
}
Public String getContent (){
Return content;
}
Public void setContent (String content ){
This. content = content;
}
Public String getHost (){
Return Host;
}
Public void setHost (String host ){
Host = host;
}
Public String getPort (){
Return Port;
}
Public void setPort (String port ){
Port = port;
}
Public String getSendAddress (){
Return SendAddress;
}
Public void setSendAddress (String sendAddress ){
SendAddress = sendAddress;
}
Public String getReceiveAddress (){
Return ReceiveAddress;
}
Public void setReceiveAddress (String receiveAddress ){
ReceiveAddress = receiveAddress;
}
Public String getUsername (){
Return username;
}
Public void setUsername (String username ){
This. username = username;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
Public boolean isIsvalidate (){
Return isvalidate;
}
Public void setIsvalidate (boolean isvalidate ){
This. isvalidate = isvalidate;
}
Public Properties getProperties (){
Properties properties = new Properties ();
Properties. put ("mail. smtp. host", this. Host );
Properties. put ("mail. smtp. port", this. Port );
Properties. put ("mail. smtp. auth", isvalidate? "True": "false ");
Return properties;
}
}

 

 

2. Write an EmailMessage to encapsulate the sending information

Public class EmailMessage {
Private String title;
Private String context;
Private String toEmail;

Public EmailMessage (){
Super ();
}
Public EmailMessage (String title, String context, String toEmail ){
Super ();
This. title = title;
This. context = context;
This. toEmail = toEmail;
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
Public String getContext (){
Return context;
}
Public void setContext (String context ){
This. context = context;
}
Public String getToEmail (){
Return toEmail;
}
Public void setToEmail (String toEmail ){
This. toEmail = toEmail;
}
}

 

3. Write a MailAttorney email password validator class

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

Public class MailAttorney extends Authenticator {
Private String username;
Private String password;

Public MailAttorney (String username, String password ){
This. username = username;
This. password = password;
}
// Override the parent method to obtain the password authenticator
@ Override
Protected PasswordAuthentication getPasswordAuthentication (){
Return new PasswordAuthentication (username, password );
}
}

 

4. Write a MailSend email tool class

Import java. util. Date;

Import javax. mail. Address;
Import javax. mail. Message;
Import javax. mail. Session;
Import javax. mail. Transport;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeMessage;

Import com. VTBBS. entity. EmailMessage;

Public class MailSend {
Public static boolean mailTest (MailSendProper mailsender ){
MailAttorney attorney = null;
If (mailsender. isIsvalidate () {// determine whether identity authentication is required
Attorney = new MailAttorney (mailsender. getUsername (), mailsender. getPassword ());
}
// Construct a mail sending seesion Based on the mailbox session attribute and password validators
Session session = Session. getInstance (mailsender. getProperties (), attorney );
// Create an email message based on the session
Message mailMessage = new MimeMessage (session );
Try {
// Create the sender's address
Address from = new InternetAddress (mailsender. getSendAddress ());
// Set the sender of the email message
MailMessage. setFrom (from );
// Create an email Receiving address and set it to the email message.
Address to = new InternetAddress (mailsender. getReceiveAddress ());
MailMessage. setRecipient (Message. RecipientType. TO, );
MailMessage. setSubject (mailsender. getSubject (); // you can specify the mail title.
MailMessage. setSentDate (new Date (); // you can specify the email sending time.
MailMessage. setText (mailsender. getContent (); // you can specify the email content.
Transport. send (mailMessage );
Return true;
} Catch (Exception e ){
E. printStackTrace ();
Return false;
}
}

Public static boolean sendEmail (EmailMessage message ){
MailSendProper mail = new MailSendProper ();
Mail. setHost ("smtp.126.com"); // smtp Simple mail transmission protocol. The default port number is 25,
Mail. setPort ("25 ");
Mail. setIsvalidate (true); // authentication required
Mail. setUsername (""); // set the logon Username
Mail. setPassword (""); // set the sender's password
Mail. setSendAddress (""); // you can specify the sender address and logon user name for sending this message.
Mail. setReceiveAddress (message. getToEmail (); // you can specify the recipient's address.
Mail. setSubject (message. getTitle (); // you can specify the email subject.
Mail. setContent (message. getContext (); // you can specify the email content.
Return mailTest (mail );
}

}

Note: The POP3/SMTP service must be enabled for the mailbox to be sent successfully,

Different email addresses have different transmission protocols, as shown in figure

Qq mail: SMTP transmission protocol is port 25 of smtp.qq.com

POP3 transmission protocol is pop3.qq.com port 110

5. Test the usage

Public static void main (String [] args ){
EmailMessage message = new EmailMessage ();
String code = String. valueOf (Math. random (). substring (3, 9); // generate the verification code
Message. setTitle ("email Verification"); // mail title
Message. setContext ("Dear user, your verification code is" + code + ". "); // Email content
Message. setToEmail ("940202884@qq.com"); // to whom to send
System. out. println (MailSend. sendEmail (message )? "Sent successfully": "failed to send ");
}

If you want to reprint it, please mark it here

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.