课程咨询 :186 8716 1620      qq:2066486918

昆明Java培训 > 达内新闻 > JAVA邮件发送
  • JAVA邮件发送

    发布:昆明Java培训      来源:达内新闻      时间:2016-09-30

  • 邮件发送怎么发?昆明Java培训机构的老师教你怎么发送。

    1.写一个MailSendProper类封装需要的属性

    import java.util.Properties;

    public class MailSendProper {

    private String Host ;                //发送邮件服务器的ip

    private String Port ;//发送邮件服务器的端口号

    private String SendAddress ;          //邮件发送者的地址

    private String ReceiveAddress ;      //邮件接受者的地址

    private String username ;               //登录发送邮箱的用户名

    private String password ;                //登录发送邮箱的密码

    private boolean isvalidate = true ; //是否需要身份验证

    private String subject ;        //邮件标题

    private String content ; //邮件内容

    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 getHost() {

    return Host;

    }

    public void setHost(String host) {

    Host = host;

    }

    public String getPort() {

    return Port;

    }

    public void setPort(String port) {

    Port = port;

    }

    public String getSendAddress() {

    return SendAddress;

    }

    public void setSendAddress(String sendAddress) {

    SendAddress = sendAddress;

    }

    public String getReceiveAddress() {

    return ReceiveAddress;

    }

    public void setReceiveAddress(String receiveAddress) {

    ReceiveAddress = receiveAddress;

    }

    public String getUsername() {

    return username;

    }

    public void setUsername(String username) {

    this.username = username;

    }

    public String getPassword() {

    return password;

    }

    public void setPassword(String password) {

    this.password = password;

    }

    public boolean isIsvalidate() {

    return isvalidate;

    }

    public void setIsvalidate(boolean isvalidate) {

    this.isvalidate = isvalidate;

    }

    public Properties getProperties(){

    Properties properties = new Properties() ;

    properties.put("mail.smtp.host", this.Host) ;

    properties.put("mail.smtp.port", this.Port) ;

    properties.put("mail.smtp.auth", isvalidate?"true":"false") ;

    return properties ;

    }

    }

    2、写一个 EmailMessage封装发送信息

    public class EmailMessage {

    private String title ;

    private String context ;

    private String toEmail ;

    public EmailMessage() {

    super();

    }

    public EmailMessage(String title, String context, String toEmail) {

    super();

    this.title = title;

    this.context = context;

    this.toEmail = toEmail;

    }

    public String getTitle() {

    return title;

    }

    public void setTitle(String title) {

    this.title = title;

    }

    public String getContext() {

    return context;

    }

    public void setContext(String context) {

    this.context = context;

    }

    public String getToEmail() {

    return toEmail;

    }

    public void setToEmail(String toEmail) {

    this.toEmail = toEmail;

    }

    }

    3、写一个MailAttorney 邮箱密码验证器类

    import javax.mail.Authenticator;

    import javax.mail.PasswordAuthentication;

    public class MailAttorney extends Authenticator {

    private String username ;

    private String password ;

    public MailAttorney(String username,String password) {

    this.username = username ;

    this.password = password ;

    }

    //覆盖父类方法,获取密码认证器

    @Override

    protected PasswordAuthentication getPasswordAuthentication() {

    return new PasswordAuthentication(username,password) ;

    }

    }

    4、写一个MailSend 邮件工具类

    import java.util.Date;

    import javax.mail.Address;

    import javax.mail.Message;

    import javax.mail.Session;

    import javax.mail.Transport;

    import javax.mail.internet.InternetAddress;

    import javax.mail.internet.MimeMessage;

    import com.VTBBS.entity.EmailMessage;

    public class MailSend {

    public static boolean mailTest(MailSendProper mailsender){

    MailAttorney attorney = null ;

    if(mailsender.isIsvalidate()){//判断是否需要身份认证

    attorney = ew MailAttorney(mailsender.getUsername(),mailsender.getPassword()) ;

    }

    //根据邮箱会话属性和密码验证器构造一个发送邮箱的seesion

    Session session = Session.getInstance(mailsender.getProperties(),attorney) ;

    //根据session创建一个邮件消息

    Message mailMessage = new MimeMessage(session) ;

    try {

    //创建邮件发送者的地址

    Address from = new InternetAddress(mailsender.getSendAddress()) ;

    //设置邮箱消息的发送者

    mailMessage.setFrom(from);

    //创建邮件接收这的地址,并设置到邮件消息中

    Address to = new InternetAddress(mailsender.getReceiveAddress()) ;

    mailMessage.setRecipient(Message.RecipientType.TO, to);

    mailMessage.setSubject(mailsender.getSubject());       //设置邮件标题

    mailMessage.setSentDate(new Date());        //设置发送邮件时间

    mailMessage.setText(mailsender.getContent()); //设置邮件内容

    Transport.send(mailMessage);

    return true ;

    } catch (Exception e) {

    e.printStackTrace();

    return false ;

    }

    }

    public static boolean sendEmail(EmailMessage message){

    MailSendProper mail = new MailSendProper() ;

    mail.setHost("smtp.126.com");//smtp简单的邮件传输协议,默认端口号是25,

    mail.setPort("25");

    mail.setIsvalidate(true);//需呀身份验证

    mail.setUsername("");//设置登录用户名

    mail.setPassword("");//设置发送者的密码

    mail.setSendAddress("");//设置发送这的地址 发送者地址和登录用户名是同一个

    mail.setReceiveAddress(message.getToEmail());//设置接收者的地址

    mail.setSubject(message.getTitle());//设置邮箱标题

    mail.setContent(message.getContext());//设置邮箱的内容

    return mailTest(mail) ;

    }

    }

    注意:使用的邮箱必须开启POP3/SMTP服务才能发送成功,

    不同的邮箱,邮箱的传输协议不一样如

    QQ邮箱:SMTP传输协议是smtp.qq.com端口25

    POP3传输协议是pop3.qq.com 端口110

    5、使用方法测试

    public static void main(String[] args) {

    EmailMessage message = new EmailMessage() ;

    String code = String.valueOf(Math.random()).substring(3, 9) ; //生成验证码

    message.setTitle("邮箱验证");             //邮件标题

    message.setContext("尊敬的用户你好,你的验证码为"+code+"。"); //邮件内容

    message.setToEmail("940202884@qq.com"); //要发送给谁

    System.out.println(MailSend.sendEmail(message)?"发送成功":"发送失败") ;

    }

    了解详情请登陆昆明达内Java培训官网(km.Java.tedu.cn)!

    推荐文章

上一篇:Java --HashMap源码解析

下一篇:测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率

最新开班日期  |  更多

Java--零基础全日制班

Java--零基础全日制班

开班日期:11/30

Java--零基础业余班

Java--零基础业余班

开班日期:11/30

Java--周末提升班

Java--周末提升班

开班日期:11/30

Java--零基础周末班

Java--零基础周末班

开班日期:11/30

  • 网址:http://km .java.tedu.cn      地址:昆明市官渡区春城路62号证券大厦附楼6楼
  • 课程培训电话:186 8716 1620      qq:2066486918    全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56