Java邮件发送

需要导入的jar包:mail.jar和activation.jar

587端口:

package mail;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class MailUtilsQQ587 {

    // 收件人,邮件主题及邮件内容通过参数形式传递
    public static void sendMail(String emailAddr,String emailSub,String emailMsg) throws MessagingException {

        // 创建Properties 类用于记录邮箱的一些属性
        Properties props = new Properties();

        // 表示SMTP发送邮件,必须进行身份验证
        props.put("mail.smtp.auth", "true");

        //SMTP服务器
        props.put("mail.smtp.host", "smtp.qq.com");

        //端口号,QQ邮箱给出了两个端口,465与587
        props.put("mail.smtp.port", 587);

        // 邮箱账号
        props.put("mail.user", "[email protected]");

        // 邮箱授权码
        props.put("mail.password", "xxxxxxxxxxxxxxxx");


        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);

        // 邮件消息
        MimeMessage message = new MimeMessage(mailSession);

        // 发件人
        InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
        message.setFrom(form);

        // 收件人
        InternetAddress to = new InternetAddress(emailAddr);
        message.setRecipient(MimeMessage.RecipientType.TO, to);

        // 邮件标题
        message.setSubject(emailSub);

        // 邮件内容体
        message.setContent(emailMsg, "text/html;charset=UTF-8");

        // 发送邮件
        Transport.send(message);
    }
}

465端口:

package mail;

import java.util.Properties;

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;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtilsQQ465 {
    public static void senMail(String emailAddr, String emailSub,String emailMsg) throws MessagingException {

        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp"); // 连接协议
        properties.put("mail.smtp.host", "smtp.qq.com"); // 主机名
        properties.put("mail.smtp.port", 465);  // 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");  // 设置是否使用ssl安全连接 (一般都使用)
        properties.put("mail.debug", "true"); // 设置是否显示debug信息 true 会在控制台显示相关信息

        // 得到回话对象
        Session session = Session.getInstance(properties);

        // 获取邮件对象
        Message message = new MimeMessage(session);

        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress("[email protected]"));

        // 设置收件人地址
        message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress(emailAddr) });

        // 设置邮件标题
        message.setSubject(emailSub);

        // 设置邮件内容
        message.setContent(emailMsg,"text/html;Charset=UTF-8");

        // 得到邮差对象
        Transport transport = session.getTransport();

        // 连接邮箱账户,密码为邮箱的授权码
        transport.connect("[email protected]", "xxxxxxxxxxxxxxxx");

        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
    }
}

  

猜你喜欢

转载自www.cnblogs.com/heqiuyong/p/9768106.html