Java sends mail javax.mail
- 2020-05-05 11:12:06
- OfStack
Direct on the source:
1. Basic message information (set to static for convenience)
below is best as an javabean
package com.lgf.Email;
import java.util.Date;
public class EmailMessage
{
/**
* Message in the mail , You can set it up yourself.
* To make it easy to set it all to static
*/
// Recipient's mailbox
public static String TO = "xxx@xxx.com";
// Sender email
public static String FROM = "xxx@xxx.com";
public static String FROM_NAME = "xxx";
// Cc people
public static String CC="xxx@nxxx.com";
// Dark to send people
public static String BCC="xxx@nxxx.com";
// Type of message
public static String Email_Content = "text/plain";
// Email title
public static String Email_Subject = "Test Email With JavaMail";
// header
public static String Email_Header = "This Is Email Header";
// content
public static String Email_Body = "<a href=\"http://www.baidu.com\">This Is Email Body</a>";
// The server Such as QQ Can be set to smtp.qq.com
public static String Email_Host = "smtp.xxx.com";
// Send time
public static Date sendDate = new Date();
// Do you need to verify the username and password
public static boolean validate = true;
}
2. Server authentication (that is, the user name and password at the time of normal login)
package com.lgf.Email;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* Authentication of username and password
* @author lgf
*
*/
public class MailAuthenticator extends Authenticator{
private String username="xxxxx";
private String password="xxxxx";
public MailAuthenticator() {
super();
}
/**
* Set the username and password for authentication
*/
public MailAuthenticator(String userName , String password) {
super();
this.username = userName;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.username,this.password);
}
}
3. Set up the information and steps
before sending the email
package com.lgf.Email;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
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.lgf.SendEmail.MailAuthenticator;
public class SendEmail
{
/**
* Sending regular mail
* @throws Exception
*/
public void doSendNormalMail()
{
// Acquiring system environment
Properties prop = new Properties();
Authenticator auth = null;
// Determine whether validation is required to send the message
if (EmailMessage.validate)
{
// Mail server authentication Username and password
auth = new MailAuthenticator();
}
// Add the necessary information
prop.put("mail.smtp.host", EmailMessage.Email_Host);
prop.put("mail.smtp.auth", "true");
// Set up a dialog to communicate with the mail server
Session session = Session.getDefaultInstance(prop, auth);
// Display in console Debug information
session.setDebug(true);
// Set the mail object
Message message = new MimeMessage(session);
try
{
// Set email subject
message.setSubject(EmailMessage.Email_Subject);
// Set the subject line
message.setHeader("Header", EmailMessage.Email_Header);
// Set sending time
message.setSentDate(EmailMessage.sendDate);
// Set the sender address and The name
Address address = new InternetAddress(EmailMessage.FROM, EmailMessage.FROM_NAME);
// Adds the sender message to the message
message.setFrom(address);
// Set sender address
Address toAddress = new InternetAddress(EmailMessage.TO);
// Set the recipient address
message.setRecipient(Message.RecipientType.TO, toAddress);
// Set multiple recipient addresses
// message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com"));
// Format your message
message.setContent("Content", EmailMessage.Email_Content);
// Set email content Must be after setting the file format
message.setText(EmailMessage.Email_Body);
// Save the information added above
message.saveChanges();
// Send E-mail
System.out.println("sendNormalEmail() Start sending emails... ");
Transport.send(message);
System.out.println(" Send successfully! ");
} catch (Exception e)
{
System.out.println(" error ");
e.printStackTrace();
}
}
/**
* send HTML Formatted mail
*/
public void doSendHtmlMail()
{
// Acquiring system environment
Properties prop = new Properties();
Authenticator auth = null;
if (EmailMessage.validate)
{
// Mail server authentication Username and password
auth = new MailAuthenticator();
}
// Add the necessary information
prop.put("mail.smtp.host", EmailMessage.Email_Host);
prop.put("mail.smtp.auth", "true");
// Set up a dialog to communicate with the mail server
Session session = Session.getDefaultInstance(prop, auth);
// Set the mail object
Message message = new MimeMessage(session);
try
{
// Set email subject
message.setSubject(EmailMessage.Email_Subject);
// Set the subject line
message.setHeader("Header", EmailMessage.Email_Header);
// Set sending time
message.setSentDate(EmailMessage.sendDate);
// Set the sender address and The name
Address address = new InternetAddress(EmailMessage.FROM, EmailMessage.FROM_NAME);
// Adds the sender message to the message
message.setFrom(address);
// Set sender address
Address toAddress = new InternetAddress(EmailMessage.TO);
// Set the recipient address
message.setRecipient(Message.RecipientType.TO, toAddress);
// Set multiple recipient addresses
// message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com"));
// Sets the content of the message to be sent The following is sending hmml
// Format your message
EmailMessage.Email_Content = "text/html; charset=utf-8";
message.setContent(EmailMessage.Email_Body, EmailMessage.Email_Content);
// Save the information added above
message.saveChanges();
// Send E-mail
System.out.println("doSendHtmlMail() Start sending emails... ");
Transport.send(message);
System.out.println(" Send successfully! ");
} catch (Exception e)
{
System.out.println(" error ");
e.printStackTrace();
}
}
/**
* Send email with attachment format
*/
public void doSendAttachmentMail()
{
// Acquiring system environment
Properties prop = new Properties();
Authenticator auth = null;
if (EmailMessage.validate)
{
// Mail server authentication Username and password
auth = new MailAuthenticator();
}
// Add the necessary information
prop.put("mail.smtp.host", EmailMessage.Email_Host);
prop.put("mail.smtp.auth", "true");
// Set up a dialog to communicate with the mail server
Session session = Session.getDefaultInstance(prop, auth);
// Set the mail object
Message message = new MimeMessage(session);
try
{
// Set email subject
message.setSubject(EmailMessage.Email_Subject);
// Set the subject line
message.setHeader("Header", EmailMessage.Email_Header);
// Set sending time
message.setSentDate(EmailMessage.sendDate);
// Set the sender address and The name
Address address = new InternetAddress(EmailMessage.FROM, EmailMessage.FROM_NAME);
// Adds the sender message to the message
message.setFrom(address);
// Set sender address
Address toAddress = new InternetAddress(EmailMessage.TO);
// Set the recipient address
message.setRecipient(Message.RecipientType.TO, toAddress);
// Set multiple recipient addresses
// message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com"));
// Sets the content of the message to be sent The attachment is sent below
message.setContent(EmailMessage.Email_Body, EmailMessage.Email_Content);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("bodypart");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
// Set up the uploaded resource
DataSource source = new FileDataSource("E:\\3.doc");
// Added to the
messageBodyPart.setDataHandler(new DataHandler(source));
// Set file name , Remember the suffix
messageBodyPart.setFileName("test.doc");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// Save the information added above
message.saveChanges();
// Send E-mail
System.out.println("doSendAttachmentMail() Start sending emails... ");
Transport.send(message);
System.out.println(" Send successfully! ");
} catch (Exception e)
{
System.out.println(" error ");
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args)
{
// new SendEmail().doSendNormalMail();
// new SendEmail().doSendHtmlMail();
new SendEmail().doSendAttachmentMail();
}
}
The above is the Java email javax.mail detailed code, I hope to help you achieve java email.