论坛首页 Java企业应用论坛

关于发送邮件问题

浏览 4371 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2016-06-16   最后修改:2016-06-16

以下程序在往163.com的邮箱发邮件时,报 Exception in thread "main" java.net.UnknownHostException: mailhost。
请大侠看看问题

public class SendMail {
	public static void main(String[] args) throws IOException {
//		if (args.length == 1)
//			System.getProperties().put("mail.host", args[0]);
		
		Properties props = System.getProperties();
		props.put("mail.smtp.host", "smtp.163.com");
		props.put("mail.smtp.port", "110");
		props.put("mail.smtp.auth", "true");
		
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.print("From: ");
		String from = in.readLine();
		System.out.print("To: ");
		String to = in.readLine();
		System.out.print("Subject: ");
		String subject = in.readLine();
		
		URL url = new URL("mailto:" + to);
		URLConnection c = url.openConnection();
		c.setDoInput(false);
		c.setDoOutput(true);
		c.connect();
		System.out.println("Connecting......");
		PrintWriter out = new PrintWriter(c.getOutputStream());
		
		out.print("From: \"" + from + "\" <" + System.getProperty("user.name") + "@" + 
		           InetAddress.getLocalHost().getHostName() + ">\r\n");
		out.print("To: " + to + "\r\n");
		out.print("Subject: " + subject + "\r\n");
		out.print("\r\n");
		
		System.out.println("Enter the mail message. End with a '.' on a line by itself.");
		String line;
		for(;;) {
			line = in.readLine();
			if (line == null || line.equals(".")) break;
			out.print(line);
		}
		
		out.close();
		in.close();
	}
}

 

   发表时间:2016-06-19  
这要都能发送成功,那163邮箱早蹦了。
发邮件最简单还是用现成的类库吧,不想用的话就先研究下源码再说。
0 请登录后投票
   发表时间:2016-06-21  
端口错了 smtp 默认是25
0 请登录后投票
   发表时间:2016-07-04  
端口25也不行的
0 请登录后投票
   发表时间:2016-07-06  
戎马西部 写道
端口25也不行的

https://github.com/redkale/redkale-plugins/tree/master/src/org/redkalex/email 这里有发邮箱功能,可以参考下
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics