CSDN博客

img arielxp

带有身份验证的JAVAMAIL编程

发表于2004/9/28 15:17:00  2159人阅读

       如果你认为没有必要设置MAIL服务器,而又要用JAVAMAIL的邮件发送功能,那最好的方式就是用自己的免费邮箱服务器!可这些服务器登陆必须要进行验证,所以需要进行带有身份验证的JAVAMAIL编程!
说明:
1.下载javamail.jar和JAF框架(处理多种格式)
2.用你现有的免费邮箱的邮件服务器(需要身份认证)
3.SendEcard.java 发送mail(带附件)的javabean
4.Email_ca.java    身份认证的javabean

package netcard;

/**
*SendEcard.java
*发送mail的javabean
*/

import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEcard {

  private String tomail;
  private String frommail;
  private String subject;
  private String content;
  private String imagecard;
  String smtp="****";//设置邮件服务器

  public SendEcard() {
  }
  public String getTomail() {
    return tomail;
  }
  public void setTomail(String tomail) {
    this.tomail = tomail;
  }
  public String getFrommail() {
    return frommail;
  }
  public void setFrommail(String frommail) {
    this.frommail = frommail;
  }
  public String getSubject() {
    return subject;
  }
  public void setSubject(String subject) {
    this.subject = subject;
  }
  public String getContent() {
    return content;
  }
  public void setContent(String content) {
    this.content = content;
  }
  public String getImagecard() {
    return imagecard;
  }
  public void setImagecard(String imagecard) {
    this.imagecard = imagecard;
  }

  public boolean sendMail(){
   try{
     Properties p=System.getProperties();
     p.put("mail.smtp.host",this.smtp);
     p.put("mail.smtp.auth","true");  //设置为须验证的模式
     Session session=Session.getDefaultInstance(p,new Email_ca("用户名","密码"));
     MimeMessage msg=new MimeMessage(session);
     msg.setSentDate(new Date());
     InternetAddress from=new InternetAddress(frommail);
     msg.setFrom(from);
     InternetAddress[] address = {
         new InternetAddress(tomail)};
     msg.setRecipients(Message.RecipientType.TO, address);
     msg.setSubject(this.subject);
     msg.setText(this.content);
     Multipart mp = new MimeMultipart();
     MimeBodyPart mbp=new MimeBodyPart();
     FileDataSource fds=new FileDataSource(文件路径);
     //得到附件本身并至入BodyPart
     mbp.setDataHandler(new DataHandler(fds));
     //得到文件名同样至入BodyPart
     mbp.setFileName(fds.getName());
     mp.addBodyPart(mbp);
     msg.setContent(mp);
     Transport.send(msg);
     return true;
   }catch(AddressException addr_e){
     System.out.println(addr_e.getMessage());
     return false;
   }catch(MessagingException msg_e){
     System.out.println(msg_e.getMessage());
     return false;
   }
}

}

package netcard;

/**
*Email_ca.java
*身份认证的javabean
*/


import javax.mail.*;

public class Email_ca extends javax.mail.Authenticator{
  private String user=null;
  private String pwd=null;
  public Email_ca(){
    super();
  }
  public void setUser(String user){
    this.user=user;
  }
  public void setPwd(String pwd){
    this.pwd=pwd;
  }
  public Email_ca(String user,String pwd){
    super();
    setUser(user);
    setPwd(pwd);
  }
  public PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication(user,pwd);
  }
}

     至此你就可以利用免费邮箱服务器进行邮件发送!

阅读全文
0 0

相关文章推荐

img
取 消
img