• java实现发送邮件功能


    项目中实现发送邮件功能,先书写一个小Demo,记录如下:

    POM.XML中导入依赖

    <!--  start java 提供的支持邮件发送相关业务的类 -->
            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>1.4.7</version>
            </dependency>
            <!--  end java 提供的支持邮件发送相关业务的类 -->
    package com.xiaowu;
    
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    
    /**
     * 
     * java实现邮箱发送邮件功能
     * 
     * 
     * @author WQ
     *
     */
    public class JavaMail {
         public static void main(String[] args) throws Exception {
                Properties props = new Properties();
                props.setProperty("mail.smtp.auth", "true");
                props.setProperty("mail.transport.protocol", "smtp");
                props.put("mail.smtp.host","smtp.163.com");// smtp服务器地址
                
                Session session = Session.getInstance(props);
                session.setDebug(true);
                
                Message msg = new MimeMessage(session);
                msg.setSubject("这是一个测试程序....");
                msg.setText("你好!这是我的第一个javamail程序---WQ");
                msg.setFrom(new InternetAddress("XXXXXXXXXXX@163.com"));//发件人邮箱(我的163邮箱)
                msg.setRecipient(Message.RecipientType.TO,
                        new InternetAddress("XXXXXXXXXXX@qq.com")); //收件人邮箱(我的QQ邮箱)
                msg.saveChanges();
    
                Transport transport = session.getTransport();
                transport.connect("XXXXXXXXXX@163.com","XXXXXXXXX");//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
                
                transport.sendMessage(msg, msg.getAllRecipients());
                
                System.out.println("邮件发送成功...");
                transport.close();
            }
    }
  • 相关阅读:
    POJ3164 Command Network
    UVa11401 Triangle Counting
    UVa11174 Stand in a Line
    UVa11806 Cheerleaders
    Uva11538 Chess Queen
    Bzoj3130 [Sdoi2013]费用流
    Bzoj3262 陌上花开
    模拟25A 题解
    模拟24 题解
    模拟23 题解
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6958576.html
Copyright © 2020-2023  润新知