栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

发送邮件到Gmail帐户

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

发送邮件到Gmail帐户

这是一个很好的例子:

public class SMTPDemo {  public static void main(String args[]) throws IOException,      UnknownHostException {    String msgFile = "file.txt";    String from = "java2s@java2s.com";    String to = "yourEmail@yourServer.com";    String mailHost = "yourHost";    SMTP mail = new SMTP(mailHost);    if (mail != null) {      if (mail.send(new FileReader(msgFile), from, to)) {        System.out.println("Mail sent.");      } else {        System.out.println("Connect to SMTP server failed!");      }    }    System.out.println("Done.");  }  static class SMTP {    private final static int SMTP_PORT = 25;    InetAddress mailHost;    InetAddress localhost;    BufferedReader in;    PrintWriter out;    public SMTP(String host) throws UnknownHostException {      mailHost = InetAddress.getByName(host);      localhost = InetAddress.getLocalHost();      System.out.println("mailhost = " + mailHost);      System.out.println("localhost= " + localhost);      System.out.println("SMTP constructor donen");    }    public boolean send(FileReader msgFileReader, String from, String to)        throws IOException {      Socket smtpPipe;      InputStream inn;      OutputStream outt;      BufferedReader msg;      msg = new BufferedReader(msgFileReader);      smtpPipe = new Socket(mailHost, SMTP_PORT);      if (smtpPipe == null) {        return false;      }      inn = smtpPipe.getInputStream();      outt = smtpPipe.getOutputStream();      in = new BufferedReader(new InputStreamReader(inn));      out = new PrintWriter(new OutputStreamWriter(outt), true);      if (inn == null || outt == null) {        System.out.println("Failed to open streams to socket.");        return false;      }      String initialID = in.readLine();      System.out.println(initialID);      System.out.println("HELO " + localhost.getHostName());      out.println("HELO " + localhost.getHostName());      String welcome = in.readLine();      System.out.println(welcome);      System.out.println("MAIL From:<" + from + ">");      out.println("MAIL From:<" + from + ">");      String senderOK = in.readLine();      System.out.println(senderOK);      System.out.println("RCPT TO:<" + to + ">");      out.println("RCPT TO:<" + to + ">");      String recipientOK = in.readLine();      System.out.println(recipientOK);      System.out.println("DATA");      out.println("DATA");      String line;      while ((line = msg.readLine()) != null) {        out.println(line);      }      System.out.println(".");      out.println(".");      String acceptedOK = in.readLine();      System.out.println(acceptedOK);      System.out.println("QUIT");      out.println("QUIT");      return true;    }  }}

-> http://www.java2s.com/Code/Java/Network-Protocol/SendingMailUsingSockets.htm



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号