JavaMail的使用,使用代码发送邮件

版权声明:本文版权归Heavn所有,转载或引用请注明出处 https://blog.csdn.net/qq_38137411/article/details/82830054

本文属于原创,转载请注明出处。

最近做了一个时光胶囊的项目,它要求写一个能够定时发送邮件的后台,想了想就决定用java来写。

首先,要引入两个jar包,可以在这里下载javamail的jar包
然后就想着要用什么邮箱来发送邮件,一开始想着用163网易邮箱,但是后来测试的时候又出现了许多不知名的bug,特别奇怪,之后就弃用了它,改用QQ邮箱。
下面说一下使用QQ邮箱的注意事项:

  1. 首先刚注册的QQ号刚开通QQ邮箱是无法使用的,因为开启POP3/SMTP服务服务需要开通QQ邮箱满14天才行。
  2. 其次,就是要开通POP3/SMTP服务,在QQ邮箱里面的设置->账户选项里面,开通之后请记住它给出来的QQ邮箱授权码,之后登录该邮箱需要使用这个授权码。
  3. 然后要注意的就是QQ邮箱的这些资料:QQ邮箱服务器地址描述
  4. 之前测试的时候用的是非SSL协议端口,即25端口,但是我的服务器是阿里云,而阿里云限制了25端口,防止滥发邮件,所以只能使用SSL协议端口号,即465端口。

下面贴出使用的具体的代码:

下面三个是发送邮件相关的类:

public class Auth extends Authenticator {

    private String username = "";
    private String password = "";

    public Auth(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}
public class Mail {

    private Properties props;// 系统属性
    private Session mailSession;// 邮件会话对象
    private MimeMessage mimeMsg; // MIME邮件对象


    public Mail(String SMTPHost, String Port, String MailUsername, String MailPassword) {
        Auth au = new Auth(MailUsername, MailPassword);
        // 设置系统属性
        props = java.lang.System.getProperties(); // 获得系统属性对象
        props.put("mail.smtp.host", SMTPHost); // 设置SMTP主机
//        props.put("mail.smtp.port", Port); // 设置服务端口号
        props.put("mail.smtp.auth", "true");//开启认证
        props.put("mail.smtp.socketFactory.port", Port);//设置ssl端口
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.smtp.auth", "true");// 同时通过验证
        // 获得邮件会话对象
        mailSession = Session.getInstance(props, au);
    }

    public boolean sendingMimeMail(String MailFrom, String MailTo,
                                   String MailCopyTo, String MailBCopyTo, String MailSubject,
                                   String MailBody) {
        try {
            // 创建MIME邮件对象
            mimeMsg = new MimeMessage(mailSession);
            // 设置发信人
            mimeMsg.setFrom(new InternetAddress(MailFrom));
            // 设置收信人
            if (MailTo != null) {
                mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
                        .parse(MailTo));
            }
            // 设置抄送人
            if (MailCopyTo != null) {
                mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,
                        InternetAddress.parse(MailCopyTo));
            }
            // 设置暗送人
            if (MailBCopyTo != null) {
                mimeMsg.setRecipients(javax.mail.Message.RecipientType.BCC,
                        InternetAddress.parse(MailBCopyTo));
            }
            // 设置邮件主题
            mimeMsg.setSubject(MailSubject, "gb2312");
            // 设置邮件内容,将邮件body部分转化为HTML格式
            mimeMsg.setContent(MailBody, "text/html;charset=gb2312");
            //mimeMsg.setDataHandler(new javax.activation.DataHandler(
            //	new StringDataSource(MailBody, "text/html")));
            // 发送邮件
            Transport.send(mimeMsg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
public class Send {
    private String title,content,time,email,sender,write;
    private Boolean sent;
    private String id;

    public Send(String id,String title, String content, String time, String email, String sender, String write, Boolean sent) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.time = time;
        this.email = email;
        this.sender = sender;
        this.write = write;
        this.sent = sent;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getSent() {
        return sent;
    }

    public void setSent(Boolean sent) {
        this.sent = sent;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getWrite() {
        return write;
    }

    public void setWrite(String write) {
        this.write = write;
    }
}

发送邮件的方法

private boolean sendmail(){
        String MailTo="收件方邮箱地址";
        String MailSubject="邮件主题";
        String MailBCopyTo="";
        String MailCopyTo="";
        String MailBody= "邮箱内容";
        String SMTPHost = "smtp.qq.com";
        String Port="465";
        String MailUsername = "发件方邮箱地址";
        String MailPassword = "刚刚的邮箱授权地址";
        String MailFrom = "发件方";
        if(SMTPHost==null||SMTPHost==""||MailUsername==null||MailUsername==""||MailPassword==null||MailPassword==""||MailFrom==null||MailFrom=="")
        {
            System.out.println("Servlet parameter Wrongs");
        }
        Mail send = new Mail(SMTPHost,Port,MailUsername,MailPassword);
        if(send.sendingMimeMail(MailFrom, MailTo, MailCopyTo, MailBCopyTo, MailSubject, MailBody)){
            Date date1 = new Date();
            System.out.println(sdf.format(date1)+"发送成功");
            return true;
        }
        else
        {
            return false;
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_38137411/article/details/82830054
今日推荐