zoukankan      html  css  js  c++  java
  • 使用java mail的网易smtp协议 发送邮件

    package com.enation.newtest;
    
    import java.security.GeneralSecurityException;
    import java.util.Properties;
    
    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class TestMail {
    
        public static void main(String[] args) throws MessagingException, GeneralSecurityException {
            Properties props = new Properties();
    
            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            props.setProperty("mail.host", "smtp.163.com");
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");
    
            
            
            Session session = Session.getInstance(props);
    
            Message msg = new MimeMessage(session);
            msg.setSubject("seenews 错误");
            StringBuilder builder = new StringBuilder();
            builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
            builder.append("
    页面爬虫错误");
            builder.append("
    时间 2016 ");
            msg.setText(builder.toString());
            msg.setFrom(new InternetAddress("**发送人的邮箱地址**"));
    
            Transport transport = session.getTransport();
            transport.connect("smtp.163.com", "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**");
    
            transport.sendMessage(msg, new Address[] { new InternetAddress("**接收人的邮箱地址**") });
            transport.close();
        }
    
    }

    这个代码使用的是网易

    smtp.163.com服务,测试前把自己的账号开通smtp服务即可
  • 相关阅读:
    Java设计模式(Design Patterns)
    P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver
    P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)
    P4818 [USACO15DEC]Bessie's Dream 贝西的梦
    P3667 [USACO17OPEN]Bovine Genomics
    P4379 [USACO18OPEN]Lemonade Line
    P4378 [USACO18OPEN]Out of Sorts S
    P4089 [USACO17DEC]The Bovine Shuffle
    P4269 [USACO18FEB]Snow Boots G
    P4086 [USACO17DEC]My Cow Ate My Homework
  • 原文地址:https://www.cnblogs.com/jiafuwei/p/6098254.html
Copyright © 2011-2022 走看看