java 发邮件

public class EmailUtil {
    /**
     * todo 发送QQ邮箱
     */
    public void sendQQEmail(String emailAddress){
        //做链接前的准备工作  也就是参数初始化
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host","smtp.qq.com");//发送邮箱服务器
        properties.setProperty("mail.smtp.port","465");//发送端口
        properties.setProperty("mail.smtp.auth","true");//是否开启权限控制
        properties.setProperty("mail.debug","true");//true 打印信息到控制台
        properties.setProperty("mail.transport","smtp");//发送的协议是简单的邮件传输协议
        properties.setProperty("mail.smtp.ssl.enable","true");
        //建立两点之间的链接
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("675361896@qq.com","wbomvmxtifjybcbb");
            }
        });
        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人
        try {
            message.setFrom(new InternetAddress("675361896@qq.com"));

            //设置收件人
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(emailAddress));//收件人
            //设置主题
            message.setSubject("Activiti工作流审核结果通知");
            //设置邮件正文  第二个参数是邮件发送的类型
            message.setContent("尊敬的用户您好,您的请假申请已通过,请知悉!","text/html;charset=UTF-8");
            //发送一封邮件
            Transport transport = session.getTransport();
            transport.connect("675361896@qq.com","wbomvmxtifjybcbb");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * todo 发送网易163邮箱
     */
    // 发件人 账号和密码
    public static final String MY_EMAIL_ACCOUNT = "fantongxue666@163.com";
    public static final String MY_EMAIL_PASSWORD = "PCRAVAFQNZVIJDOI";// 密码,是你自己的设置的授权码

    // SMTP服务器(这里用的163 SMTP服务器)
    public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
    public static final String SMTP_163_PORT = "25";// 端口号,这个是163使用到的;QQ的应该是465或者875

    // 收件人
    public static final String RECEIVE_EMAIL_ACCOUNT = "fantongxue666@163.com";

    @Test
    public void sendWY163Email(String emailAddress) throws Exception{
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
        p.setProperty("mail.smtp.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");

        Session session = Session.getInstance(p, new Authenticator() {
            // 设置认证账户信息
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
            }
        });
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        // 发件人
        message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
        // 收件人和抄送人
        message.setRecipients(Message.RecipientType.TO, RECEIVE_EMAIL_ACCOUNT);
//		message.setRecipients(Message.RecipientType.CC, MY_EMAIL_ACCOUNT);

        // 内容(这个内容还不能乱写,有可能会被SMTP拒绝掉;多试几次吧)
        message.setSubject("Activiti工作流审核结果通知");
        message.setContent("尊敬的用户您好,您的请假申请已通过,请知悉!", "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();
        Transport.send(message);

    }
}

qq发邮件


import java.security.GeneralSecurityException;
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.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;


public class EmailUtil {

    /**
     * todo 发送QQ邮箱
     */
    public static void sendQQEmail(String account ,String passWord ,String adresss) throws GeneralSecurityException {
        // 收件人电子邮箱
        String to = adresss;
        // 发件人电子邮箱xx
        String from = "6x'x97824@qq.com";
        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("63x'x24@qq.com", "utgx'xgupx'xcea");
            }
        });xx
        try {xxxx
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));
            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // Set Subject: 头部头字段
            message.setSubject("x'xd科技有限xx公司:账户信息");
            // 设置消息体
            message.setText("您的账号为:"+ account +"密码 :" + passWord );
            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....from runoob.com");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

}

Java中发送邮件可以使用JavaMail API。下面是一个简单的Java程序示例,用于发送带有文本内容的邮件。 首先,需要导入javax.mail和javax.mail.internet包的类。 ```java import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { // 配置发送邮件的SMTP服务器信息 String host = "smtp.example.com"; final String username = "yourusername"; final String password = "yourpassword"; // 创建Properties对象,设置SMTP服务器信息 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); // 创建Session对象,用于发送邮件 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { // 创建Message对象,设置收件人、发件人和邮件内容 Message message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("邮件主题"); message.setText("邮件内容"); // 发送邮件 Transport.send(message); System.out.println("邮件发送成功"); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 上述代码中,需要替换以下信息: - `host`:SMTP服务器的主机名。 - `username`:发件人的用户名。 - `password`:发件人的密码。 - `sender@example.com`:发件人的邮箱地址。 - `recipient@example.com`:收件人的邮箱地址。 你也可以根据需要,使用JavaMail API发送附件、HTML内容等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值