This article shares the specific code of Java Web stmp sending emails with attachments for your reference. The specific content is as follows
public class MailFileSendUtils { private Properties props; //System property private Session session; //Mail session object private MimeMessage mimeMsg; //MIME mail object private Multipart mp; //Multipart object, email content, title, attachment and other content are all added to it and then the MimeMessage object is generated/** * Constructor * @param */ public MailFileSendUtils(){ props = System.getProperties(); props.put("mail.smtp.auth","false"); session = Session.getDefaultInstance(props, null); session.setDebug(true); mimeMsg = new MimeMessage(session); mp = new MimeMultipart(); } /** * Constructor * @param smtp Mail SendUtils(String smtp, String username, String password){ props = System.getProperties(); props.put("mail.smtp.auth","true"); props.put("mail.smtp.host", smtp); props.put("username", username); props.put("password", password); session = Session.getDefaultInstance(props, null); session.setDebug(true); mimeMsg = new MimeMessage(session); mp = new MimeMultipart(); } /** * Send email*/ public boolean sendMail(String from, String[] to, String subject, String content, String filename) { try { //Set the sender mimeMsg.setFrom(new InternetAddress(from)); //Set the recipient for (int i = 0; i < to.length; i++) { mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i])); } //Set cc// for (int i = 0; i < copyto.length; i++) {// mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i])); // } //Set the topic mimeMsg.setSubject(subject); //Set the text BodyPart bp = new MimeBodyPart(); bp.setContent(content, "text/html;charset=utf-8"); mp.addBodyPart(bp); //Set attachment bp = new MimeBodyPart(); FileDataSource files = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B")); mp.addBodyPart(bp); mimeMsg.setContent(mp); mimeMsg.saveChanges(); //Send email if(props.get("mail.smtp.auth").equals("true")){ Transport transport = session.getTransport("smtp"); transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password")); transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));// transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC)); transport.close(); }else{ Transport.send(mimeMsg); } System.out.println("Mail sending successfully"); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; }// public void toSendMail(SendMailParam sendMailParam){// MailFileSendUtils email = new MailFileSendUtils(sendMailParam.getSmtp(), sendMailParam.getUsername(), sendMailParam.getPassword());// email.sendMail(sendMailParam.getFrom(), sendMailParam.getTo(), sendMailParam.getSubject(), sendMailParam.getContent(), sendMailParam.getFilepath());// } public static void main(String[] args) { String smtp = "smtp.exmail.qq.com"; String username = "send email account"; String password = "send email password"; String from = "send email"; String[] to = {"email address to receive email"};// String[] copyto = {"ccc email address"}; String subject = "subject 6"; String content = "mail content 6"; String filename = "attached file"; MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);// email.sendMail(from, to, copyto, subject, content, filename); email.sendMail(from, to, subject, content, filename); }}(attachment: SSL version)
public class MailFileSendUtils { private Properties props; //System property private Session session; //Mail session object private MimeMessage mimeMsg; //MIME mail object private Multipart mp; //Multipart object, email content, title, attachment and other content are all added to it and then the MimeMessage object is generated/** * Constructor * @param */ public MailFileSendUtils(){ props = System.getProperties(); props.put("mail.smtp.auth","false"); session = Session.getDefaultInstance(props, null); session.setDebug(true); mimeMsg = new MimeMessage(session); mp = new MimeMultipart(); } /** * Constructor * @param smtp Mail SendUtils(String smtp, String username, String password){ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; props = System.getProperties(); MailSSLSocketFactory sf = null; try { sf = new MailSSLSocketFactory(); } catch (GeneralSecurityException e) { } sf.setTrustAllHosts(true); props.put("mail.smtp.auth","true"); props.put("mail.smtp.host", smtp); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.ssl.socketFactory", sf);// props.put("username", username);// props.put("password", password); session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); session.setDebug(true); mimeMsg = new MimeMessage(session); mp = new MimeMultipart(); } /** * Send email*/ public boolean sendMail(String from, String[] to, String subject, String content, String filename) { try { //Set the sender mimeMsg.setFrom(new InternetAddress(from)); //Set the receiver for (int i = 0; i < to.length; i++) { mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i])); } //Set cc// for (int i = 0; i < copyto.length; i++) {// mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i]));// } //Set topic mimeMsg.setSubject(subject); //Set the text BodyPart bp = new MimeBodyPart(); bp.setContent(content, "text/html;charset=utf-8"); mp.addBodyPart(bp); //Set attachment bp = new MimeBodyPart(); FileDataSource files = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B")); mp.addBodyPart(bp); mimeMsg.setContent(mp); mimeMsg.saveChanges(); //Send mail if(props.get("mail.smtp.auth").equals("true")){ Transport transport = session.getTransport("smtp"); transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password")); transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO)); // transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC)); transport.close(); }else{ Transport.send(mimeMsg); } System.out.println("Mails sent successfully"); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return true; } public boolean toSendMail(SendMailParam sendMailParam){ MailFileSendUtils email = new MailFileSendUtils( sendMailParam.getSmtp(), sendMailParam.getUsername(), sendMailParam.getPassword()); email.sendMail( sendMailParam.getFrom(), sendMailParam.getTo(), sendMailParam.getSubject(), sendMailParam.getContent(), sendMailParam.getFilepath()); return true; }// public static void main(String[] args) {// String smtp = "smtp.mxhichina.com";// String username = "emailbox";// String password = "email password";// String from = "who will send";// String[] to = {"send to whom"};/// String[] copyto = {"ccc email"};// String subject = "huawei";// String content = "email content 6666";// String filename = "gdt-3583118353-ad-20170823.xls";// MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);//// email.sendMail(from, to, copyto, subject, content, filename);// email.sendMail(from, to, subject, content, filename);// }} In the project, I comment out the main method and then use toSendMail (SendMailParam sendMailParam).
The SendMailParam defined here is:
public class SendMailParam { private String smtp; private String username; private String password; private String from;//Sender private String[] to;//Recipient//String[] copyright = {"[email protected]"}; private String subject;//Email subject private String content;//Email content private String filepath;//Path to get the file public SendMailParam(){ this.smtp = "smtp.exmail.qq.com";//Example this.username = "Email account"; this.password = "Email password"; this.from = "Email"; this.subject = ""; this.content = ""; this.filepath = ""; } public String getSmtp() { return smtp; } public void setSmtp(String smtp) { this.smtp = smtp; } 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 String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String[] getTo() { return to; } public void setTo(String[] to) { this.to = to; } 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 getFilepath() { return filepath; } public void setFilepath(String filepath) { this.filepath = filepath; }}maven dependency package
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
gradle dependency package
compile "javax.mail:mail:1.4.7"
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.