Java mail (with attachments) supports SSL and ssl
Java mail has three types of email sending (with attachments ):
MailSenderInfo. java
Package mail; import java. util. properties; import java. util. vector; public class MailSenderInfo {// IP address and port of the server sending the mail private String mailServerHost; private String mailServerPort = "25"; // private String fromAddress; // The email recipient's address: private String toAddress; // the userName and password used to log on to the email sending server: private String userName; private String password; // whether authentication is required: private boolean validate = false; // whether to enable ssl private boole An validateSSL = false; // Email subject private String subject; // The text content of the email private String content; // the file name of the email attachment private Vector attachFileNames; /*** get mail session Properties */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 Vector getAttachFileNames () {return attachFileNames;} public void setAttachFileNames (Vector attachFileNames) {this. attachFileNames = attachFileNames;} 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;} public boolean isValidateSSL () {return validateSSL;} public void setValidateSSL (boolean validateSSL) {this. validateSSL = validateSSL ;}}
MailAuthenticator. java
package mail;import javax.mail.*;public class MailAuthenticator extends Authenticator {private String userName;private String password;public MailAuthenticator() {}public MailAuthenticator(String username, String password) {this.userName = username;this.password = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(userName, password);}}
SimpleMailSender. java
Package mail; import java. util. date; import java. util. enumeration; import java. util. properties; import java. util. vector; import javax. activation. dataHandler; import javax. activation. fileDataSource; 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; import javax. mail. internet. mimeUtility;/*** simple email (email with attachment) sender */public class SimpleMailSender {/*** send an email with html (with attachment) ** @ param mailInfo * message of the email to be sent */public boolean sendHtmlAndAffixMail (MailSenderInfo mailInfo) {// determine whether identity authentication is required for MailAuthenticator authenticator = null; Properties pro = mailInfo. getProperties (); if (mailInfo. isValidateSSL () {pro. put ("mail. smtp. starttls. enable "," true "); pro. put ("mail. smtp. socketFactory. class "," javax.net. ssl. SSLSocketFactory ");} // if identity authentication is required, create a password validator if (mailInfo. isValidate () {authenticator = new MailAuthenticator (mailInfo. getUserName (), mailInfo. getPassword ();} // construct a sessionSession session = Session for sending an email Based on the mail session attribute and password validators. getDefaultInstance (pro, authenticator); try {MimeMessage msg = new MimeMessage (session); // construct the MimeMessage and set the basic value // MimeMessage msg = new MimeMessage (); msg. setFrom (new InternetAddress (mailInfo. getFromAddress (); // msg. addRecipients (Message. recipientType. TO, address); // This can only send emailmsg TO a person. setRecipients (Message. recipientType. TO, InternetAddress. parse (mailInfo. getToAddress (); msg. setSubject (MimeUtility. encodeText (mailInfo. getSubject (); Multipart mp = new MimeMultipart (); // construct MultipartMimeBodyPart mbpContent = new MimeBodyPart (); // Add the body mbpContent to Multipart. setContent (mailInfo. getContent (), "text/html; charset = GB2312"); mp. addBodyPart (mbpContent); // Add to MimeMessage (Multipart represents the body) Vector file = mailInfo. getAttachFileNames (); Enumeration efile = file. elements (); // Add the attachment while (efile. hasMoreElements () {MimeBodyPart mbpFile = new MimeBodyPart (); String filename = efile. nextElement (). toString (); System. out. println (filename. toLowerCase (); FileDataSource fds = new FileDataSource (filename); mbpFile. setDataHandler (new DataHandler (fds); System. out. println (fds. getName (); mbpFile. setFileName (MimeUtility. encodeText (fds. getName (); // Add to MimeMessage (Multipart represents the attachment) mp. addBodyPart (mbpFile);} file. removeAllElements (); // Add MimeMessagemsg to Multipart. setContent (mp); msg. setSentDate (new Date (); msg. saveChanges (); // send the email Transport transport = session. getTransport ("smtp"); transport. connect (mailInfo. getMailServerHost (), mailInfo. getUserName (), mailInfo. getPassword (); transport. sendMessage (msg, msg. getAllRecipients (); transport. close ();} catch (Exception mex) {mex. printStackTrace (); return false;} return true ;}}
Test class
Package mail; import java. util. vector; public class MailTest {/*** @ param args */public static void main (String [] args) {// This class is mainly used to set MailSenderInfo mailInfo = new MailSenderInfo (); mailInfo. setMailServerHost (host); mailInfo. setMailServerPort ("465"); mailInfo. setValidate (true); mailInfo. setValidateSSL (true); mailInfo. setUserName ("username"); mailInfo. setPassword ("pwd"); // your email password mailInfo. setFromAddr Ess ("address"); mailInfo. setToAddress ("XXXXX@sina.cn"); mailInfo. setSubject ("set email title"); mailInfo. setContent ("today should be the day of meizu powder! "+" Meizu officially released MX4 in Beijing today. meizu MX4 uses a 5.36-inch 1920x1152 resolution screen (PPI418) and is equipped with the eight-core processor of mediatek. It provides a 20.7 million pixel camera and a mah battery, run the brand new Flyme 4.0 system, support mobile and Unicom network dual 4G, antu rabbit run points 46124 points, provide dark gray, pure white and tuhaojin version, 16G version price 1799 yuan, the price of the 32G version is 1999 yuan. "); // This class mainly sends the mail Vector fileNames = new Vector (); fileNames. add ("D :\\ delete \ weekly report 20140903103213.xls"); fileNames. add ("D :\\ delete \ recovery weekly report 20140903103213.xls"); fileNames. add ("D :\\ delete \ duplicate (2) Weekly Report 20140903103213.xls"); mailInfo. setAttachFileNames (fileNames); SimpleMailSender sms = new SimpleMailSender (); // sms. sendTextMail (mailInfo); // The text style of sms. sendHtmlAndAffixMail (mailInfo); // sends the message in html format }}
How can I implement an attachment when a java mail sends an email?
Java mail sends a program with a nearby component. You can search for an example.
This is not uploaded to the email server. It is included in the email when the email is sent,
What you can see in the webpage mailbox is to use the background program to parse the email, extract the file and put it on the server for download.
Javamail sends an email with an Excel attachment
I have an available one here. It is also from the network and can be tested ..