JavaWeb发送邮件(不包含附件)

使用该功能之前要先打开邮箱的pop3/smtp功能,具体方法自己去百度。
1.使用的jar包
点击下载
2.后端代码

	public static void sendCode(String e_mail) {e_mail需要发送的邮箱地址
		Properties pro = new Properties();
	      pro.setProperty("mail.host","smtp.qq.com");//qq邮箱为smtp.qq.com 163邮箱为smtp.163.com
	      pro.setProperty("mail.transport.protocol","smtp");
	      pro.setProperty("mail.smtp.auth","true");
          Session session = Session.getInstance(pro);
//          session.setDebug(true);//开启Debug,可以看到发送邮件信息
          Transport ts=null;
          try {
			 ts = session.getTransport();
			 ts.connect(“smtp.xx.com”,端口号(qq邮箱一般为587,163邮箱为25),“邮箱账号”,“授权码”);
			 Message message = creatEmail(session,e_mail);
			 ts.sendMessage(message, message.getAllRecipients());//发送邮件
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			SendCodeEmail.setMessage("邮件发送失败!");
			e.printStackTrace();
		}finally {
			try {
				if(ts!=null)ts.close();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
          
		
	}
	private static MimeMessage creatEmail(Session sion,String e_mail) {//创建邮件
		MimeMessage message = new MimeMessage(sion);
		try {
			message.setFrom(new InternetAddress(从哪里发送的地址(xxx@qq.com)));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(e_mail));//发送到哪里
			message.setSubject(“主题”);//设置主题
			message.setContent(“内容”,"text/html;charset=UTF-8");//设置内容
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return message;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值