java 发送邮件功能(本地/线上均可)

java 发送邮件功能

第一步先写一个工具类  授权码通过登录自己的邮箱获取
public class JavaMail {
    public void sendMail(String name,String email,String content) throws MessagingException {
        Properties props = new Properties();
        props.setProperty("mail.smtp.timeout", "200000");//设置链接超时
        props.setProperty("mail.smtp.port", Integer.toString(25));//设置端口
        props.setProperty("mail.smtp.socketFactory.port", Integer.toString(465));//设置ssl端口
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host","smtp.163.com");// smtp服务器地址
        Session session = Session.getInstance(props);
        session.setDebug(true);

        Message msg = new MimeMessage(session);
        msg.setSubject("尊敬的"+name+": 您好");
        msg.setText(content+"\r\n"+"(根据您的留言,稍后我们会与您取得联系)");
        msg.setFrom(new InternetAddress("XXX@163.com"));//发件人邮箱(我的163邮箱)
        msg.setRecipient(Message.RecipientType.TO,
                new InternetAddress(email)); //收件人邮箱
        msg.saveChanges();

        Transport transport = session.getTransport();
        transport.connect("XXX@163.com","XXXXXXXXX");//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
        try{
            transport.sendMessage(msg, msg.getAllRecipients());

            System.out.println("邮件发送成功...");
            transport.close();
        }catch (Exception e){
            System.out.println("邮件发送失败...");
            transport.close();
        }


    }
}
一定要设置 timeout 默认没有超时限制,25端口,465端口开放一下。
第二步 在代码中调用它实现发邮件功能
        JavaMail mail = new JavaMail();
        mail.sendMail(name,email,nr);

项目源码

https://github.com/zhoushibo-111/SSMairuiqiwebsite
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java发送邮件功能可以通过使用JavaMail API来实现。JavaMail API是Java平台上用于发送和接收邮件的标准API,它提供了一个灵活且可扩展的框架,允许开发人员使用各种邮件协议进行邮件的发送和接收。 要在Java发送邮件,首先需要配置SMTP服务器的信息,包括SMTP服务器的主机名、端口号、是否需要身份验证等。通过JavaMail API,可以创建一个连接到SMTP服务器的会话对象,并根据配置信息建立与服务器的连接。 使用JavaMail API发送邮件需要使用以下关键类: 1. Session类:用于创建SMTP协议会话,提供与SMTP服务器的连接。 2. MimeMessage类:表示邮件的消息体,包括邮件的发送者、接收者、主题、内容等。可以设置邮件的附件、图片等相关信息。 3. Transport类:用于发送邮件。可以通过发送Transport.send(message)方法将邮件发送出去。 示例代码如下: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendMail { public static void main(String[] args) { String host = "smtp.example.com"; // SMTP服务器主机名 String port = "25"; // SMTP服务器端口号 String username = "sender@example.com"; // 发件人邮箱用户名 String password = "password"; // 发件人邮箱密码 String toAddress = "recipient@example.com"; // 收件人邮箱地址 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); message.setSubject("Java邮件发送测试"); message.setText("这是由Java程序发送的一封测试邮件。"); Transport.send(message); System.out.println("邮件发送成功。"); } catch (MessagingException e) { System.out.println("邮件发送失败。"); e.printStackTrace(); } } } ``` 以上示例中,首先根据SMTP服务器的信息创建Properties对象,然后创建Session对象,其中包含SMTP服务器的相关配置信息和身份验证信息。然后创建MimeMessage对象,设置邮件的发送者、接收者、主题和内容等。最后通过Transport类发送邮件。 通过以上的代码示例,我们可以在Java中实现邮件的发送功能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值