Java implementation of mail delivery

Source: Internet
Author: User

Java Implementation of mail delivery
/**
*
*/
Package com.email;

/**
* <p>description: </p>
* @author Chu Kaixin
* @date June 6, 2018
*
*/
Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;

/**
* Server mailbox
*/
public class Mailauthenticator extends authenticator{

/**
* User Name
*/
Private String username;
/**
* Login Password
*/
private String password;
/**
* Inheriting the authenticator class requires overriding this method
*/
@Override
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (Username,password);
}

Public Mailauthenticator () {

}
Public Mailauthenticator (string Username, string password) {
This.username = Username;
This.password = password;
}
Public String GetUserName () {
return username;
}
Public String GetPassword () {
return password;
}
}
/**
*
*/
Package com.email;

/**
* <p>description: </p>
* @author Chu Kaixin
* @date June 6, 2018
*
*/
/**
* Mail Object
*/
public class Mail {

/**
* Mail Title
*/
Private String subject;
/**
* Email Content
*/
Private String content;

Public Mail () {

}

Public Mail (string subject, string content) {
This.subject = subject;
this.content = 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;
}

}

/**
*
*/
Package com.email;

Import java.security.GeneralSecurityException;
/**
* <p>description: </p>
* @author Chu Kaixin
* @date June 6, 2018
*
*/
Import java.util.List;
Import java.util.Properties;

Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMessage.RecipientType;

Import Com.sun.mail.util.MailSSLSocketFactory;



/**
* Mail Sender: Can be single, can be mass
*/
public class MailSender {
/**
* Properties file for sending mail
*/
Private final transient Properties props = system.getproperties ();
/**
* Server Mailbox Login Verification
*/
private transient mailauthenticator mailauthenticator;
/**
* Email Session
*/
private transient session session;
/**
* Initialize the mailbox sender
*
* @param mailserviceurl
* Server Email address
* @param username
* Server mailbox User name
* @param password
* Server Mailbox Login Password
*/
Public MailSender (final string mailserviceurl,final string username, final string password) {
Init (mailserviceurl, username, password);
}
/**
* Initialize the mailbox sender
*
* @param username
* Server mailbox User name
* @param password
* Server Mailbox Login Password
*/
Public MailSender (final string username, final string password) {
Resolves an SMTP server through a mailbox address, which works for most mailboxes
Final String Mailserviceurl = "smtp." + Username.split ("@") [1];
Init (mailserviceurl, username, password);
}
/**
* Initialization operation
*
* @param mailserviceurl
* Server Email address
* @param username
* Server mailbox User name
* @param password
* Server Mailbox Login Password
*/
public void init (string mailserviceurl, string Username, string password) {
Initialize Props
Props.put ("Mail.smtp.auth", "true");
Props.put ("Mail.smtp.host", Mailserviceurl);
Mailsslsocketfactory SF;
try {
SF = new Mailsslsocketfactory ();
Sf.settrustallhosts (TRUE);
Props.put ("Mail.smtp.ssl.enable", "true");
Props.put ("Mail.smtp.ssl.socketFactory", SF);
} catch (Generalsecurityexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


Server Mailbox Validation
Mailauthenticator = new Mailauthenticator (username, password);
Create a session and want to log in as a mailbox
Session = Session.getinstance (props, mailauthenticator);
}
/**
* Send mail
*
* @param recipient
* Email address of the person receiving the letter
* @param subject
* Mail Title
* @param content
* Email Content
* @throws addressexception
* @throws messagingexception
*/
public void Send (string recipient, string subject, string content) throws Addressexception, messagingexception{
Creating MIME-type messages
Final MimeMessage msg = new MimeMessage (session);
Set Sender
Msg.setfrom (New InternetAddress (Mailauthenticator.getusername ()));
Set up the recipient.
Msg.setrecipient (recipienttype.to, New InternetAddress (recipient));
Set the message header
Msg.setsubject (subject);
Set up message content
Msg.setcontent (Content, "text/html;charset=utf-8");
Send mail
Transport.send (msg);
}
/**
* Mass Mailing
*
* @param recipients
* Email address of the person receiving the letter
* @param subject
* Mail Title
* @param content
* Email Content
* @throws addressexception
* @throws messagingexception
*/
public void Send (list<string> recipients, string subject, string content) throws Addressexception, messagingexception{
Creating MIME-type messages
Final MimeMessage msg = new MimeMessage (session);
Set Sender
Msg.setfrom (New InternetAddress (Mailauthenticator.getusername ()));
Set up the people who receive the message
int num = Recipients.size ();
Internetaddress[] addresses = new Internetaddress[num];
for (int i = 0; i < num; i++) {
Addresses[i] = new InternetAddress (Recipients.get (i));
}
Msg.setrecipients (recipienttype.to, addresses);
Set the message header
Msg.setsubject (subject);
Set up message content
Msg.setcontent (Content, "text/html;charset=utf-8");
Transport.send (msg);
}
/**
* Send mail
*
* @param recipient
* Email address of the person receiving the letter
* @param mail
* Mail Object
* @throws addressexception
* @throws messagingexception
* @throws
*/
public void Send (String recipient, mail mail) throws Addressexception, messagingexception{
This.send (recipient, Mail.getsubject (), mail.getcontent ());
}
/**
* Mass Mailing
*
* @param recipients
* Email address of the person receiving the letter
* @param mail
* Mail Object
* @throws addressexception
* @throws messagingexception
* @throws
*/
public void Send (list<string> recipients, mail mail) throws Addressexception, messagingexception{
This.send (Recipients, Mail.getsubject (), mail.getcontent ());
}
}
/**
*
*/
Package com.email;

/**
* <p>description: </p>
* @author Chu Kaixin
* @date June 6, 2018
*
*/
Import javax.mail.MessagingException;
Import javax.mail.internet.AddressException;



/**
* Test Send mail
*/
public class MailTest {

public static void Main (string[] args) {
Sender Email Address: Fill out the email address you want to send out
String username = "[email protected]";
Sender Mailbox Login Password: Here fill in the mailbox to send the login password can
String password = "Akxpgntfdbtubcja";
Akxpgntfdbtubcja
Vulyumdckcaibfga
Akxpgntfdbtubcja
Create a Mailbox Sender
MailSender MailSender = new MailSender (Username,password);
Mailbox Object
Mail mail = new mail ("Zhukaixin", "I am the most handsome man in the World");
Recipient's email address: here to fill in the address of the mail inbox
String recipient = "[email protected]";
try {
Mailsender.send (recipient, mail);
} catch (Addressexception e) {
System.out.println ("Sender's mailbox path is incorrect ...");
E.printstacktrace ();
} catch (Messagingexception e) {
SYSTEM.OUT.PRINTLN ("message sent failed ...");
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Send successfully ...");
}
}

Java implementation of mail delivery

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.