Java实现邮件发送

发送邮件可与使用java提供的mail.jar实现。

public class EmailUtils {

    private static String fromEmail="你的邮箱";//发件邮箱
    private static String sqm="授权码";//授权码,切记只能是第三方授权码,不要用密码
    private static String host="smtp.163.com";//服务器地址

    /**
     * 参数说明:
     * 1、接收人的邮箱
     * 2、接收人姓名
     * 3、要发送的消息
     * @throws MessagingException 
     * @throws UnsupportedEncodingException 
     * @throws UnknownHostException */
    public static void sendEmail(String toEmail,String name,String msg)  {
        Properties properties=new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");//设置邮箱协议
        properties.setProperty("mail.smtp.host", host);//设置邮箱的服务器地址
        properties.setProperty("mail.smtp.auth", "true");//设置是否请求认证
        //获取会话对象---连接邮箱服务器
        Session session=Session.getInstance(properties);
        session.setDebug(true);//开启调试信息
        //创建消息对象

        try {
            MimeMessage message=new MimeMessage(session);
            //设置消息信息
            message.setFrom(new InternetAddress(fromEmail, "发件人","UTF-8"));//设置发件人信息
            message.setRecipient(MimeMessage.RecipientType.TO, 
                    new InternetAddress(toEmail, name, "utf-8"));
            message.setSubject("标题");
            message.setContent(msg,
                    "text/html;charset=utf-8");
            message.setSentDate(Calendar.getInstance().getTime());//设置发送时间
            //获取传输对象
            Transport transport=session.getTransport();
            //登录
            transport.connect(fromEmail, sqm);
            //发送邮件
    transport.sendMessage(message,message.getAllRecipients());
            //关闭
            transport.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 可以使用 JavaMail API 实现邮件发送功能。以下是一个简单的代码示例: ``` import java.util.Properties; 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; public class MailSender { public static void main(String[] args) { final String username = "your-email@example.com"; // 替换为你的邮箱用户名 final String password = "your-password"; // 替换为你的邮箱密码 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.example.com"); // 替换为你的 SMTP 服务器地址 props.put("mail.smtp.port", "587"); // 替换为你的 SMTP 端口号 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your-email@example.com")); // 替换为你的发件人邮箱地址 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@example.com")); // 替换为你的收件人邮箱地址,多个邮箱地址用逗号分隔 message.setSubject("Test Email"); message.setText("This is a test email sent from Java."); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值