为了运行javamail。我们需要在classpath里导入两个包:mail.jar和 activation.jar,这是在纯DOS里或者其它简单工具使用时才需要这个classpath。如果使用像eclipes这样的工具就不需要配置。因为myeclipes已经帮你做好了。
为了要发送邮件和接收邮件,我们必须要遵守smtp和pop3协议,不过还有其它协议也可执行(如IMAP)。如果把本机当作服务器来发送邮件请在http://jakrata.apache.org网站里下载wang@126.com”)
或者是:InterntAddress addr=new InternetAddress(“wang@126.com”)
另外,InternetAddress类还提供了地址解析方法如
Address[] addrs=InternetAddress.parse(“wang@163.com,f@126.com,zuo@126.com”);
地址之后就可以发送消息了。
//设置发送者
Msg.setFrom(new InternetAddress(“test@126.com”));
//设置接收者,并还具有解析功能
Msg.setRecipients(Message.RecipientType.To,InternetAddress.parse(“zuolin0806@163.com”));
//抄送的接收者
Msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(“zuolin0806@163.com”));
//暗送的接收者
Msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(“zuolin0806@163.com”));
//设置消息主题
msg.setSubject(“我的爱”)
//设置内容的基本机制,其参数Mime类型
msg.setContent(“这是我的内容”,”text/html;charset=gb2312”)
其中的text/html表示消息内容采用的是HTML格式。如果消息格式是(text/plain),而且使用的是MimeMessage,那么setText()方法设置邮件内容的参数(Mime类型默认为text/plain)
//设置邮件内容
msg.setText();
//设置发送时间
msg.setSentDate(new java.util.Date());
最后一步是就是发送了
javax.mail.Transport
该类也是抽象类,它可通过静态方法和实便方法发送消息。Transport断承service类。所以它提供了通用方法,如命名传输,连接服务器和监听传输事件等。
//默认
Transport.send(msg);
最后看一下使用外部邮件服务器的使用方法。先看程序再讲解,注意在使用外部邮件服务器时一定要有一个类来认证。到时在讲。
public class MySecondMail
{
protected Session session = null;
//邮件用户名
String mailUser = "zuolin0806";
String host = "smtp.163.com";
String pwd = "用户名的密码";
public MySecondMail()
{
init();
}
public void init()
{
Properties props = new Properties();
props.put("mail.transpost.protocol", "smtp");
props.put("mail.smtp.host", "smtp.163.com");
//必须要有一个类来认证
props.put("mail.smtp.auth", "true");
props.put("mail.smpt.port", "25")
Email_Autherticatorbean auth = new Email_Autherticatorbean(mailUser,pwd);
//session认证
session = Session.getInstance(props,auth);
//这个是跟踪后台消息。打印在控制台
session.setDebug(true);
}
public static void main(String[] args)
{
new MySecondMail().sendMails();
System.out.println("send mail success!");
}
public void sendMails()
{
try
{
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("zuolin0806@163.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("javaboy@126.com"));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse("javaboy@126.com"));
msg.setSentDate(new Date());
msg.setSubject("this is my Threee mail");
msg.setContent("this is my mail","text/html");
msg.setText("我成功了。哈哈");
Transport transport = session.getTransport("smtp");
与发送者的邮箱相连
transport.connect(host,mailUser,pwd);
transport.send(msg);
}
catch (SendFailedException e)
{
e.printStackTrace();
}
catch (Exception ee)
{
ee.printStackTrace();
}
}
}
认证类必须继承Authenticator 例子如下:
imp
public class Email_Autherticatorbean extends Authenticator
{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}