用javamail发邮件,但是为什么会出错呢?有谁能不能把你们写邮件的代码贴出来呢?还有就是邮件群发的问题,不知道该怎么办(50分)

  • 主题发起人 主题发起人 ★蓝天★
  • 开始时间 开始时间

★蓝天★

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用javamail发邮件,应该也已经配置好了,从书上抄了一锻,但是为什么会出错呢?有谁能不能把你们写邮件的代码贴出来呢?还有就是邮件群发的问题,不知道该怎么办啊?
<%@ page import="java.text.*"%>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
Properties props=new Properties();
props.put("mail.smtp.host","smtp.citiz.net");
Session s=Session.getInstance(props);
MimeMessage message=new MimeMessage(s);
InternetAddress from=new InternetAddress("aaa@citiz.net");
message.setFrom(from);
InternetAddress to=new InternetAddress("bbb@citiz.net");
message.addRecipient(Message.RecipientType.To,to);//这一句有错,他说RecipientType没有方法To,
message.setSubject("Test from JavaMail.");
message.setText("hello from javamail");
Store store=s.getStore("pop3");
store.connect("pop.citiz.net","aaa","password");
Transport.send(message);
store.close();

%>
 
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
 
-------
这是一个发邮件的javabean 你看看就知道了
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class mailBean {
private String host="132.147.0.254";
private String from;
private String subject;
private String body;
private String to;
public void setHost(String host) {
this.host = host;
}
public String getHost() {
return host;
}
public void setFrom(String from) {
this.from = from;
}
public String getFrom() {
return from;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getSubject() {
return subject;
}
public void setBody(String body) {
this.body = body;
}
public String getBody() {
return body;
}
public void setTo(String to) {
this.to = to;
}
public String getTo() {
return to;
}
public void send(){
boolean sessionDebug =false;
Properties props=System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.transport.protocal","smtp");
Session session=Session.getDefaultInstance(props,null);
session.setDebug(sessionDebug);
try
{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport.send(msg);
}
catch(MessagingException e)
{
e.printStackTrace();
}
}
}
 
后退
顶部