Java发送带附件email

参考 :https://blog.csdn.net/wgs_93/article/details/52093563

棒棒:https://blog.csdn.net/u013132051/article/details/52691657?fps=1&locationNum=9

public void sendMail(Mail mail, String warningText)
	{

		Properties properties = null;
		Session mailSession = null;
		MimeMessage mailMessage = null;
		String subject = (String) mail.get(Mail.TITLE);
		if (subject == null)
		{
			subject = "";
		}
		String content = (String) mail.get(Mail.CONTENTS);
		if (content == null)
		{
			content = "";
		}

		try
		{
			if (this.currentServer.isShowWarn() && !StringUtils.isNullString(warningText))
			{
				content = content + "\r\n" + warningText;
			}

			properties = new Properties();
			// 设置邮件服务器
			properties.put("mail.smtp.host", this.currentServer.getSMTP());
			if (StringUtils.isNullString(MailScheduledTask2.this.currentServer.getPassword()))
			{
				// 不验证密码
				Session.getInstance(properties, null);
			}
			else
			{
				// 验证密码
				properties.put("mail.smtp.auth", "true");
				// 根据属性新建一个邮件会话
				mailSession = Session.getInstance(properties, new Authenticator() {
					@Override
					public PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication(MailScheduledTask2.this.currentServer.getUserName(),
								MailScheduledTask2.this.currentServer.getPassword());
					}
				});
			}
			
			// mailSession.setDebug(true);
			// 建立消息对象
			mailMessage = new MimeMessage(mailSession);
			// 发件人
			mailMessage.setFrom(new InternetAddress(this.currentServer.getUserName()));
			// 收件人
			mailMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(this.mailAddress));
			// 主题
			// MimeUtility.encodeText(subject, "UTF-8", "B");
			mailMessage.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
			
			//===================================添加附件和内容start==========================================
			MimeMultipart allPart = new MimeMultipart("mixed");//创建一个MimeMultipart组件,用来装载附件  
			for (String fileInfo : fileList)
			{
				String[]str = fileInfo.split("!O!");
				String fileName = str[0];//附件名称
				String filePath = str[1];//附件绝对路径
				MimeBodyPart attachmentPart = new MimeBodyPart();//装载邮件附件
				FileDataSource fds = new FileDataSource(filePath);//根据附件绝对路径,得到数据源  
				attachmentPart.setDataHandler(new DataHandler(fds));//得到附件本身,并装载进入attachmentPart(MimeBodyPart)
				//attachmentPart.setFileName(fileName);
				attachmentPart.setFileName(MimeUtility.encodeText(fileName));//得到文件名,同样装载进入attachmentPart(MimeBodyPart),并且对文件名进行编码处理
				allPart.addBodyPart(attachmentPart);//附件也变成邮件的内容  
			}
			MimeBodyPart bodyContent = new MimeBodyPart();
			mail.getSenderUser();
			User user = aas.getUser(mail.getSenderUser());
			bodyContent.setText("发件人: "+user.getName()+"\n"+"Email  : "+user.getEmail()+"\n"+content);
			allPart.addBodyPart(bodyContent);
			mailMessage.setContent(allPart);  
			//====================================添加附件和内容end=========================================
			
			// 发信时间
			mailMessage.setSentDate(new Date());
			// 存储信息
			mailMessage.saveChanges();

			try
			{
				Transport.send(mailMessage);
				DynaLogger.debug("Send e-mail successful, user:" + this.mailAddress + " subject:" + subject
						+ " content:" + content);
			}
			catch (MessagingException e)
			{
				DynaLogger.error("Send e-mail error: " + e + ", " + e.getMessage());
			}

		}
		catch (Exception e)
		{
			DynaLogger.error("Send e-mail error: " + e + ", " + e.getMessage());
		}
		finally
		{
		}
	}

猜你喜欢

转载自blog.csdn.net/Grace_1203/article/details/82877729
今日推荐