邮件发送

	public String sendMail(Mail entity){
			if ("text".equals(entity.getType())){
				return sendTextMail(entity);
			}else if ("html".equals(entity.getType())){
				return sendHtmlMail(entity);
			}else if ("cureText".equals(entity.getType()))
			{
				return sendCureTextMail(entity);
			}
			else{
				return "Wrong type, please select text or html.";
			}
		}
		
		/*
		 * Send text mail
		 * (non-Javadoc)
		 * @see com.jiwu.publicejb.service.MailManager#sendMail(com.jiwu.publicejb.entity.Mail)
		 */
		public String sendHtmlMail(Mail mailBean){
			InputStream in;
			try {
				in = new BufferedInputStream(new FileInputStream(EmailUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath().concat("mail.properties")));
				Properties p = new Properties();
				p.load(in);

				//MyAuthenticator authen = new MyAuthenticator(p.getProperty("userName"),p.getProperty("password"));
				//根据邮件会话属性和密码验证器构造一个发送邮件的session 
				Properties ps = new Properties();
				ps.put("mail.smtp.host", p.getProperty("mail.smtp.host"));
				ps.put("mail.smtp.auth", p.getProperty("mail.smtp.auth"));
				Session session = Session.getDefaultInstance(ps, null);
				
				//根据session创建消息
				Message msg = new MimeMessage(session);
				//创建发送地址
				Address from = new InternetAddress(p.getProperty("userName"));
				//为消息设置发送者
				msg.setFrom(from);
				//创建接收地址
				InternetAddress[] addresses = new InternetAddress().parse(mailBean.getDestinationAddress());
				//为消息设置接收地址
				msg.setRecipients(Message.RecipientType.TO, addresses);
				//设置发送日期
				msg.setSentDate(new Date());
				//设置消息主题
				msg.setSubject(mailBean.getTheme());
				// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象    
				Multipart   mp   =   new   MimeMultipart();  
				//设置文本内容
				if(mailBean.getMessageBody()!= null)
				{	
					//创建一个BodyPart
					BodyPart mbp = new MimeBodyPart();
					mbp.setContent(mailBean.getMessageBody(), "text/html;charset=utf-8");
					mp.addBodyPart(mbp);
				}
				//附件处理
				if(mailBean.getAttchmentPath()!= null)
				{
					for(int i=0 ; i < mailBean.getAttchmentPath().length ; i++ )
					{
						BodyPart mbp1 = new MimeBodyPart();
						FileDataSource fds = new FileDataSource(mailBean.getAttchmentPath()[i]);
						String lstSttachFileName[] = mailBean.getAttchmentPath()[i].split("\\.");
						
						String attachFileName = lstSttachFileName[lstSttachFileName.length-1];
						mbp1.setDataHandler(new DataHandler(fds));
						//设置附件名称
						mbp1.setFileName(MimeUtility.encodeText(attachFileName, "utf-8", null));
						mp.addBodyPart(mbp1);
					}
				}
				//将MiniMultipart设置为邮件内容
				msg.setContent(mp);
				//邮件服务器验证
				Transport trans = session.getTransport("smtp");
				trans.connect(p.getProperty("mail.smtp.host"),p.getProperty("userName"),p.getProperty("password"));
				//发送邮件
				trans.sendMessage(msg,msg.getAllRecipients());
				System.out.println("发送成功");
				trans.close();
				return "ok";

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "error";
			}

		}
		
		/*
		 * Send html mail
		 * (non-Javadoc)
		 * @see com.jiwu.publicejb.service.MailManager#sendMail(com.jiwu.publicejb.entity.Mail)
		 */
		public String sendTextMail(Mail mailBean){
			InputStream in;
			try {
				in = new BufferedInputStream(new FileInputStream(EmailUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath().concat("mail.properties")));
				//in = new BufferedInputStream(new FileInputStream("D:\\mail.properties"));
				
				Properties p = new Properties();
				p.load(in);

				//MyAuthenticator authen = new MyAuthenticator(p.getProperty("userName"),p.getProperty("password"));
				//根据邮件会话属性和密码验证器构造一个发送邮件的session 
				Properties ps = new Properties();
				ps.put("mail.smtp.host", p.getProperty("mail.smtp.host"));
				ps.put("mail.smtp.auth", p.getProperty("mail.smtp.auth"));
				Session session = Session.getDefaultInstance(ps, null);
				
				//根据session创建消息
				Message msg = new MimeMessage(session);
				//创建发送地址
				Address from = new InternetAddress(p.getProperty("userName"));
				//为消息设置发送者
				msg.setFrom(from);
				//创建接收地址
				InternetAddress[] addresses = new InternetAddress().parse(mailBean.getDestinationAddress());
				//为消息设置接收地址
				msg.setRecipients(Message.RecipientType.TO, addresses);
				//设置发送日期
				msg.setSentDate(new Date());
				//设置消息主题
				msg.setSubject(mailBean.getTheme());
				//设置文本内容
				msg.setText(mailBean.getMessageBody());
				// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象    
				Multipart   mp   =   new   MimeMultipart();  
				
				//附件处理
				if(mailBean.getAttchmentPath()!= null)
				{
					for(int i=0 ; i < mailBean.getAttchmentPath().length ; i++ )
					{
						BodyPart mbp1 = new MimeBodyPart();
						FileDataSource fds = new FileDataSource(mailBean.getAttchmentPath()[i]);
						String lstSttachFileName[] = mailBean.getAttchmentPath()[i].split("\\.");
						
						String attachFileName = lstSttachFileName[lstSttachFileName.length-1];
						mbp1.setDataHandler(new DataHandler(fds));
						//设置附件名称
						mbp1.setFileName(MimeUtility.encodeText(attachFileName, "utf-8", null));
						mp.addBodyPart(mbp1);
					}
				}
				//将MiniMultipart设置为邮件内容
				msg.setContent(mp);
				//邮件服务器验证
				Transport trans = session.getTransport("smtp");
				trans.connect(p.getProperty("mail.smtp.host"),p.getProperty("userName"),p.getProperty("password"));
				//发送邮件
				trans.sendMessage(msg,msg.getAllRecipients());
				System.out.println("发送成功");
				trans.close();
				return "ok";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "error";
			}

		}
		
		public String sendCureTextMail(Mail mailBean){
			InputStream in;
			try {
				in = new BufferedInputStream(new FileInputStream(EmailUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath().concat("mail.properties")));
				//in = new BufferedInputStream(new FileInputStream("D:\\mail.properties"));
				
				Properties p = new Properties();
				p.load(in);

				//MyAuthenticator authen = new MyAuthenticator(p.getProperty("userName"),p.getProperty("password"));
				//根据邮件会话属性和密码验证器构造一个发送邮件的session 
				Properties ps = new Properties();
				ps.put("mail.smtp.host", p.getProperty("mail.smtp.host"));
				ps.put("mail.smtp.auth", p.getProperty("mail.smtp.auth"));
				Session session = Session.getDefaultInstance(ps, null);
				
				//根据session创建消息
				Message msg = new MimeMessage(session);
				//创建发送地址
				Address from = new InternetAddress(p.getProperty("userName"));
				//为消息设置发送者
				msg.setFrom(from);
				//创建接收地址
				InternetAddress[] addresses = new InternetAddress().parse(mailBean.getDestinationAddress());
				//为消息设置接收地址
				msg.setRecipients(Message.RecipientType.TO, addresses);
				//设置发送日期
				msg.setSentDate(new Date());
				//设置消息主题
				msg.setSubject(mailBean.getTheme());
				//设置文本内容
				msg.setText(mailBean.getMessageBody());

				//邮件服务器验证
				Transport trans = session.getTransport("smtp");
				trans.connect(p.getProperty("mail.smtp.host"),p.getProperty("userName"),p.getProperty("password"));
				//发送邮件
				trans.sendMessage(msg,msg.getAllRecipients());
				trans.close();
				System.out.println("发送成功");
				return "ok";
			} catch (Exception e) {
				e.printStackTrace();
				return "error";
			}

		}

 邮件配置放在SRC下面

mail.smtp.host=smtp.exmail.qq.com
mail.smtp.auth=true
userName=service@xxxx.com
password=密码

 

 

发送时候 如果遇到javax.mail.NoSuchProviderException: smtp

则加入mail.jar 解决

 

 

如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
原因是jar包版本不统一,解决方法如下:

删除Java EE 5 Libraries/javaee.jar/mail里的包有东西.

具体方法如下:
用rar打开X:/Program Files/MyEclipse 6.0/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710/data/libraryset/EE_5/javaee.jar
,然后删除mail,一切就ok了.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值