使用java实现发送邮箱

Posted zuiweng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用java实现发送邮箱相关的知识,希望对你有一定的参考价值。

第一步:依赖
         <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.6</version>
        </dependency>               

第二步:用户邮箱基本配置 email.properties

技术图片

 

email_name=xxx@163.com
email_password=xxxx
post=smtp.163.com

第三步:java代码

package com.mjkt.office.msg.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
 * 
 * @author zuiweng clq_zuiweng@163.com
 * 2019年9月20日
 */
public class MailAuthenticator extends   Authenticator{

    public static String USERNAME = "";
    public static String PASSWORD = "";

    public MailAuthenticator() {
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(USERNAME, PASSWORD);
    }

}
package com.mjkt.office.msg.mail;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;
/**
 * 邮件发送工具类
 * @author zuiweng ***@163.com
 * 2019年9月20日
 */
public class MailOperation {
    Logger logger = Logger.getLogger(MailOperation.class);

    /**
     * 发送邮件
     * @param user 发件人邮箱
     * @param password 授权码(注意不是邮箱登录密码)
     * @param host 
     * @param from 发件人邮箱
     * @param to 接收者邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return success 发送成功 failure 发送失败
     * @throws Exception
     */
    public String sendMail(String user, String password, String host,
            String from, String to, String subject, String content)
            throws Exception {
        if (to != null){
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            MailAuthenticator auth = new MailAuthenticator();
            MailAuthenticator.USERNAME = user;
            MailAuthenticator.PASSWORD = password;
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            try {
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                if (!to.trim().equals(""))
                    message.addRecipient(Message.RecipientType.TO,
                            new InternetAddress(to.trim()));
                message.setSubject(subject);
                
                MimeBodyPart mbp1 = new MimeBodyPart(); // 正文
                mbp1.setContent(content, "text/html;charset=utf-8");
                Multipart mp = new MimeMultipart(); // 整个邮件:正文+附件
                mp.addBodyPart(mbp1);
                message.setContent(mp);
                message.setSentDate(new Date());
                message.saveChanges();
                Transport trans = session.getTransport("smtp");
                trans.send(message);
                logger.info(message.toString());
                System.out.println(message.toString());
            } catch (Exception e){
                e.printStackTrace();
                return "failure";
            }
            return "success";
        }else{            
            return "failure";
        }
    }

}
package com.mjkt.office.msg.mail;

import com.mjkt.office.properties.read.ReadProperties;

public class MailStart {
    private static String email_name ;
    private static String email_password;
    private static String post;
    static {
    //读取email.properties文件 email_name
= ReadProperties.readProperties("email_name", "email.properties"); post = ReadProperties.readProperties("post", "email.properties"); email_password = ReadProperties.readProperties("email_password", "email.properties"); } /** * * @param content 邮件正文内容 * @param acceptMail 接受邮件的邮箱 * @param subject 邮件主题 * @return */ public static String startSendEmail(String content,String acceptMail,String subject) throws Exception{ MailOperation operation = new MailOperation(); String response = operation.sendMail(email_name, email_password, post, email_name, acceptMail, subject,content); return response; } public static void main(String[] args) { try { String response = startSendEmail("你好", "***@qq.com", "测试"); System.out.println(response); } catch (Exception e) { e.printStackTrace(); } } }

 

提示:

需要注意的是邮箱密码是邮箱授权密码:

技术图片

 

 技术图片

 

 发送邮件就这样简单的完成了。


 

以上是关于使用java实现发送邮箱的主要内容,如果未能解决你的问题,请参考以下文章

基于java mail实现简单的QQ邮箱发送邮件

Java实现邮箱发送验证码

Java实现发邮件功能---网易邮箱

使用java实现发送邮箱

你喜欢这个邮箱代码吗?用代码实现邮件的撰写和发送

使用java发送电子邮件