电子邮件的发送分两种:
1、发送的附件是在本地服务器上面,可以在置顶文件目录下面直接获取文件进行发送文件
(1)使用
JavaMailSenderImpl技术 简单的发送邮件 JavaMailSenderImpl mailSender = new JavaMailSenderImpl() mailSender.setHost(smtpHost)//发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com. 网易163邮箱的 SMTP 服务器地址为: smtp.163.com mailSender.setUsername(account) //发件人邮箱账号 mailSender.setPassword(password) //发件人邮箱密码 Properties javaMailProperties = new Properties() javaMailProperties.put("mail.smtp.auth", true) javaMailProperties.put("mail.smtp.starttls.enable", true) javaMailProperties.put("mail.smtp.timeout", 5000) javaMailProperties.put("mail.smtps.ssl.checkserveridentity", "false") mailSender.setJavaMailProperties(javaMailProperties) //创建邮件内容 SimpleMailMessage message=new SimpleMailMessage() message.setFrom(account) message.setTo(to) message.setSubject(sysConfigCache.getValueByCode("sendEmail.subject"))//发送内容主题 message.setText(sysConfigCache.getValueByCode("sendEmail.text"))//发送的内容 //发送邮件 mailSender.send(message)
(2)直接发送附件的方式
JavaMailSenderImpl mailSender = new JavaMailSenderImpl() mailSender.setHost(smtpHost) mailSender.setUsername(account) mailSender.setPassword(password) Properties javaMailProperties = new Properties() javaMailProperties.put("mail.smtp.auth", true) javaMailProperties.put("mail.smtp.starttls.enable", true) javaMailProperties.put("mail.smtp.timeout", 5000) javaMailProperties.put("mail.smtps.ssl.checkserveridentity", "false") mailSender.setJavaMailProperties(javaMailProperties) MimeMessage msg = mailSender.createMimeMessage() MimeMessageHelper helper = new MimeMessageHelper(msg,true ,"UTF-8") helper.setFrom(mailSender.getUsername()) helper.setTo(to) helper.setSubject(sysConfigCache.getValueByCode("sendEmail.subject")) helper.setText(sysConfigCache.getValueByCode("sendEmail.text")) FileSystemResource fileAdd = new FileSystemResource(发送文件地址+"/"+name) helper.addAttachment(fileAdd.getFilename(),fileAdd) // 发送邮件 mailSender.send(msg)
(3)发送附件直接预览
JavaMailSenderImpl mailSender = new JavaMailSenderImpl() mailSender.setHost(smtpHost) mailSender.setUsername(account) mailSender.setPassword(password) Properties javaMailProperties = new Properties() javaMailProperties.put("mail.smtp.auth", true) javaMailProperties.put("mail.smtp.starttls.enable", true) javaMailProperties.put("mail.smtp.timeout", 5000) mailSender.setJavaMailProperties(javaMailProperties) MimeMessage msg = mailSender.createMimeMessage() MimeMessageHelper helper = new MimeMessageHelper(msg,true ,"UTF-8") helper.setFrom(mailSender.getUsername()) helper.setTo(to) helper.setSubject(sysConfigCache.getValueByCode("sendEmail.subject")) helper.setText("<body><p>"+sysConfigCache.getValueByCode("sendEmail.text")+"</p><img src='cid:file'/></body>", true) FileSystemResource fileAdd = new FileSystemResource(发送文件地址+"/"+name) helper.addInline(fileAdd.getFilename(),fileAdd) // 发送邮件 mailSender.send(msg)
2、直接将附件照片以内容的方式发送
Properties props = System.getProperties() props.setProperty("mail.transport.protocol", "smtp") // 使用的协议(JavaMail规范要求) props.setProperty("mail.smtp.host", smtpHost) props.setProperty("mail.smtp.auth", "true") props.setProperty("mail.debug", "true") props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory") props.setProperty("mail.smtp.socketFactory.fallback", "false") final String smtpPort = "465" props.setProperty("mail.smtp.port", smtpPort) // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = Session.getDefaultInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(account, password) } }) Message mailMessage = new MimeMessage(sendMailSession)// 根据session创建一个邮件消息 mailMessage.setFrom(new InternetAddress(account)) // 设置邮件消息的发送者 mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)) // 创建邮件的接收者地址,并设置到邮件消息中 mailMessage.setSubject(sysConfigCache.getValueByCode("sendEmail.subject")) mailMessage.setSentDate(new Date()) // 设置邮件消息发送的时间 // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mainPart = new MimeMultipart() BodyPart html = new MimeBodyPart()// 创建一个包含HTML内容的MimeBodyPart //设置HTML内容 html.setContent("<html><body>《可放发送内容》<img src="发送url地址"></body></html>", "text/html; charset=utf-8") mainPart.addBodyPart(html)//将MiniMultipart对象设置为邮件内容 mailMessage.setContent(mainPart) //发送邮件 Transport.send(mailMessage)