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

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

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

测试代码

 

 

public static void main(String[] args){
      Mail sendmail = new Mail();
      sendmail.setHost("smtp.163.com");
      sendmail.setUserName("aaaaaaaaa@163.com");
      sendmail.setPassWord("aaaaaaaaa");
      sendmail.setTo("aaaaaaaaa@163.com");
      sendmail.setFrom("aaaaaaaaa@163.com");
      sendmail.setSubject("你好,这是测试!");
      sendmail.setContent("你好这是一个带多附件的测试!");    
      //Mail sendmail = new Mail("aaaaaaaaa@163.com","aaaaaaaaa@163.com","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)

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值