CSDN博客

img flyforlove

javamail中使用发信身份验证

发表于2004/11/4 10:59:00  837人阅读

1.下载javamail.jar和JAF框架(处理多种格式)
    javamail: http://java.sun.com/products/javamail/
    jaf:  http://java.sun.com/products/javabeans/glasgow/jaf.html
2.用你现有的免费邮箱的邮件服务器(需要身份认证)
3.SendEcard.java 发送mail(带附件)的javabean
4.Email_ca.java    身份认证的javabean

package netcard;
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;
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