发送带附件的邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发送带附件的邮件相关的知识,希望对你有一定的参考价值。
import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class EmailUtils { public static void main(String[] args) { Properties properties = new Properties(); properties.put("from", "[email protected]"); properties.put("password", "****"); String content = "this is a test email content"; String subject = "this is a test email subject"; List<String> filepathList = new ArrayList<String>(); filepathList.add("C:\\Documents and Settings\\Administrator\\桌面\\测试.docx"); List<String> toList = new ArrayList<String>(); toList.add("[email protected]"); send(properties, subject, content, filepathList, toList); } public static boolean send(Properties properties,String subject,String content,List<String> filepathList,List<String> toList){ String from = properties.getProperty("from"); String passwd = properties.getProperty("password"); String smtpServer = null; if(!properties.containsKey("smtpServer")){ smtpServer = getTriableSmtpServer(from); }else{ smtpServer = properties.getProperty("smtpServer"); } return send(from, passwd, subject, content, smtpServer, filepathList, toList); } public static boolean send(String from ,String passwd,String subject,String content,List<String> filepathList, List<String> toList){ String smtpServer = getTriableSmtpServer(from); return send(from, passwd, subject, content, smtpServer, filepathList, toList); } private static String getTriableSmtpServer(String from) { int begin = from.indexOf("@"); int end = from.indexOf("."); String smtpServer = "smtp."+from.substring(begin+1,end)+".com"; return smtpServer; } public static boolean send(String from, String passwd, String subject, String content, String smtpServer, List<String> filepathList, List<String> toList) { Properties properties = System.getProperties(); properties.setProperty("mail.smtp.auth", "false"); Session session = Session.getInstance(properties); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { InternetAddress fromAddress = new InternetAddress(from); message.setFrom(fromAddress); InternetAddress[] toAddressArray = new InternetAddress[toList .size()]; for (int i = 0; i < toAddressArray.length; i++) { toAddressArray[i] = new InternetAddress(toList.get(i)); } message.setRecipients(Message.RecipientType.TO, toAddressArray); message.setSubject(subject); if (null == filepathList || filepathList.size() == 0) { message.setContent(content, "text/html;charset=GBK"); } else { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(content, "text/html;charset=GBK"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); for (String filepath : filepathList) { mimeBodyPart = new MimeBodyPart(); DataSource ds = new FileDataSource(filepath); mimeBodyPart.setDataHandler(new DataHandler(ds)); filepath = new String(new File(filepath).getName().getBytes(), "GBK"); mimeBodyPart.setFileName(filepath); // mimeBodyPart.setFileName(new File(filepath).getName()); multipart.addBodyPart(mimeBodyPart); } message.setContent(multipart); } message.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(smtpServer, from, passwd); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (AddressException e) { e.printStackTrace(); return false; } catch (MessagingException e) { e.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
以上是关于发送带附件的邮件的主要内容,如果未能解决你的问题,请参考以下文章