发送邮件

package com.email.send;

import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;

/**//**
 * @author Bromon
 */
public class SenderWithSMTPVer
{
 String host="";
 String user="";
 String password="";

 public void setHost(String host)
 {
  this.host=host;
 }

 public void setAccount(String user,String password)
 {
  this.user=user;
  this.password=password;
 }

 public void send(String from,String to,String subject,String content)
 {
  Properties props = new Properties();
  props.put("mail.smtp.host", host);//指定SMTP服务器
  props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
  try
  {
   Session mailSession = Session.getDefaultInstance(props);

   mailSession.setDebug(true);//是否在控制台显示debug信息

   Message message=new MimeMessage(mailSession);
   message.setFrom(new InternetAddress(from));//发件人
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//收件人

   message.setSubject(subject);//邮件主题
   message.setText(content);//邮件内容
   message.saveChanges();

   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, user, password);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
  }catch(Exception e)
  {
   System.out.println(e);
  }

 }

 public static void main(String args[])
 {
  SenderWithSMTPVer sm=new SenderWithSMTPVer();

  sm.setHost("smtp.sina.com");//指定要使用的邮件服务器
  sm.setAccount("","");//指定帐号和密码

  /**//*
   * @param String 发件人的地址
   * @param String 收件人地址
   * @param String 邮件标题
   * @param String 邮件正文
   */
  sm.send("","","测试发送","新邮件");
 }

}

package com.email.send;

import java.net.*;
import java.io.*;

public class Service {
 public static void main(String args[])
 {
  String ip1, ip2;
  try
  {
   InetAddress in = InetAddress.getLocalHost();
   InetAddress[] all = InetAddress.getAllByName(in.getHostName());
   ip2 = ReadFile();
   //通过本机主机名,遍历多个ip  
   for (int i = 0; i < all.length; i++)
   {
    String tmp=null;
    tmp=ip1=all[i].getHostAddress().toString();
    System.out.println("IP =  " +tmp);//输出计算机所有的ip地址
    if (isInnerIP(ipToLong(tmp)) == -1)//检查是不是外网ip,如果是就保存文件
    {
     if(!ip2.equals(ip1)){
     //发送邮件
     SenderWithSMTPVer sm=new SenderWithSMTPVer();

     sm.setHost("smtp.sina.com");//指定要使用的邮件服务器
     sm.setAccount("taingong2007","taingong2007");//指定帐号和密码

     
      /* @param String 发件人的地址
      * @param String 收件人地址
      * @param String 邮件标题
      * @param String 邮件正文
      */
     
     sm.send("taingong2007@sina.com","taingong2008@sina.com",ip1,"");
     }
     //     将新的ip写入.txt文件中
     WriteFile(ip1);//将ip地址写入文件
     System.out.println("IP保存在同目录IP.txt文件中");
     System.out.println("同目录IP.txt文件中的IP是:" + ReadFile());
    }
   }
  }
  catch (UnknownHostException e)
  {
   System.out.println(e.getMessage());
  }
 }

    public static int isInnerIP(long a_ip)//检查ip地址是否是内网ip
 {
  int bValid = -1;
  if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
  {
   bValid = 0;
  }
  return bValid;
 }
 //将127.0.0.1 形式的IP地址转换成10进制整数,这里没有进行任何错误处理
 public static long ipToLong(String strIP)
 {
  long[] ip = new long[4];
  //先找到IP地址字符串中.的位置
  int position1 = strIP.indexOf(".");
  int position2 = strIP.indexOf(".", position1 + 1);
  int position3 = strIP.indexOf(".", position2 + 1);
  //将每个.之间的字符串转换成整型
  ip[0] = Long.parseLong(strIP.substring(0, position1));
  ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
  ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
  ip[3] = Long.parseLong(strIP.substring(position3 + 1));
  return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
 }

  //写文件 
 public static void WriteFile(String str)
 {
  String filename = "IP.txt";
  File write = new File(filename);
  BufferedWriter bw;
  if (write.exists())
  {
   write.delete();
  }
  else
  //如果文件不存,则建立  
  {
   try
   {
   write.createNewFile();
   }
   catch (IOException e)
   {
    System.out.println(e.getMessage());
   }
  }
  
  try
  {
    bw = new BufferedWriter(new FileWriter(write));
    bw.write(str);
    bw.close();
  }
  catch (IOException e)
  {
   System.out.println(e.getMessage());
  }

 }
  //读文件
 public static String ReadFile()
 {
  String filename = "IP.txt";
  File read = new File(filename);
  BufferedReader br;
  String temp = null;
  if (read.exists())
  {
   
   try
   {
    br = new BufferedReader(new FileReader(read));
    temp = br.readLine();
    br.close();
    System.out.println(temp);
   }
   catch (IOException e)
   {
    System.out.println(e.getMessage());
   }   
  }
  else
      {temp = "";}
  return temp;
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值