使用Java实现QQ邮件的发送


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 = "[email protected]"; //发送者
        String to = "[email protected]"; // 收件人
        boolean isAuth = true;
        final String username = "[email protected]"; // 邮箱登录账号
        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("发送完毕!");
    }
}


你可能感兴趣的:(使用Java实现QQ邮件的发送)