java发送邮件

  • Post author:
  • Post category:java


闲来无事,写个java发送邮件玩玩。核心代码如下:
private boolean mailSend (String fromMail,String toMail,String subject,String strContext){
		  try {
			  Properties props = new Properties();
			  props.put("mail.transport.protocol","smtp");
			  props.put("mail.smtp.host", smtpServer); //
			  props.put("mail.smtp.auth", "true"); //认证是否设置
			  props.put("mail.host",smtpServer);
			  //建立会话并且认证
			  Authenticator auth1 = new Authenticator() {//封装用户名 密码
				  public PasswordAuthentication getPasswordAuthentication() {
					  return new PasswordAuthentication(username,psw);
				  }
			  };
			  javax.mail.Session session =javax.mail.Session.getInstance(props,auth1);
			  Message msg = new MimeMessage(session);
			  //发送源地址
			  msg.setFrom(new InternetAddress(fromMail));
			  //发送目的地址
			  ArrayList<String> altMail = this.parseMailAddress(toMail



版权声明:本文为seu2hit原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。