码迷,mamicode.com
首页 > 编程语言 > 详细

使用java实现发送邮箱

时间:2019-09-20 14:03:42      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:基本配置   public   prope   mic   htm   成功   turn   tran   protected   

第一步:依赖
         <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实现发送邮箱

标签:基本配置   public   prope   mic   htm   成功   turn   tran   protected   

原文地址:https://www.cnblogs.com/zuiweng/p/11556719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!