• Java 发送邮件


    一、两个jar包

    二、代码

    import java.util.Properties;
    
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Authenticator;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    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 com.sun.mail.util.MailSSLSocketFactory;
    
    public class Test1 {
        public static void main(String[] args) {
            sendMail();
        }
    
        public static void sendMail() {
            String from = "90xxxx31@qq.com";
            String to = "229xxxx572@qq.com";
            String host = "smtp.qq.com";
            try {
                Properties properties = System.getProperties();
                //SSL加密
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                properties.put("mail.smtp.ssl.enable", "true");
                properties.put("mail.smtp.ssl.socketFactory", sf);
                properties.setProperty("mail.smtp.host", host);
                properties.put("mail.smtp.auth", "true");
    
                //获取发送邮件会话、获取第三方登录授权码
                Session session = Session.getDefaultInstance(properties, new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(from, "xxxxxxxx");//第三方登录授权码
                    }
                });
    
                Message message = new MimeMessage(session);
    
                //防止邮件被当然垃圾邮件处理,披上Outlook的马甲
                message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
    
                message.setFrom(new InternetAddress(from));
    
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    
                message.setSubject("This is the subject line!");
    
                BodyPart bodyPart = new MimeBodyPart();
    
                bodyPart.setText("发送");
    
                Multipart multipart = new MimeMultipart();
    
                multipart.addBodyPart(bodyPart);
    
                //附件
                // bodyPart = new MimeBodyPart();
                // String fileName = "文件路径";
                // DataSource dataSource = new FileDataSource(fileName);
                // bodyPart.setDataHandler(new DataHandler(dataSource));
                // bodyPart.setFileName("文件显示的名称");
                // multipart.addBodyPart(bodyPart);
    
                message.setContent(multipart);
    
                Transport.send(message);
                System.out.println("mail transports successfully");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    使用JAVA发送QQ邮件的总结

  • 相关阅读:
    Android—应用程序开机自启
    Android—简单的仿QQ聊天界面
    Android—关于自定义对话框的工具类
    Android—基于GifView显示gif动态图片
    Android—ListView条目背景为图片时,条目间距问题解决
    Android—自定义开关按钮实现
    FileProvider的使用
    Android 7.0新特性
    Android SDK自带调试优化工具
    Android监视器概述
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/12104930.html
Copyright © 2020-2023  润新知