注册 登录
编程论坛 J2EE论坛

求助,我的程序为什么会错

xwf106 发布于 2007-01-06 22:06, 555 次点击
import java.io.*;
import java.net.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class J_mail {
public static void main(String[] args)
{
String host=new String("SMTP.163.com");
String from=new String("xwf310@163.com");
String to=new String("xwf310@163.com");
String cc=null;
String subject=new String("163");
String msg=new String("SMTP.163.com");
try
{
Properties props=System.getProperties();
props.put("mail.smtp.host", "host");
Session m_session =Session.getDefaultInstance(props, null);
//创建Message对象
Message m=new MimeMessage(m_session);
//InternetAddress[] iAddr=null;
//设置发送人
m.setFrom(new InternetAddress(from));
//设置接受人
m.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
//抄送地址
if((cc!=null)&&(cc.trim().length()>0))
{
m.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc,false));
}
//设置主题
m.setSubject(subject);
//设置消息内容
m.setContent(msg,"text/plain");
//发送时间
m.setSentDate(new Date());
//发送信息
Transport.send(m);
System.out.println("发送成功");
}
catch(Exception ex)
{
System.out.println("发送失败");
}
}
}
5 回复
#2
无理取闹2007-01-06 22:57
#3
千里冰封2007-01-07 09:31

[CODE]/*
* Send.java
*
* Created on 2006年11月27日, 上午9:36
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package lbf.email;
/**
*
* @author lbf
*/
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import javax.mail.util.*;
import javax.mail.event.*;
public class Send {
private String content,subject;//表示发送内容
private String userName,pwd;//表示发送邮箱的用户名密码
private Session s;//一个发送邮件的会话
private MimeMessage mm;//网际网络邮件协议
private List<String> to;//发往的地址

/**
* Creates a new instance of Send
*/
public Send(Properties pro,String from,String userName,String pwd,String subject,String content) {
s=Session.getInstance(pro);
s.setDebug(true);
mm=new MimeMessage(s);
to=new Vector<String>();
this.userName=userName;
this.pwd=pwd;
this.subject=subject;
this.content=content;
try{
mm.setFrom(new InternetAddress(from));
}
catch(Exception exe){
exe.printStackTrace();
}
}
//把地址加入到要发送者的名单中
public void addAddress(String address){
to.add(address);
}
//把一大串地址加入到要发送者名单中
public void addAddress(List<String> ls){
to.addAll(ls);
}
//发送邮件
public void send()throws Exception{
mm.setSubject(subject);
mm.setText(content);
mm.setSentDate(new Date());
mm.saveChanges();
Transport t=s.getTransport("smtp");
t.connect(userName,pwd);
for(String s:to){
mm.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(s));
t.sendMessage(mm,mm.getAllRecipients());
}
t.close();
System.out.println("所有信件发送成功");
}
public static void main(String[] args)throws Exception {
System.out.println("开始运行!");
Properties props=System.getProperties();
props.put("mail.smtp.host","smtp.sohu.com");
props.put("mail.smtp.auth","true");
Send s=new Send(props,"libinfeng1982@sohu.com","libinfeng1982","密码","测试的一封信","这是测试的内容,有中文有英文,English");
s.addAddress("libinfeng1982@sohu.com");
s.addAddress("hades_lbf@hotmail.com");
s.addAddress("hades_lbf@163.com");
s.addAddress("24325142@qq.com");
s.send();
}

}[/CODE]

以前写的一个发送E-MAIL的,你可以参考一下

#4
禹_二2007-01-07 13:02
开发这种mail需要先下载那些包!!
#5
xwf1062007-04-22 18:24
#6
zplove2007-04-29 17:39

我也写得jsp 得
<%

try{
//从html表单中接收邮件信息
String to_mail=email;
String to_title="title";
String to_content="content";

//建立邮件会话
Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put("mail.smtp.host","smtp.sina.com");//存储发送邮件服务器的信息
props.put("mail.smtp.auth","true");//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);

//由邮件会话新建一个消息对象
MimeMessage message=new MimeMessage(s);//由邮件会话新建一个消息对象

//设置邮件
InternetAddress from=new InternetAddress("emailname@sina.com");
message.setFrom(from);//设置发件人
InternetAddress to=new InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(to_title);//设置主题
message.setText(to_content);//设置信件内容
message.setSentDate(new Date());//设置发信时间

//发送邮件
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport("smtp");
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect("smtp.sina.com","emailname@sina.com ","password");
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close()
}catch(MessagingException e){
out.println("发送失败!"+e);
}

%>

1