• 利用JavaMail发送邮件:smtp.163.com


    一、利用JavaMail发送邮件案例:

    1、maven项目结构:

    2、先在pom.xml里边加入Javamail依赖,系统会根据坐标自动下载mail包(前提是配置好了maven):

    3、配置email.properties属性文件,主要是为了不更改代码的前提下,该改变发送邮件的一些基本信息:

    4、实现发送邮件的主体类SendMailUtils,代码下:

    package top.hzelin.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    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;
    
    
    
    public class SendMailUtils {
        private static String from = "";
        private static String user = "";
        private static String password = "";
        /*
         * 读取属性文件的内容,并为上面上个属性赋初始值
         */
        static {
            Properties prop = new Properties();
            InputStream is = SendMailUtils.class.getClassLoader().getResourceAsStream("email.properties");
            try {
                prop.load(is);
                from = prop.getProperty("from");
                user=prop.getProperty("username");
                password=prop.getProperty("password");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void sendMail(String to,String text,String title) {
            Properties props = new Properties();
            props.setProperty("mail.smtp.host", "smtp.163.com");//设置邮件服务器主机名
            props.put("mail.smtp.host", "smtp.163.com");
            props.put("mail.smtp.auth", "true");//发送服务器需要身份验证
            Session session = Session.getDefaultInstance(props);//设置环境信息
            session.setDebug(true);
            MimeMessage message = new MimeMessage(session);
            Multipart multipart = null;
            BodyPart contentPart = null;
            Transport transport = null;
            try {
                message.setFrom(from);//设置发件人
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject(title);
                multipart = new MimeMultipart();//设置附件
                contentPart = new MimeBodyPart();
                contentPart.setContent(text, "text/html;charset=utf-8");
                multipart.addBodyPart(contentPart);
                message.setContent(multipart);
                message.saveChanges();
                transport = session.getTransport("smtp");
                transport.connect("smtp.163.com", user, password);
                transport.sendMessage(message, message.getAllRecipients());
            } catch (MessagingException e) {
                
                e.printStackTrace();
            }finally {
                try {
                    transport.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
            
            
            
        }
    
    }

    5、测试发送邮件功能是否可用SendEmailTest:

    注意:email.properties配置文件中的密码应该是客户端授权码,不是登录密码,设置位置如下:

  • 相关阅读:
    NYOJ 267(郁闷的C小加(二)) 后缀表达式求值
    NYOJ 104 最大和
    NYOJ 654(01包优化)
    CMM (度量Metrics)
    CMM (软件工程与集成产品开发)
    CMM (同行评审Peer Review)
    Software Engineering (软件工程简述)
    CMM (需求管理Requirement Management)
    CMM (项目计划与跟踪Project Management)
    Symbian(Carbide IDE开发环境搭建)
  • 原文地址:https://www.cnblogs.com/zl-huang/p/10113011.html
Copyright © 2020-2023  润新知