Send mail
1. Write a property required for the Mailsendproper class package
Import java.util.Properties;
public class Mailsendproper {
Private String Host; IP of the sending mail server
Private String Port; Port number of the outgoing mail server
Private String sendaddress; E-mail Sender's Address
Private String receiveaddress; Address of the recipient of the message
Private String username; User name to log on to the sending mailbox
Private String password; Log in to send the password for the mailbox
Private Boolean isvalidate = true; Whether authentication is required
Private String subject; Message header
Private String content; Message 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 a emailmessage package to send 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 mailbox password authenticator 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 class method to get the password authenticator
@Override
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (username,password);
}
}
4, Write a MailSend mail 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 if authentication is required
attorney = new Mailattorney (mailsender.getusername (), Mailsender.getpassword ());
}
Constructs a seesion for sending mailboxes based on mailbox session properties and Password verifier
Session session = Session.getinstance (mailsender.getproperties (), attorney);
Create a mail message based on session
Message mailmessage = new MimeMessage (session);
try {
Create the address of the sender of the message
Address from = new InternetAddress (mailsender.getsendaddress ());
Set up senders for mailbox messages
Mailmessage.setfrom (from);
Create a message to receive this address and set it to the mail message
Address to = new InternetAddress (mailsender.getreceiveaddress ());
Mailmessage.setrecipient (Message.RecipientType.TO, to);
Mailmessage.setsubject (mailsender.getsubject ());//set The message header
Mailmessage.setsentdate (new Date ());//set Send Message time
Mailmessage.settext (mailsender.getcontent ()); Set up message 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 transfer protocol, The default port number is 25,
Mail.setport ("25");
Mail.setisvalidate (true); yes, I need Authentication.
Mail.setusername (""); Set the login user name
Mail.setpassword (""); Set the Sender's password
Mail.setsendaddress (""); Set the address of the sender to send this and the login user name is the same
Mail.setreceiveaddress (message.gettoemail ()); Set the Recipient's address
Mail.setsubject (message.gettitle ()); Set the mailbox header
Mail.setcontent (message.getcontext ()); Set the contents of a mailbox
return MailTest (mail);
}
}
Note: The mailbox used must have the POP3/SMTP service turned on to send a successful
Different mailbox, the transport protocol of the mailbox is not the same as
QQ Mailbox: SMTP transport protocol is smtp.qq.com port 25
POP3 transport protocol is pop3.qq.com Port 110
5. Test Using method
public static void main (string[] Args) {
Emailmessage message = new Emailmessage ();
String code = string.valueof (math.random ()). substring (3, 9); Generate Verification Code
Message.settitle ("email verification"); Message header
Message.setcontext ("dear User hello, Your verification code is" +code+ ". "); Message content
Message.settoemail ("[email protected]"); Who to send to
System.out.println (mailsend.sendemail (message)? " Sent successfully ":" failed to send ");
}
To reprint, please indicate here
JAVA Mail Sending