使用Java实现QQ邮件的发送

来源:互联网 发布:知乎 文人无耻 编辑:程序博客网 时间:2024/05/11 05:02
import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;/** * 发送邮件  * @author vicki * */public class Sendmail {public static void main(String[] args) {        boolean isSSL = true;        String host = "smtp.qq.com"; // 邮件服务器        int port = 465; // 端口号        String from = "448733929@qq.com"; //发送者        String to = "2955204239@qq.com"; // 收件人        boolean isAuth = true;        final String username = "448733929@qq.com"; // 邮箱登录账号        final String password = "**********"; // 邮箱登录密码        Properties props = new Properties();        props.put("mail.smtp.ssl.enable", isSSL);        props.put("mail.smtp.host", host);        props.put("mail.smtp.port", port);        props.put("mail.smtp.auth", isAuth);        Session session = Session.getDefaultInstance(props, new Authenticator() {            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(username, password);            }        });        try {            Message message = new MimeMessage(session);            message.setFrom(new InternetAddress(from));            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));            message.setSubject("主题");            message.setText("内容");            Transport.send(message);        } catch (AddressException e) {            e.printStackTrace();        } catch (MessagingException e) {            e.printStackTrace();        }        System.out.println("发送完毕!");    }}


0 0
原创粉丝点击