论坛首页 Java企业应用论坛

谁有java做的发送email的例子吗?

浏览 4873 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2006-12-11  
想做一个java发送email的程序,弄了半天都没发出去,谁能给我个例子吗?jwfdp@yahoo.com.cn
   发表时间:2006-12-11  
package com;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*;
import java.net.*;
public class sendMail
{
static PrintStream ps = null;
static DataInputStream dis = null;

public static void send(String str) throws IOException
{
ps.println(str);
ps.flush();
System.out.println("java sent:"+str);
}

public static void receive() throws IOException
{
String readstr = dis.readLine();
System.out.println("SMTP response:"+readstr);
}

public static void main(String args[])
{
String HELO="HELO";
String MAIL_FROM="jwfdp@yahoo.com";
String RCPT_TO="jwfdp@hotmail.com";
String SUBJECT="SUBJECT:java is cool!";
String DATA   = "DATA";
String BODY   = "Java send this!\r\n.\r\n";
Socket smtp =null;
try
{
smtp=new Socket("smtp.yahoo.com",25);
OutputStream os = smtp.getOutputStream();
ps = new PrintStream(os);
InputStream  is = smtp.getInputStream();
dis = new DataInputStream(is);
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
String loc = InetAddress.getLocalHost().getHostName();
send(HELO+loc);
receive();
send(MAIL_FROM);
receive();
send(RCPT_TO);
receive();
send(DATA);
receive();
send(SUBJECT);
receive();
send(BODY);
receive();
smtp.close();
System.out.println("==end========");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
就是这段代码,为什么smtp=new Socket("smtp.yahoo.com",25);有错呢
0 请登录后投票
   发表时间:2006-12-11  

jakarta commons-emails 很简单:

java 代码
  1. SimpleEmail email = new SimpleEmail();   
  2. email.setHostName("mail.yyy.com");   
  3. email.setAuthentication("username","password")   
  4. email.addTo("aaa@xxx.com");   
  5. email.setFrom("bbb@yyy.cn");   
  6. email.setSubject("主题");   
  7. email.setMsg("内容");   
  8. email.send();  


要发送附件、HTML邮件,看文档。

0 请登录后投票
   发表时间:2006-12-11  
谢谢了哈,怎么我还是发不出去啊
public static void test() throws EmailException
{
SimpleEmail email = new SimpleEmail();   
email.setHostName("mail.yahoo.com");   
email.setAuthentication("username","password");  
email.addTo("wei@movaya.com");
email.setFrom("jwfdp@yahoo.com.cn");   
email.setSubject("主题");   
email.setMsg("内容");   
email.send(); 
}


错误报告:
Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : mail.yahoo.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:873)
at org.apache.commons.mail.Email.send(Email.java:898)
at com.testDate.test(testDate.java:132)
at com.testDate.main(testDate.java:52)
Caused by: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:138)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:863)
... 3 more
0 请登录后投票
   发表时间:2006-12-21  
没人回答啊
0 请登录后投票
论坛首页 Java企业应用版

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