集册 Java实例教程 发送短信

发送短信

欢马劈雪     最近更新时间:2020-01-02 10:19:05

429
发送短信


//package com.nowjava;
/**来自 n o w j a v a . c o m - 时代Java**/

import java.util.Properties;

import javax.mail.Message;


import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;


public class Main {

    public static void sendTextMessage(String to, String link,

            String subject, String message2) throws Exception {/*来 自 时代Java公众号*/

        Properties properties = definePropertyParameters();


        //mail session

        Session session = Session.getDefaultInstance(properties,

                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {

                        return new PasswordAuthentication(

                                "ramazanfirin@gmail.com", "ra5699mo");

                    }

                });


        //mail message


        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress("from@no-spam.com"));

        message.setRecipients(Message.RecipientType.TO,

                InternetAddress.parse(to));

        message.setSubject(subject);

        message.setText(message2 + "\n\n " + link);


        Transport.send(message);


        System.out.println("Done");


    }


    public static Properties definePropertyParameters() {

        //Properties object to set environment property parameters

        Properties props = new Properties();



        props.put(
展开阅读全文