Simple email sending

Source: Internet
Author: User

Package com. util. mail;
Import java. util. Date;
Import java. util. Properties;
Import javax. mail. Address;
Import javax. mail. BodyPart;
Import javax. mail. Message;
Import javax. mail. MessagingException;
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;
/**
* Simple email (email without attachments) sender
*/
Public class SimpleMailSender {
/**
* Send emails in text format
* @ Param mailInfo information of the email to be sent
*/
Public boolean sendTextMail (MailSenderInfo mailInfo ){
// Determine whether identity authentication is required
MyAuthenticator authenticator = null;
Properties pro = mailInfo. getProperties ();
If (mailInfo. isValidate ()){
// If You Need identity authentication, create a password authenticator
Authenticator = new MyAuthenticator (mailInfo. getUserName (), mailInfo. getPassword ());
}
// Construct a mail sending session based on the mail session attribute and the password validator
Session sendMailSession = Session. getDefaultInstance (pro, authenticator );
Try {
// Create an email message based on the session
Message mailMessage = new MimeMessage (sendMailSession );
// Create the mail sender address
Address from = new InternetAddress (mailInfo. getFromAddress ());
// Set the sender of the email message
MailMessage. setFrom (from );
// Create the email receiver address and set it to the email message
Address to = new InternetAddress (mailInfo. getToAddress ());
MailMessage. setRecipient (Message. RecipientType. TO, );
// Set the subject of the email message
MailMessage. setSubject (mailInfo. getSubject ());
// Set the mail message sending time
MailMessage. setSentDate (new Date ());
// Set the main content of the email message
String mailContent = mailInfo. getContent ();
MailMessage. setText (mailContent );
// Send an email
Transport. send (mailMessage );
Return true;
} Catch (MessagingException ex ){
Ex. printStackTrace ();
}
Return false;
}
/**
* Send emails in HTML Format
* @ Param mailInfo refers to the mail information to be sent.
*/
Public static boolean sendHtmlMail (MailSenderInfo mailInfo ){
// Determine whether identity authentication is required
MyAuthenticator authenticator = null;
Properties pro = mailInfo. getProperties ();
// If You Need identity authentication, create a password authenticator
If (mailInfo. isValidate ()){
Authenticator = new MyAuthenticator (mailInfo. getUserName (), mailInfo. getPassword ());
}
// Construct a mail sending session based on the mail session attribute and the password validator
Session sendMailSession = Session. getDefaultInstance (pro, authenticator );
Try {
// Create an email message based on the session
Message mailMessage = new MimeMessage (sendMailSession );
// Create the mail sender address
Address from = new InternetAddress (mailInfo. getFromAddress ());
// Set the sender of the email message
MailMessage. setFrom (from );
// Create the email receiver address and set it to the email message
Address to = new InternetAddress (mailInfo. getToAddress ());
// The Message. RecipientType. TO attribute indicates that the type of the recipient is
MailMessage. setRecipient (Message. RecipientType. TO, );
// Set the subject of the email message
MailMessage. setSubject (mailInfo. getSubject ());
// Set the mail message sending time
MailMessage. setSentDate (new Date ());
// The MiniMultipart class is a container class that contains objects of the MimeBodyPart type.
Multipart mainPart = new MimeMultipart ();
// Create a MimeBodyPart containing HTML content
BodyPart html = new MimeBodyPart ();
// Set the HTML content www.2cto.com
Html. setContent (mailInfo. getContent (), "text/html; charset = UTF-8 ");
MainPart. addBodyPart (html );
// Set the MiniMultipart object to the Mail content
MailMessage. setContent (mainPart );
// Send an email
Transport. send (mailMessage );
Return true;
} Catch (MessagingException ex ){
Ex. printStackTrace ();
}
Return false;
}
}
Package com. util. mail;
Import java. util. Properties;
/**
* Basic information required for sending emails
*
*/
Public class MailSenderInfo {
// IP address and port of the server sending the mail
Private String mailServerHost;
Private String mailServerPort = "25 ";
// The sender's address
Private String fromAddress;
// Email recipient's address
Private String toAddress;
// Log on to the email sending server's username and password
Private String userName;
Private String password;
// Whether authentication is required
Private boolean validate = false;
// Email Subject
Private String subject;
// Text of the email
Private String content;
// File Name of the email attachment
Private String [] attachFileNames;
/**
* Get mail session attributes
*/
Public Properties getProperties (){
Properties p = new Properties ();
P. put ("mail. smtp. host", this. mailServerHost );
P. put ("mail. smtp. port", this. mailServerPort );
P. put ("mail. smtp. auth", validate? "True": "false ");
Return p;
}
Public String getMailServerHost (){
Return mailServerHost;
}
Public void setMailServerHost (String mailServerHost ){
This. mailServerHost = mailServerHost;
}
Public String getMailServerPort (){
Return mailServerPort;
}
Public void setMailServerPort (String mailServerPort ){
This. mailServerPort = mailServerPort;
}
Public boolean isValidate (){
Return validate;
}
Public void setValidate (boolean validate ){
This. validate = validate;
}
Public String [] getAttachFileNames (){
Return attachFileNames;
}
Public void setAttachFileNames (String [] fileNames ){
This. attachFileNames = fileNames;
}
Public String getFromAddress (){
Return fromAddress;
}
Public void setFromAddress (String fromAddress ){
This. fromAddress = fromAddress;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
Public String getToAddress (){
Return toAddress;
}
Public void setToAddress (String toAddress ){
This. toAddress = toAddress;
}
Public String getUserName (){
Return userName;
}
Public void setUserName (String userName ){
This. userName = userName;
}
Public String getSubject (){
Return subject;
}
Public void setSubject (String subject ){
This. subject = subject;
}
Public String getContent (){
Return content;
}
Public void setContent (String textContent ){
This. content = textContent;
}
}
 
 
Package com. util. mail;
Import javax. mail .*;
Public class MyAuthenticator extends Authenticator {
String userName = null;
String password = null;
Public MyAuthenticator (){
}
Public MyAuthenticator (String username, String password ){
This. userName = username;
This. password = password;
}
Protected PasswordAuthentication getPasswordAuthentication (){
Return new PasswordAuthentication (userName, password );
}

Public static void main (String [] args ){
// This class is mainly used to set emails
MailSenderInfo mailInfo = new MailSenderInfo ();
MailInfo. setMailServerHost ("smtp.sina.com ");
MailInfo. setMailServerPort ("25 ");
MailInfo. setValidate (true );

MailInfo. setSubject ("test email ");
MailInfo. setContent ("test mail content ");
// This class mainly sends emails
SimpleMailSender sms = new SimpleMailSender ();
Sms. sendTextMail (mailInfo); // sending Style
// Sms. sendHtmlMail (mailInfo); // sends the message in html format.
}
}
 

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.