时间:2021-05-20
本文实例为大家分享了Java Web stmp发送带附件邮件的具体代码,供大家参考,具体内容如下
public class MailFileSendUtils { private Properties props; //系统属性 private Session session; //邮件会话对象 private MimeMessage mimeMsg; //MIME邮件对象 private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 /** * 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 邮件发送服务器 */ public MailFileSendUtils(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(); } /** * 发送邮件 */ public boolean sendMail(String from, String[] to, String subject, String content, String filename) { try { //设置发信人 mimeMsg.setFrom(new InternetAddress(from)); //设置接收人 for (int i = 0; i < to.length; i++) { mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i])); } //设置抄送人// for (int i = 0; i < copyto.length; i++) {// mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i]));// } //设置主题 mimeMsg.setSubject(subject); //设置正文 BodyPart bp = new MimeBodyPart(); bp.setContent(content, "text/html;charset=utf-8"); mp.addBodyPart(bp); //设置附件 bp = new MimeBodyPart(); FileDataSource fileds = 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(); //发送邮件 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("邮件发送成功"); } 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 = "发送的邮箱账号"; String password = "发送的邮箱密码"; String from = "发送的邮箱"; String[] to = {"接收邮件的邮箱"};// String[] copyto = {"抄送的邮箱"}; String subject = "主题6"; String content = "邮件内容6"; String filename = "附件的文件"; MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);// email.sendMail(from, to, copyto, subject, content, filename); email.sendMail(from, to, subject, content, filename); }}(附:SSL版)
public class MailFileSendUtils { private Properties props; //系统属性 private Session session; //邮件会话对象 private MimeMessage mimeMsg; //MIME邮件对象 private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 /** * 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 邮件发送服务器 */ public MailFileSendUtils(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(); } /** * 发送邮件 */ public boolean sendMail(String from, String[] to, String subject, String content, String filename) { try { //设置发信人 mimeMsg.setFrom(new InternetAddress(from)); //设置接收人 for (int i = 0; i < to.length; i++) { mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i])); } //设置抄送人// for (int i = 0; i < copyto.length; i++) {// mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i]));// } //设置主题 mimeMsg.setSubject(subject); //设置正文 BodyPart bp = new MimeBodyPart(); bp.setContent(content, "text/html;charset=utf-8"); mp.addBodyPart(bp); //设置附件 bp = new MimeBodyPart(); FileDataSource fileds = 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(); //发送邮件 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("邮件发送成功"); } 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 = "邮箱";// String password = "邮箱密码";// String from = "谁去发";// String[] to = {"发给谁"};//// String[] copyto = {"抄送的邮箱"};// String subject = "huawei";// String 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);// }}在项目中使用这套工具,main方法我注释掉,然后使用toSendMail(SendMailParam sendMailParam)。
这里定义的SendMailParam 为:
maven依赖包
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>gradle依赖包
compile "javax.mail:mail:1.4.7"以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了php简单实现发送带附件的邮件。分享给大家供大家参考。具体如下:下面是静态html代码:带附件的邮件发送发送人:收件人:邮件主题:邮件内容:附件上
本实例所做功能为发送带附件邮件,可以上传多个附件,操作为选择一个附件以后自动上传,然后继续选择附件,填写完表单其他信息,点击保存发送带附件邮件。HTML标签js
利用javax.mail发送邮件,图片与附件都可发送1,Controller类packagecom.web.controller.api;importjavax
本文实例讲述了Codeigniter实现发送带附件的邮件的方法。分享给大家供大家参考。具体分析如下:attach()方法允许你的发邮件时带上附件,下面是演示代码
C++发邮件用的是阻塞式socket模型,发送完数据后需要接收返回值,才能接着发送。本程序不发送邮件附件,发附件的实例:C++实现含附件的邮件发送功能#incl