美文网首页
【java】利用javamail发送邮件

【java】利用javamail发送邮件

作者: jiandanyaobai | 来源:发表于2019-11-26 22:49 被阅读0次

    如何通过javamail来发送邮件(SSL加密)??这里做一个小测试

    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 java.util.Properties;
    
    public class JavaSendEmail {
        public static void main(String[] args) throws MessagingException {
            Properties properties = new Properties();
            properties.put("mail.transport.protocol","smtp");//协议
            properties.put("mail.smtp.host","IP地址");
            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信息
    
            //会话对象
            Session session = Session.getInstance(properties);
            //获取邮件对象
            Message message = new MimeMessage(session);
            //设置发件人邮箱地址
            message.setFrom(new InternetAddress("kaibo_cg@kaibo.com"));
            //设置收件人邮箱地址
            message.setRecipients(Message.RecipientType.TO,
                    new InternetAddress[]{
                            new InternetAddress("收件人的邮箱@"),
                    });
            //设置邮件标题
            message.setSubject("发给我自己");
            //设置邮件内容
            message.setText("testtesttesttesttest");
            //得到邮箱对象
            Transport transport = session.getTransport();
            transport.connect("账号@","密码");
            //发送邮件
            transport.sendMessage(message,message.getAllRecipients());
            //关闭邮件对象
            transport.close();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:【java】利用javamail发送邮件

          本文链接:https://www.haomeiwen.com/subject/ejfkuctx.html