java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件

java使用javax.mail包发送电子邮件。这个实例可发送多附件。

这里使用163邮箱进行测试。可以设置账号、密码、主题、文本内容、附件

测试代码

 

 

public static void main(String[] args){
      Mail sendmail = new Mail();
      sendmail.setHost("smtp.163.com");
      sendmail.setUserName("[email protected]");
      sendmail.setPassWord("aaaaaaaaa");
      sendmail.setTo("[email protected]");
      sendmail.setFrom("[email protected]");
      sendmail.setSubject("你好,这是测试!");
      sendmail.setContent("你好这是一个带多附件的测试!");    
      //Mail sendmail = new Mail("[email protected]","[email protected]","smtp.163.com","aaaaaaaaa","aaaaaaaaa","你好","胃,你好吗?");
      sendmail.attachfile("D:\\test.jpg");
      sendmail.attachfile("D:\\test.mp4");
      sendmail.sendMail();

     }

邮件发送类的实现

package com.lp.app.net;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


String to = "";//收件人
String from = "";//发件人
String host = "";//smtp主机
String username = "" ;
String password = "" ;
String filename = "";//附件文件名
String subject = "";//邮件主题
String content = "";//邮件正文
Vector file = new Vector();//附件文件集合

public Mail(){
}
//构造器,提供直接的参数传入
public Mail(String to,String from,String smtpServer,String username,String password,String subject,String content){
  this.to = to;
  this.from = from;
  this.host = smtpServer;
  this.username = username;
  this.password = password;
  this.subject = subject;
  this.content = content;
}
//设置邮件服务器地址
public void setHost(String host){
  this.host = host;
}
//设置登录服务器校验密码
public void setPassWord(String pwd){
  this.password = pwd;
}
//设置登录服务器校验用户
public void setUserName(String usn){
  this.username = usn;
}
//设置邮件发送目的邮箱
public void setTo(String to){
  this.to = to;
}
//设置邮件发送源邮箱
public void setFrom(String from){
  this.from = from;
}
//设置邮件主题
public void setSubject(String subject){
  this.subject = subject;
}
//设置邮件内容
public void setContent(String content){
  this.content = content;
}
//把主题转换为中文
public String transferChinese(String strText){
  try{
    strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
  }catch(Exception e){
    e.printStackTrace();
  }
  return strText;
}
//往附件组合中添加附件
public void attachfile(String fname){
  file.addElement(fname);
}
//发送邮件
public boolean sendMail(){

  //构造mail session
  Properties props = System.getProperties();
  props.put("mail.smtp.host",host);
  props.put("mail.smtp.auth","true");
  Session session=Session.getDefaultInstance(props, new Authenticator(){
   public PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication(username,password); 
   }
  });

  try {
    //构造MimeMessage 并设定基本的值
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address={new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO,address);
    subject = transferChinese(subject);
    msg.setSubject(subject);

    //构造Multipart
    Multipart mp = new MimeMultipart();

    //向Multipart添加正文
    MimeBodyPart mbpContent = new MimeBodyPart();
    mbpContent.setText(content);
    //向MimeMessage添加(Multipart代表正文)
    mp.addBodyPart(mbpContent);

    //向Multipart添加附件
    Enumeration efile=file.elements();
    while(efile.hasMoreElements()){

      MimeBodyPart mbpFile = new MimeBodyPart();
      filename=efile.nextElement().toString();
      FileDataSource fds = new FileDataSource(filename);
      mbpFile.setDataHandler(new DataHandler(fds));
      mbpFile.setFileName(fds.getName());
      //向MimeMessage添加(Multipart代表附件)
      mp.addBodyPart(mbpFile);

    }

    file.removeAllElements();
    //向Multipart添加MimeMessage
    msg.setContent(mp);
    msg.setSentDate(new Date());
    //发送邮件
    Transport.send(msg);

  } catch (MessagingException mex) {
    mex.printStackTrace();
    Exception ex = null;
    if ((ex=mex.getNextException())!=null){
      ex.printStackTrace();
    }
    return false;
  }
  return true;
 }

}

本文只试验了163邮件,读者可以按下面的主机地址,试验其他邮箱。但并不是每种邮箱都能成功。

gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)

21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)

sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)

tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)

163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)

263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)

yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com

263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)

Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)

sinaVIP
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)

sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)

etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com

x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)

yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587)
雅虎邮箱POP3的SSL不启用端口为110,POP3的SSL启用端口995;SMTP的SSL不启用端口为25,SMTP的SSL启用端口为465

QQ邮箱 QQ企业邮箱
POP3服务器地址:pop.qq.com(端口:110) POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.qq.com (端口:25) SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
SMTP服务器需要身份验证

126邮箱 HotMail
POP3服务器地址:pop.126.com(端口:110) POP3服务器地址:pop.live.com (端口:995)
SMTP服务器地址:smtp.126.com(端口:25) SMTP服务器地址:smtp.live.com (端口:587)

china.com: 139邮箱
POP3服务器地址:pop.china.com(端口:110) POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25) SMTP服务器地址:SMTP.139.com(端口:25)

 

你可能感兴趣的:(java使用javax.mail包发送电子邮件:设置账号、密码、主题、文本、附件)