mail_host指邮件主机.不需要你的机器上配置什么东西,只要有tcpip协议就可以了.
举过例子,[email protected]是你的邮箱,别人要发信给你,就必须先发到你的邮件主机机上,fun.21cn.com的邮件主机是mta.21cn.com,在NT或2000下可以通过
nslookup -type=mx fun.21cn.com 查到.
举过例子,[email protected]是你的邮箱,别人要发信给你,就必须先发到你的邮件主机机上,fun.21cn.com的邮件主机是mta.21cn.com,在NT或2000下可以通过
nslookup -type=mx fun.21cn.com 查到.
用以下的代码就可以发邮件了import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class SendMessage {// Collect the necessary information to send a simple message
// Make sure to replace the values for host, to, and from with
// valid information.
// host - must be a valid smtp server that you currently have
// access to.
// to - whoever is going to get your email
// from - whoever you want to be. Just remember that many smtp
// servers will validate the domain of the from address
// before allowing the mail to be sent. private String host;
private String from;
private String to;
private String subject;
private String messagetext;
private String transportprotocol; public SendMessage() {
} public void setHost(String shost){
host = shost;
} public void setTo(String sto){
to = sto;
} public void setFrom(String sfrom){
from = sfrom;
} public void setSubject(String ssubject){
subject = ssubject;
} public void setMessagetext(String smessage){
messagetext = smessage;
} public void setTransportprotocol(String stp){
transportprotocol = stp;
} public boolean Send() {
boolean brt = false; Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", transportprotocol); Session session = Session.getDefaultInstance(props, null);
try {
//Instantiate a new MimeMessage and fill it with the
//required information.
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(messagetext); // Hand the message to the default transport service
// for delivery.
Transport.send(msg);
brt = true;
}
catch (MessagingException e) {
e.printStackTrace();
} return brt; }}